Link to home
Start Free TrialLog in
Avatar of Artform04
Artform04

asked on

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!
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Morning ;°)
Avatar of Artform04
Artform04

ASKER

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!
Shouldn't that have been a  points split? We posted simultanuously.
>> Cheers for spotting it so fast chaps!
Or did you mean "chap"?
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!) ;)
Tim?
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...
>> 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 https://www.experts-exchange.com/Community_Support/

Subject: Moderator Please Reopen
Body: Please reopen this question:
https://www.experts-exchange.com/questions/21354212/Code-stops-on-readLine-method-doesnt-resume.html

Once reopened, you can re-accept
?!

So I go from getting the points to getting none?

Nice...
>> So I go from getting the points to getting none?
None? Who said that?
Tim, if he had accepted only my comment I would have asked why he didn't split too, you know.
Tim, do you feel a split as not being fair?
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...
>> led me to believe this Q was going your way...
No, that was not my intention.
>> I'm probably wrong...
:°) Yeah
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?
>> So split?

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

Good luck Artform04!!

Tim
:)