A socket server waiting for messages from a client to read

I have the following socket server in java,

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class MiddleSocket { private ServerSocket middleman; private int port = 1027; private Socket client; protected void createSocketServer() { try { middleman = new ServerSocket(port); client = middleman.accept(); middleman.close(); PrintWriter out = new PrintWriter(client.getOutputStream(),true); BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); while(true) { System.out.println("echo: " + in.readLine()); out.println("test"); } } catch(IOException e) { System.out.println(e); } } } 

It works correctly in the way it reads from the client and everything. However, I want the socket server to only try to read and read when there is a message from the client, because in the loop it continues to read, even if messages from the client are not received, it gives a heap

echo: null

Is there some mechanism that is less than wait() to have a while loop, to know, to just sit and wait, and then run when a message is sent from the client to read this message, and as soon as this message is read, and the answer will be sent, and then just sit idle until the next message is received from the client?

+5
source share
3 answers

You can just do something like the following:

 String line; while ((line = in.readLine()) != null) { \\Do stuff } 

This should have the expected behavior.

Edit:

here is a complete example of what I said in the comments using your code:

 package javaapplication12; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class SocketExample { private ServerSocket middleman; private int port = 8080; private Socket client; protected void createSocketServer() { try { while (true){ middleman = new ServerSocket(port); client = middleman.accept(); middleman.close(); PrintWriter out = new PrintWriter(client.getOutputStream(),true); BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); String line; while((line = in.readLine()) != null) { System.out.println("echo: " + line); out.println("test"); } } } catch(IOException e) { System.out.println(e); } } public static void main(String[] args){ SocketExample test = new SocketExample(); test.createSocketServer(); } } 
+3
source

You can use InputStream.read () or InputStream.read (byte []). Both will block

Here is an example

+2
source

I want the socket server to only try to read and read when there is a message from the client, because in the loop it continues to read, even if messages from the client are not received, it gives a bunch

 echo: null 

This is because you are ignoring the end of the stream, which means zero. This does not mean that there are no messages from the client at all. This means that the partner has closed the connection.

+1
source

Source: https://habr.com/ru/post/1205110/


All Articles