• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 401
  • Last Modified:

Code stops on readLine() method, doesnt resume!

Hi Experts,

I am building a simple client/server system in Java. In a nutshell, this is what happens:-

Server is loaded and waits for connections
Login Client is loaded, constructor builds GUI with username and password fields, a radio option button group, and a submit button.
Login Client connects to Server & starts a BufferedReader & BufferedWriter.
Login Client then calls a go() function inside the constructor including a readLine() method from the BufferedReader.
On submit, username, password, and label of chosen radio button is sent to the server.
Server authenticates user and sends back message to Login Client.
Login client reads message and depending on content either loads the client application class or refuses connection.

Currently the application is coded to support only one client.

My problem is, the code for the SeatLogin class appears to halt at the readLine() within the go method, and it doesnt move from there. It is within a do {} while(true) loop, which i think is supposed to continue executing. This kind of loop is used both on the Server and the Client Application class, and works just fine within these. However, it does not appear to work in my class.

SeatLogin go() method:-
  public void go() {
    System.out.println("user status is "+userStatus);
    try {
      do {
        System.out.println("No reason why you cant see me");
        String commandLine;
        String commandStr;
        String expectedOperator;

        System.out.println("But then things would make sense then");
        commandLine = br.readLine();
        System.out.println("they probably dont now");

        System.out.println("user status is "+userStatus);
        System.out.println("Command line recieved:"+commandLine);

        StringTokenizer st = new StringTokenizer(commandLine);
        commandStr = st.nextToken();
        expectedOperator = st.nextToken();

        if (commandStr.equals("bye-bye"))
          break;
        else {
          userStatus = Integer.parseInt(commandStr);
        }
        System.out.println(" user status is "+userStatus);

        if (userStatus == 0) {
          System.out.println("im waiting, user status is still "+userStatus);
        }
        if (userStatus == 1) {
          System.out.println("Thunderbirds are go");
          Block[] b = new Block[ 3 ];
          b[ 0 ] = new Block( 7, 7 );
          b[ 1 ] = new Block( 4, 4 );
          b[ 2 ] = new Block( 7, 7 );

          SeatClient s = new SeatClient( b, txtUsername.getText());      // Create.
          s.pack();  //Pack objects
          s.setVisible( true );  //Set GUI visible
          s.go();  //Run the code to interpret Server-sent commands


        }
        else if (userStatus == 2) {
          System.out.println("Thunderbird already here, sorry.");

        }
        else if (userStatus == 3) {
          System.out.println("Security Alert! Unauthorised User");

        }
      }
      while ( true );
      socket.close();
      System.out.println("Client network connection closed.");
      System.exit(1);
    }
    catch (IOException e) {
      System.out.println(e.toString());
      System.exit(1);
    }
  }

SeatClient go() method (very similar):-
  public void go() {
    try {
      do {
        String commandLine;      // From server e.g: 6D RESERVE SUSAN
        String seatStr;            // Seat e.g: 6D
        String commandStr;      // Command e.g: RESERVE
        String operatorStr;      // Operator e.g. SUSAN

        commandLine  = br.readLine();      // Command from server.
        System.out.println(
            systemOperator + " received: " + commandLine );
        StringTokenizer st = new StringTokenizer( commandLine );
        seatStr     = st.nextToken();
        if (seatStr.equals("bye-bye"))
          break;
        commandStr  = st.nextToken();
        operatorStr = st.nextToken();


        // Get button from hashtable and set its colour:
        Button b = (Button)( ht.get( seatStr ) );
        if ( commandStr.equals( "FREE" ) )
          b.setBackground( Color.lightGray );
        else if ( commandStr.equals( "RESERVE" ) ) {
          if ( operatorStr.equals( systemOperator ) )
            b.setBackground( Color.green );
          else
            b.setBackground( Color.red );
        }
        else if ( commandStr.equals( "ALLOCATE" ) )
          b.setBackground( Color.red );
      }

      while ( true );
      socket.close();
      System.out.println("Client network connection closed.");
      System.exit(1);
    }
    catch ( IOException e ) {
      System.out.println( e.toString() );
      System.exit( 1 );
    }
  }

Socket connection code (set up after UI is built elsewhere):-
    try {                  // Connect to server:
      socket = new Socket( "127.0.0.1", 6001 );
      br = new BufferedReader( new InputStreamReader(
          socket.getInputStream() ) );
      dos = new DataOutputStream( socket.getOutputStream() );
    }
    catch ( IOException e ) {
      System.out.println( e.getMessage() );
    }

What am i doing wrong!
0
Artform04
Asked:
Artform04
  • 13
  • 6
  • 4
2 Solutions
 
TimYatesCommented:
where are you sending the data?

Are you sending CRLF symbols?

If not, readLine will not return...
0
 
zzynxSoftware engineerCommented:
That's normal behaviour.

readLine() :
Read a line of text.
A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'),
or a carriage return followed immediately by a linefeed.


As long as none of those is encountered that method() "hangs"/ keeps waiting.
0
 
TimYatesCommented:
:-)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
zzynxSoftware engineerCommented:
Morning ;°)
0
 
Artform04Author Commented:
Typical WriteBytes method:-

dos.writeBytes("bye-bye"+eol);

The WriteBytes i wrote to return my login sucessful code:-

dos.writeBytes(userStatus +" "+ seatStr);

NO EOL!

DOH!

Cheers for spotting it so fast chaps!
0
 
zzynxSoftware engineerCommented:
Shouldn't that have been a  points split? We posted simultanuously.
0
 
zzynxSoftware engineerCommented:
>> Cheers for spotting it so fast chaps!
Or did you mean "chap"?
0
 
Artform04Author Commented:
Umm...if you guys want to split the points, i have no problem with it!

Theres a minute in it, while Tim was faster, zzynx's reply made more sense (and took longer to type!) ;)
0
 
zzynxSoftware engineerCommented:
Tim?
0
 
Artform04Author Commented:
zzynx's reply made more sense (to me) - Ive got a rotten cold and haven't slept yet, it took me a while to realise what CRLF meant ;)

Now how do i call a moderator's attention to change the points...
0
 
zzynxSoftware engineerCommented:
>> Now how do i call a moderator's attention to change the points...
You can reopen this question, by posting a zero-point question in http://www.experts-exchange.com/Community_Support/

Subject: Moderator Please Reopen
Body: Please reopen this question:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21354212.html

0
 
zzynxSoftware engineerCommented:
Once reopened, you can re-accept
0
 
TimYatesCommented:
?!

So I go from getting the points to getting none?

Nice...
0
 
zzynxSoftware engineerCommented:
>> So I go from getting the points to getting none?
None? Who said that?
0
 
zzynxSoftware engineerCommented:
Tim, if he had accepted only my comment I would have asked why he didn't split too, you know.
0
 
zzynxSoftware engineerCommented:
Tim, do you feel a split as not being fair?
0
 
TimYatesCommented:
Split is fair enough...  but:

>>  zzynx's reply made more sense (to me)

followed by

>>  Once reopened, you can re-accept

led me to believe this Q was going your way...

I'm probably wrong...
0
 
zzynxSoftware engineerCommented:
>> led me to believe this Q was going your way...
No, that was not my intention.
>> I'm probably wrong...
:°) Yeah
0
 
Artform04Author Commented:
Technically Tim, you answered the question first, its the acronym that threw me off...

On reading zzynx's post, i understood what the problem was, and understood what you meant by CRLF, so theoretically, you answered the question first, but zzynx made sense of it.

So split?
0
 
TimYatesCommented:
>> So split?

Seems fair ;-)
0
 
zzynxSoftware engineerCommented:
Thank you both for your fairness.
0
 
TimYatesCommented:
:-)  That's better :-)

Good luck Artform04!!

Tim
0
 
zzynxSoftware engineerCommented:
:)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 13
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now