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`