3

Im trying to make a client/server socket connection via BufferedReader and Buffered Writer, but reader not reading anything it is just hanged, where client send and flush properly. Server does not throw any exception as if client not sending anything to server.

My head is going go explode...

Im using same for both client and server:

in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

Here is the code of server:

//inside of try catch
while(true){
          while(!in.ready())// just to debug
            System.out.println("READY: " + in.ready()); //just to debug
          System.out.println("READY: OK"); //just to debug
            msg = receive().toString(); //hangs here...
            System.out.println("KEYIS: " + msg);
           ///some stuff to do ....

public StringBuilder receive() throws IOException {
    StringBuilder str = new StringBuilder();
    int tmp;
    while(true){
        tmp = in.read();
        if(tmp == 0)
            break;
        else if(tmp == -1)
            throw new IOException();
        str.append((char)tmp);
    }
    return str;
}

Client code: not hanging here

 //inside of try catch
         send(KEY); //sended properly, no exception
         while(true){
             send(KEY); // sended properly, no exception
             System.out.println("sent");
             //System.out.println(receive().toString());
         }

public void send(String str) throws IOException{
    out.write(str + "\n"); //
    //out.newLine(); //tried too, not helped
    out.flush(); //push message to server
}
2
  • 1
    Why do you use BufferedReader? Try to remove it, that should help. Commented Dec 4, 2014 at 22:20
  • As per the answer below by the poster, this was caused by a typo and is not reproducible. I'm voting to close this question. Commented May 5, 2021 at 21:33

4 Answers 4

2

Well server waits for if(tmp == 0) which is 0 is a nil, and the client never sends it.
I think You are waiting about \n which it's not 0, it's 10(line feed).
Just wondered why don't you use DataOutputStream#writeUTF() and DataInputStream#readUTF()?

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

3 Comments

\0 used to determine the end of data, -1 means that there was an error in connection.
readUTF can not be resolved by lua program, that will connect to server too
@Bob No, an IOException means that there was an error in the connection. -1 means that the peer has closed the connection. No error.
1

You're writing lines but you have a pretty pointless and elaborate and very buggy input method which doesn't actually read lines, and which throws the wrong exception at end of stream.

Try BufferedReader.readLine(), not forgetting to test for null.

2 Comments

I have to pass the data that may contain \n \r characters, so cant use readLine
So use BufferedInputStream.read() then, and don't use String as a container for binary data.
1
    StringBuffer sb = new StringBuffer();
    int i;
    while ((i = bi.read()) != -1) {
        char c = (char) i;
        sb.append(c);
        
    }
    return sb.toString();

Comments

0

Solved the problem... So stupid mistake... I just forgot to add a \0 to determine the end of message, so recieve method was waiting as if more data coming...

Comments

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.