?
Solved

Code stops on readLine() method, doesnt resume!

Posted on 2005-03-17
24
Medium Priority
?
393 Views
Last Modified: 2008-02-26
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
Comment
Question by:Artform04
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 6
  • 4
24 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 1000 total points
ID: 13563296
where are you sending the data?

Are you sending CRLF symbols?

If not, readLine will not return...
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 1000 total points
ID: 13563309
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13563319
:-)
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 37

Expert Comment

by:zzynx
ID: 13563340
Morning ;°)
0
 
LVL 1

Author Comment

by:Artform04
ID: 13563341
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13563363
Shouldn't that have been a  points split? We posted simultanuously.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13563374
>> Cheers for spotting it so fast chaps!
Or did you mean "chap"?
0
 
LVL 1

Author Comment

by:Artform04
ID: 13563397
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13563402
Tim?
0
 
LVL 1

Author Comment

by:Artform04
ID: 13563410
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13563418
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 13563424
Once reopened, you can re-accept
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13563436
?!

So I go from getting the points to getting none?

Nice...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13563441
>> So I go from getting the points to getting none?
None? Who said that?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13563446
Tim, if he had accepted only my comment I would have asked why he didn't split too, you know.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13563460
Tim, do you feel a split as not being fair?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13563475
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13563488
>> led me to believe this Q was going your way...
No, that was not my intention.
>> I'm probably wrong...
:°) Yeah
0
 
LVL 1

Author Comment

by:Artform04
ID: 13563491
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13563510
>> So split?

Seems fair ;-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13563543
Thank you both for your fairness.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13564914
:-)  That's better :-)

Good luck Artform04!!

Tim
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13564939
:)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question