Reputation: 696
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.
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.
Examples...
Server and client giving one line to each other.
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
Client:
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);
}}
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..
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<10;i++){
PW.println("Server giving response to client"+i);}
PW.flush();}
}}//end class server
Client:
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<10;i++)
myPW.println("Message from client to server"+i);
String temp=null;
while((temp=myBR.readLine())!=null){
System.out.println(temp);}
}}
The above will print the message 10 times to the server and the server will display what the client has sent it successfully:
while((fromClient = BR.readLine())!=null)
{
System.out.println(fromClient);//this is being printed successfully
}
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:
String temp=null;
while((temp=myBR.readLine())!=null){
System.out.println(temp);} //should be printing "Server giving repsonse to client"
Sorry it is long, thanks but I am trying with no success!
****EDIT***
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:
for(int i =0;i<10;i++){
myPW.println("Message from client to server"+i);}
myPW.println("Finished");
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.
while((fromClient = BR.readLine())!=null)
{
if(fromClient.equals("Finished"))
{
break;
}
System.out.println(fromClient);
}
Upvotes: 0
Views: 151
Reputation: 533550
You are waiting for the client to close the stream i.e. when br.readLine() == null
before sending anything. After this happens fromClient
will be null in which case you send nothing anyway
I suggest you try
for (String s; (s = BR.readLine())!=null;) {
System.out.println(s);
PW.println("Server response to " + s);
}
Upvotes: 1
Reputation: 10772
I think the issue is with:
while((fromClient = BR.readLine())!=null)
{
System.out.println(fromClient);//this is being printed successfully
}
BR.readLine()
will not return null
until the stream ends (eg is closed/disconnected). You shouldn't use this as your break condition, it will wait inside readLine()
for more data to appear on the stream.
Upvotes: 1
Reputation: 5315
It looks like you never exit the while() loop in the server, after the 10th line, the server waits for another line (and another, and another, etc.). The client has to notify the server that he has finished sending, e.g. by sending a special line. Then you should exit the while loop on the server and start sending.
Upvotes: 2