separate thread for reading blocking I/O stream
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Serbulent_Cevik
Posted On:   Saturday, May 17, 2003 12:58 AM

How can I write data to a socket stream at random times while reading from the same socket stream! I created a separate thread for blocking-read. So I could send data at random times but I couldn't get the response of the server with the read thread. It blocks forever. Code is something like this: public void keyPressed(KeyEvent e) { if(e.getKeyCode() == 39) // this part is OK out.println("1"); } public void run() { String matrix; boolean loop = true; while ( loop ) { matrix = in.readLine(); // blocks here forever if ( matr   More>>

How can I write data to a socket stream at random times
while reading from the same socket stream!


I created a separate thread for blocking-read. So I could
send data at random times but I couldn't get the
response of the server with the read thread. It blocks
forever. Code is something like this:


public void keyPressed(KeyEvent e) {



if(e.getKeyCode() == 39) // this part is OK

out.println("1");

}



public void run() {



String matrix;

boolean loop = true;



while ( loop ) {

matrix = in.readLine(); // blocks here forever

if ( matrix.charAt(0) == 'D' ) {

loop = false;

break;

}



updateApplet( matrix );



}

}



public void start() {



inputThread = new Thread( this );

inputThread.start();



}



At matrix = in.readLine() line thread blocks forever
although the server sends data. What is wrong?

   <<Less

Re: separate thread for reading blocking I/O stream

Posted By:   Laurent_Mihalkovic  
Posted On:   Sunday, June 8, 2003 09:17 AM

hmmm...



In Java parlance, a line of text ( in.readLine(); ) is considered to be terminated by any one of a linefeed ('
'), a carriage return ('
'), or a carriage return immediatly followed by a linefeed. So unless your data matches these conditions, the readline() is expectedly blocking.

cheers
laurent

About | Sitemap | Contact