Socket Multithreading - Reading an input stream pauses the thread

I am testing sockets on my local machine. I am trying to start socket and server in the same program using streams. My server is an echo server, so it sends back all received messages. My problem is that when I run both streams, both client and server, they freeze when they reach the part where I read the input stream. It works great up to the part where the client sends the message. Afterwards, it just stops as it seems like the client is waiting for the message as well as the server, even though I already sent the message to the server via writing to the output stream. What's wrong with the code?

Client.java

@Override
    public void run() {

        try {
            Socket socket = new Socket("localhost", 22600);

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    socket.getInputStream()));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                    socket.getOutputStream()));
            BufferedReader input = new BufferedReader(new InputStreamReader(
                    System.in));
            System.out.println("Client 1");

            while (true) {

                System.out.print("\nEnter text : ");
                String inputText = input.readLine();
                writer.write(inputText);
                System.out.println("Client 2");

                System.out.println("Client 3");
                String s = br.readLine();
                System.out.println("CLIENT RECEIVED : " + s);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

      

Server.java

@Override
public void run() {

    try {
        ServerSocket server = new ServerSocket(22600);
        Socket socket = server.accept();
        BufferedReader br = new BufferedReader(new InputStreamReader(
                socket.getInputStream()));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                socket.getOutputStream()));


        System.out.println("Server 1");
        while (true) {

            System.out.println("Server 2");
            String s = br.readLine();
            System.out.println("Server 3");
            if (s == null) {
                System.out.println("NULL SERVER SIDE ERROR");
                break;
            }

            writer.write("ECHO : " + s);
            System.out.println("SYSOUT ECHO " + s);

        }
        server.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

      

+3


source to share


2 answers


You are writing a line that has no line end at the end.

            String inputText = input.readLine();
            writer.write(inputText);
            System.out.println("Client 2");

      

The line inputText

does not include the end of the line entered. And you write it as it is on the server. However, the server tries to read the line:



        String s = br.readLine();
        System.out.println("Server 3");

      

This way it will keep waiting for the client to send a new line. But by now the client is waiting for a response from the server and now they are at a dead end.

So, you have to add to the client writer.newLine()

as well as the echo server which is suffering from the same problem. It is also recommended to use it after each entry writer.flush()

, both on the server and on the client. Otherwise, it can wait until the buffer is full before actually writing, and the same deadlock occurs.

+5


source


The readLine

BufferedReader method requires the new line terminator to return a value (if the end of the stream has not been reached) and then returns a line without that character. So the client

  • Reads a string from the user into a variable inputText

  • Client writes inputText

    to OutputStream
  • The server receives data, but waits until it receives a new row (which it does not).


If you want to use a newline as a link separator, add it to the end of the submitted data

writer.write(inputText + "\n");

      

+3


source







All Articles