简体   繁体   中英

How to get a server to send a client a message on connection?

I am trying to write a basic client-server program to allow the client to traverse the files of the server. I wish to get the server to, on connection of the client, send a list of all the files in the current directory to the client. How would I go about doing this ? I have already created an array of all the filenames, and am trying to send these to the client it just sits in an infinite loop (as it is meant to !) and does nothing.

Attempting to have the server send a message on connection of the client :

    DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());


    boolean found = false;

    //Read the data from the input stream
    for (int i=0;i < fileList.length;i++) {
      outToClient.writeBytes(fileList[i].getName());
    }

And to have the server receive this :

//Prepare an object for reading from the input stream
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

//Read the data from the input stream
sentence = inFromServer.readLine();

but doing this just makes the client sit in an infinite loop and do nothing.

Help please?

First make sure that you have following handshaking between client and server:

  1. listening server
  2. client connecting
  3. server accepts
    now there are two paths:
  4. server listen for command
  5. client sends command 'dir'
  6. client start to listen
  7. server sends back your list
    alternate path:
  8. client listens
  9. server sends list
  10. server start to listen

... normal communication.
Sending 0 size messages might cause problems sometimes.
So using query-response approach seems to be accurate.

You primary problem is that you are attempting to send an empty string from the client to the server. An empty string translates to a 0 length byte array. In effect, you are not sending any data between the client and the server. In this situation, your server will continue to wait for data to be retrieved over the socket's InputStream. Since the server is still waiting, it does not move on to send the data back to the client. As a result, when you client tries to listen for data from the server, it waits indefinitely as the server never reaches that part of the code.

If your goal is to have the server simply send the data upon connect, you have a couple of options.

  1. Upon connection have the server immediately send the list to the client. You do not have to have two way communication here. The client can start listening on the InputStream immediately.
  2. Have the client send at least 1 byte to the server that server will be able to retrieve over its InputStream and then continue to process. If you need to perform multiple functions, then you may want to look into sending a string value that means something, eg, out.write("list".getBytes("UTF-8")); The server can then perform an operation based on the received value.

Example Server:

ServerSocket socket = new ServerSocket(8888);
Socket cSocket = socket.accept();

PrintWriter out = null;
try {
    out = new PrintWriter(new OutputStreamWriter(cSocket.getOutputStream()));

    for (String file : new File(".").list()) {
        out.println(file);
    }
}
finally {
    if (out != null) {
        out.close();
    }
    cSocket.close();
    socket.close();
}

Example Client:

Socket s = new Socket("localhost", 8888);
BufferedReader in = null;

try {
    in = new BufferedReader(new InputStreamReader(s.getInputStream()));

    String line;
    while ((line = in.readLine()) != null) {
        System.out.println(line);
    }
}
finally {
    if (in != null) {
        in.close();
    }
    s.close();
}

In my basic server-client TCP example I make a way ...when a client is got accepted it got a welcome message . In that when from client side I send role , it gives me back name. Here is the link of source >> http://matrixsust.blogspot.com/2011/10/basic-tcp-server-client.html .

Hope it helps.

I don't believe it is possible to have a server send a message to a client unprompted. Send a message "dir" and listen on the server, then listen on client and reply.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM