4 Replies Latest reply: Feb 18, 2009 5:05 PM by EJP RSS

    BufferedReader with Sockets

    807588
      I'm trying to make a simple server and client terminal that requests a username and password, but I can't even seem to get past step one. Everything in my program works fine until the server requests input from the user. How does the readLine() work from the server's point of view. Does the client have to enter data BEFORE the server attempts the readLine() method? How can I force the server to wait until input is received?

      Server side:
      //Assume Socket sock;
      BufferedReader bin = new BufferedReader(new InputStreamReader(sock.getInputStream()));
      String input;
      pout.println("Log in or create a new user.\nUsername:");
      while (input == null) {
          input = bin.readLine();
      }
      The server accepts the socket, outputs "Log in or create a new user.\nUsername:" and then loops input until it receives something from the client. This is where I've been hanging. When I type something into the terminal, nothing happens: no errors, no memory leaks, nothing.

      Client side:
      PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
      BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
      BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
      String fromServer, fromUser;
      while (true) {
           while ((fromServer = in.readLine()) != null) {
                System.out.println(fromServer);
           }
           
           System.out.println("TEST"); //test progress
           while (fromUser == null) {
                fromUser = stdIn.readLine();
                if (fromUser != null) out.println(fromUser);
           }
           if (fromUser.equals("/quit")) break;
      }
      I'm trying to have the client read output from the server until in.readLine() == null. The while loop will end forcing user input and then doing out.println(fromUser);. The client will loop this process indefinitely until they type /quit. Unfortunately, the client hangs somewhere after the fromServer while loop and the TEST line. The loop isn't indefinite as far as I've tested it, but I'm not entirely sure.

      Any help would be appreciated! Thanks in advance.

      --dorky                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
        • 1. Re: BufferedReader with Sockets
          807588
          Does anybody know what I might be doing wrong? I feel as if while ((fromServer = in.readLine()) != null) on the client side is an infinite loop. It's requesting information from the server until it sends a null response, but the server doesn't send anything. So how can I have the client print out each line from the server not knowing how many lines are to be read?

          --dorky                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
          • 2. Re: BufferedReader with Sockets
            dcminter
            I'm confused. Is your server sending anything to the client? If not, why would you expect it to display anything? If it is, how did you check? What does the sending code look like?
            • 3. Re: BufferedReader with Sockets
              807588
              I did some testing and confirmed that the client was stuck in an infinite loop on readLine() from the server. I fixed the problem by simply adding a method to the server that produces a simple println informing the client that the transfer is done and is now waiting on the client.

              server:
              ...
              String input;
              pout.println("Log in or create a new user.\nUsername:");
              input = userInput();
              ...
              
              public String userInput() throws IOException {
                   pout.println("//USERINPUT//");
                   String str = bin.readLine();
                   if (str.equalsIgnoreCase("/quit")) {
                        System.out.println("//" + player.userName + " logged off//");
                   }
                   return str.toLowerCase();
              }
              client:
              while (true) {
                   fromServer = sin.readLine();
                   while (!fromServer.equals("//USERINPUT//")) {
                        System.out.println(fromServer);
                        fromServer = sin.readLine();
                   }
                        
                   fromUser = stdIn.readLine();
                   while (fromUser == null) {
                        fromUser = stdIn.readLine();
                   }
              
                   sout.println(fromUser);         
                   if (fromUser.equals("/quit")) break;
              }
              While this procedure is something I can tolerate, I was wondering if there was another way of figuring out whether or not the server had finished printing. Right now client waits until the //USERINPUT// line is sent, but it's relatively inconvenient. Are there any other ways of checking this?

              --dorky                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
              • 4. Re: BufferedReader with Sockets
                EJP
                     while (fromUser == null) {
                          fromUser = stdIn.readLine();
                     }
                The problem lies in your use of this incorrect construction. If readLine() returns null, which it will do at the end of the stream (i.e. when the peer closes the connection), this will loop forever. The correct way to write this is thus:
                while ((fromUser = stdIn.readLine()) != null)
                {
                  // 
                } 
                This applies both to the socket connection and to reading from System.in, a file, anything at all.