The client cannot receive information from the server

I am trying to send file information from the server to the client, but I can not get the information on the client side. I think this is a threading error, but cannot find it.

I am debugging the code, and I see that the information is well read, but then something bad happens when it comes to the line:

while ((readLine = read.readLine()) != null) { 

This is null and it all ends.

 public class Server { private String urlFile = "http://riemann.fmi.uni-ofia.bg/vladov/students/boil.txt"; private ServerSocket serverSocket = null; private BufferedReader read = null; private BufferedWriter write = null; private FileReader fileReader = null; URLConnection urlConnection = null; void acceptConnection() { try { serverSocket = new ServerSocket(3000); Socket client = null; while (true) { client = serverSocket.accept(); handleConnection(client); } } catch (IOException e) { e.printStackTrace(); } } private void handleConnection(Socket clientSocket) { try { URL url = new URL(urlFile); urlConnection = url.openConnection(); read = new BufferedReader(new InputStreamReader( urlConnection.getInputStream())); write = new BufferedWriter(new OutputStreamWriter( clientSocket.getOutputStream())); String readLine = null; while ((readLine = read.readLine()) != null) { write.write(readLine); write.flush(); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void tearDownConnection() { try { write.close(); read.close(); serverSocket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 

Client

  public class Client { Socket client = null; BufferedReader reader = null; BufferedWriter writer = null; void connectToServer(String hostAddress, int port) { try { System.out.println("Client is waitting."); client = new Socket(hostAddress, port); reader = new BufferedReader(new InputStreamReader( client.getInputStream())); writer = new BufferedWriter(new OutputStreamWriter( client.getOutputStream())); String readedLine = null; readedLine = reader.toString(); while ((readedLine = reader.readLine()) != null) { System.out.println(readedLine); } } catch (UnknownHostException e) { System.out.println("Host name is unkown."); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } void tearDownConnection() { try { if (client != null) { client.close(); } if (writer != null) { writer.close(); } if (reader != null) { reader.close(); } } catch (IOException e) { e.printStackTrace(); } } } 
+4
source share
1 answer

The client expects a string (i.e. ends with a return / newline character), while the server does not send it. You can add a new line yourself on the server:

 while ((readLine = read.readLine()) != null) { write.write(readLine+"\n"); write.flush(); } 
+3
source

All Articles