简体   繁体   中英

Java client-server issue

Please have a look at the example here .

Wanting to be able to keep the connection alive and send multiple client messages to the server, I changed the code to this:

// Send the message to the server
OutputStream os = Client.socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
InputStream is = Client.socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);

for (int i = 0; i < 10; i++) {
    String sendMessage = i + "\n";
    bw.write(sendMessage);
    bw.flush();
    System.out.println("Message sent to the server : " + sendMessage);
    String message = br.readLine();
    System.out.println("Message received from the server : " + message);
}

I cannot understand why this only works for the first message.

Could someone please explain me ?

Update:

Sysouts:

Server side:

`Server Started and listening to the port 25000`
`Message received from client is 0`
`Message sent to the client is 0`

Client side:

`Message sent to the server : 0`
`Message received from the server : 0`
`Message sent to the server : 1`

The problem I see in the posted code link is server does not handle request completely, it accepts (client socket connects) and reads whatever is available on this connection and then waits for more client connections (accepts) which your client program never makes (which is fine).

It does not keep on reading client's input stream (just read once) hence whatever sent to client after first request is discarded.

At server side, once a connection is made with client (accepts) fork a new thread and pass associated socket to it. In this thread (say request handler) have a loop around socket input stream

Server:

 Socket socket = serverSocket.accept()
 InputStream is = socket.getInputStream();
 InputStreamReader isr = new InputStreamReader(is);
 BufferedReader br = new BufferedReader(isr);
while(socket.isConnected() && !socket.isInputShutdown())
{
 String number = br.readLine(); //this would block until message arrives from client socket
 //further logic
 //write back to client etc
}

OK, sorry, hadn't seen it. The problem is on the server side. The ServerSocket is just a listening socket to accept connections from many clients. Normally, you would start a new Thread after each "accept" which will handle the resulting Socket object.

The socket object is what finally represents your real connection. Now, where you have to use the loop is for reading from your socket, not from the serversocket.

At the end, it means that the Server side is not properly implemented.

You could also use it to attend only one client in a one-threaded environment if that's what you need, but then the right practice is to close the socket object inside the while loop (at the end), and have the loop that controls the message just for the read/write functions.

Regards

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