0

I created an application which establishes connection with the given port and transport data either ways. But I am having issues in reading the data from the server.

try{

    Socket skt = new Socket(127.98.68.11, 1111); // connecting to this to get data

    String message = "some test message";

    if(option.equalsIgnoreCase("send")){

         OutputStream outToServer = skt.getOutputStream();
          outToServer.write(message); // this is working, message stored on server-side
    }else if(option.equalsIgnoreCase("receive")){
          BufferedReader in = new BufferedReader (new InputStreamReader(sit.getInputStream()));

          String fromServer = in.readLine();
          System.Out.Println(fromServer);
    }

}catch(IOException io){
   io.printStackTrace();
}

In this program everything is working as expected. except in.readline().

I tried running this program in debugging mode, and the by the time compiler reaches this command. is was doing nothing and i can't see the cursor also

4
  • What do you expect to happen ? It sounds like your server isn't sending you a line, so debug what's happening on the server (and even on the TCP stream using e.g. Wireshark.) Commented Aug 3, 2011 at 20:04
  • 1
    Your BufferedReader is getting an input stream from a variable called sit but your Socket is declared with a name of skt. Is this a typo? Commented Aug 3, 2011 at 20:04
  • What about equalsIgnoreCase(receive) without quotes? Commented Aug 3, 2011 at 20:07
  • These typo errors were not in the actual code. my question is I'm not getting the stream of data or not even throwing any exception/errors. the program keeps running for hours and terminating without printing the expected stream of data Commented Aug 3, 2011 at 21:02

1 Answer 1

1

It could be because you are trying to do an in.readLine() this requires that the server terminates the "receive" command which it is sending to the client with a newline.. "\n" or "\r\n" along

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

2 Comments

does in.read() get the full string of data? can you tell how to get the data. because whatever read/readLine the program is terminating. how to get this work??
in.read() will only retrieve a single character.. you need to use in.read(char[], int, int) method to read into an char buffer array. keep reading until you get a -1 after which you convert the buffer array to a String.. then check if this string.equalsIgnoreCase("receive")

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.