Note that there are some explanatory texts on larger screens.

plurals
  1. PONothing back from server to client
    primarykey
    data
    text
    <p>I am trying to make a simple server/client application to get my head around using sockets. I can successfully get single line communication between server and client, for example client sends message to server, server acknowledges and sends single message back.</p> <p>The problem is when I try to read the incoming replies from the server back to the client when there are multiple lines in the BufferedReader.</p> <p>Examples...</p> <p>Server and client giving one line to each other.</p> <pre><code>import java.io.*; import java.net.*; import java.awt.*; class Server { public static void main(String args[])throws IOException { Server myServ = new Server(); myServ.run(); }//end main public void run() throws IOException { ServerSocket serSKT = new ServerSocket(729); Socket socket = serSKT.accept(); BufferedReader BR = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter PW = new PrintWriter(socket.getOutputStream()); String fromClient = BR.readLine(); System.out.println(fromClient); if(fromClient !=null) {PW.println("Server giving response to client");PW.flush();} }}//end class server </code></pre> <p>Client:</p> <pre><code>import java.io.*; import java.net.*; class Client { public static void main(String args[])throws IOException { Client myCli = new Client(); myCli.run(); } public void run() throws IOException { int port = 729; Socket mySkt = new Socket("localhost",port); PrintWriter myPW = new PrintWriter(mySkt.getOutputStream(),true); BufferedReader myBR = new BufferedReader(new InputStreamReader(mySkt.getInputStream())); myPW.println("Message from client to server"); String temp=myBR.readLine(); System.out.println(temp); }} </code></pre> <p>So the above works fine..now when I try to use for loops to print 10 lines of text to the server and then 10 lines back from the server to the client, I run in to problems. For example..Here is the server and client..</p> <pre><code>import java.io.*; import java.net.*; import java.awt.*; class Server { public static void main(String args[])throws IOException { Server myServ = new Server(); myServ.run(); }//end main public void run() throws IOException { ServerSocket serSKT = new ServerSocket(729); Socket socket = serSKT.accept(); BufferedReader BR = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter PW = new PrintWriter(socket.getOutputStream()); String fromClient = null; while((fromClient = BR.readLine())!=null) { System.out.println(fromClient);//this is being printed successfully } if(fromClient !=null) { for(int i=0;i&lt;10;i++){ PW.println("Server giving response to client"+i);} PW.flush();} }}//end class server </code></pre> <p>Client:</p> <pre><code>import java.io.*; import java.net.*; class Client { public static void main(String args[])throws IOException { Client myCli = new Client(); myCli.run(); } public void run() throws IOException { int port = 729; Socket mySkt = new Socket("localhost",port); PrintWriter myPW = new PrintWriter(mySkt.getOutputStream(),true); BufferedReader myBR = new BufferedReader(new InputStreamReader(mySkt.getInputStream())); for(int i =0;i&lt;10;i++) myPW.println("Message from client to server"+i); String temp=null; while((temp=myBR.readLine())!=null){ System.out.println(temp);} }} </code></pre> <p>The above will print the message 10 times to the server and the server will display what the client has sent it successfully:</p> <pre><code>while((fromClient = BR.readLine())!=null) { System.out.println(fromClient);//this is being printed successfully } </code></pre> <p>What I do not understand is why the client will not get the message "Server giving response to client" and print it to the console:</p> <pre><code>String temp=null; while((temp=myBR.readLine())!=null){ System.out.println(temp);} //should be printing "Server giving repsonse to client" </code></pre> <p>Sorry it is long, thanks but I am trying with no success!</p> <p><em><strong></em>****</strong><em>EDIT</em><strong><em>*</em>**</strong></p> <p>Incase anyone else comes across this problem, This was solved by printing a command to the printwriter in the client class to allow the server to know it has finished. e.g:</p> <pre><code>for(int i =0;i&lt;10;i++){ myPW.println("Message from client to server"+i);} myPW.println("Finished"); </code></pre> <p>Now in the server class the text being sent to it should be checked for the "Finished" command, if so, break out of the while loop and start sending back to the client, I believe that the only way BufferedReader will be null is if it is closed, so it is infinitely looping. Thanks for the help everyone.</p> <pre><code>while((fromClient = BR.readLine())!=null) { if(fromClient.equals("Finished")) { break; } System.out.println(fromClient); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload