java - BufferedReader is not blocking in readLine -


i'm creating networked game in java using sockets api, , got following problem:

when app starts server socket starts listening , spawns new thread each new client (basic stuff, doesn't need show code).

then client starts conversation, sending string "login". server receives , responds "login_ok". conversation over, console keeps printing login_ok (which happens on client).

why happens? readline() not supposed block? clarification!

edit: problem in client loop. client sending server's response server! commented out line solved it!

server loop

public void run()     {         string fromclient = null;          try {             while ((fromclient = in.readline()) != null) {                 system.out.println(fromclient);                 string response = sessionprotocol.handleinput(fromclient);                  out.println(response);                 thread.sleep(50l);             }          } catch(ioexception ex){ex.printstacktrace();}             catch(interruptedexception ex){ex.printstacktrace();}     } 

client loop

public void run()     {         string fromserver = null;          try {             while ((fromserver = in.readline()) != null) {                 system.out.println(fromserver);                 string response = sessionprotocol.handleinput(fromserver);                  // next line causes problem                 //out.println(response);                 thread.sleep(50l);             }          } catch(ioexception ex){ex.printstacktrace();}             catch(interruptedexception ex){ex.printstacktrace();}     } 

yes, readline() blocking.

your in.readline() reads until next \n character, regardless of if there's text before or not. since you're calling out.println(), you're appending \n output, regardless of if response empty or not.

thus, each iteration of loop on both client , server receive newline , output newline, why seems it's not blocking.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -