5 Replies Latest reply: Sep 16, 2010 10:29 PM by EJP RSS

    network IOstream deadlock problem

    843789
      I wrote a static method is called by different console chat clients. It's purpose is as follows:
      1. takes InputStream and OutputStream objects as parameters
      2.let's user enter the text info to be sent.
      3.checks if buffer contains incoming msg
      4.send the text user keyed in.
      5.if user entered "end of stream" method terminates.

      the above mentioned runs in a infinite loop with (5) being the exit condition.
      However when i run this method, i ended up with a dead lock.
      Please tell me why did the dead lock occur, and how to solve the problem.

           /******
            *Chat module for client to client chats
            *  Returns true if the other party asked for disconnection
            *  Returns false if user asked for disconnection
            ***********************************************************/
           public static boolean ChatModule(InputStream in, OutputStream out) throws Exception{
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                Scanner keyboard = new Scanner(System.in);
                String input;
                try{
                     while(true){
                          if(br.ready())
                               System.out.println("Recv : "+br.readLine());
                          System.out.print("Send : ");
                          input = keyboard.nextLine();
                          //Disconnect if user typed "end of stream"
                          if(input.equalsIgnoreCase("end of stream"))
                               return false;
                          out.write(input.getBytes());                    
                     }
                }
                catch(IOException e){
                     e.printStackTrace();
                     System.out.println("Most likely the other user has disconnected.");
                     return true;
                }
           }
        • 1. Re: network IOstream deadlock problem
          EJP
          (a) Get rid of the br.ready() test.

          (b) Are you reading lines at the other end? because you're not sending lines. readLine() removes the line separator. If the other end is reading lines, it will never get a line separator, so it will never exit from readLine() unless you close the socket.

          (c) is 'out' buffered? If so, you need to flush it.
          • 2. Re: network IOstream deadlock problem
            843789
            I got the problem solves.

            a. I kept the .ready() test. seems that it is not affecting much.
            b. i still dont quite get the line separator part, though i fixed the problem by doing a .read(char[] cbuf, int offset, int len) . From my past assignment experiences, getlines in C and readLines and nextLines in java often creates a null-terminator problem, yet i am not what is the exact mechanism behind it.

            c. Nope. i kept it unbuffered and parse it myself; so stay away from the flush problem.
            • 3. Re: network IOstream deadlock problem
              EJP
              b. i still dont quite get the line separator part
              It's simple. readLine() reads until it gets a line terminator, throws it away, and returns. So the readLine() in your client returns the line without the line terminator. write() doesn't add one. So a readLine() at the other end will block forever.
              From my past assignment experiences, getlines in C and readLines and nextLines in java often creates a null-terminator problem
              No they don't. There is no null-terminator problem in Java.
              yet i am not what is the exact mechanism behind it.
              That's because it doesn't exist in Java.
              • 4. Re: network IOstream deadlock problem
                843789
                so... theoractically
                if i do a
                String msg = oldmsg + "\n"; //is "\n" a line separator? in any case i'll assume it is for this scenario
                out.write(msg.getBytes() ); should not block the other side right ?

                Edited by: J-Ideas on Sep 16, 2010 8:20 PM
                • 5. Re: network IOstream deadlock problem
                  EJP
                  Correct