1

I have been banging my head trying to figure out what is going wrong.

I am trying a simple server client TCP socket in java. Server can send multiple lines as response.
I am using while((str = in.readLine())!= null) at client side and it hangs after reading the response. Please find the code below. I have searched before posting. I am making sure I am ending the response with new line.

I have tried combinations of \n, \r\n but readLine is not detecting the end of line.
But it works fine at server end.

Please help.

Thanks.

SERVER:

import java.net.*;
import java.io.*;
public class SimpleServer {
public static void main(String args[]) throws IOException {

    ServerSocket s = new ServerSocket(55555);
    Socket socket = s.accept();

    BufferedReader in = new BufferedReader(
            new InputStreamReader(socket.getInputStream()));

    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

    String inputLine= null;
    System.out.println("call processInput"); 

       while ((inputLine = in.readLine()) != null) {
           System.out.println("before call processInput");           
           out.print("200 Success \r\n");
           out.flush();
           System.out.println("after call processInput: ");            
    }
    System.out.println("after while");
    out.close();
    in.close();
    socket.close();
    }
}

CLIENT:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.CharBuffer;

public class Test {
     public static void main(String[] args) throws IOException {

          try {

              System.out.println(":Connect");
                  Socket s = new Socket("192.168.1.114",55555);
                  System.out.println("Socket:" + s); 


                  System.out.println("after :Connect");



                  OutputStream s1out = s.getOutputStream();


                  PrintWriter output = new PrintWriter(s1out, true);   
                  output.println("login user root");
                  output.flush();


                  BufferedReader in = new BufferedReader(
                        new InputStreamReader(
                            s.getInputStream()));

                  System.out.println( in.readLine());



                  output.println("servershare");
                  output.flush();
                  System.out.println( "servershare");
                  String str = null;


             while((str = in.readLine())!= null) // hangs here
             {
                  System.out.println(str);

              }


               System.out.println( "share");
               output.println("share file1.txt file1.html, roopa ramesh");
                   str = null;
                   while((str = in.readLine())!= null && !str.equals(""))
                  System.out.println(str);

                  in.close();
                  output.close();
                  s.close();







          } catch (UnknownHostException e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

          } catch (IOException e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

          } 
     }
}

1 Answer 1

4

You have a deadlock :

  1. client sends login
  2. server reads login line and sends 200 OK, and waits for the next line
  3. client reads 200 OK
  4. client sends servershare
  5. server reads servershare line, sends 200 OK, and waits for the next line
  6. client reads 200 OK, and waits for the next line

Both the client and the server are waiting for the next line from the other end. Your protocol isn't correct.

Moreover, you're using the platform default encoding at server-side and client-side to read and write the messages. If the client and server don't have the same default encoding, you'll have a problem. You should wrap your streams with Input/Output stream writers and specify a specific encoding when constructing them.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Protocol is request or response should end with \r\n and if it ahs multiple lines each line should end with \r. Also readline should detect end of line and exit from while at client. So if server is sending multiple lines as response how to read it at the client end without using while loop??. Thanks
The readLine method of BufferedReader considers \r, \n and \r\n as valid end of lines. You should read char by char yourself if you need to do something different for \r and \r\n. Moreover, your loop doesn't stop when a line is read. It stops when readLine returns null, i.e. when there is nothing more to read in the stream. The only way for this loop to stop, is for the server to close the output stream.
Hi, thanks. But how do I check if a character read is \r\n since it is two cahrsacters. Do I hv to rememeber wht I read previous and if followed by \n confirm its \r\n.
Sorry can you please elaborate. I didnt quite understand.
BufferedReader has a mark and a reset methods. They might be useful to implement your protocol. Read their javadoc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.