views:

40

answers:

2

Im trying to run a thread that goes to a socket, grabs the input stream, and reads it. Im creating hundreds of these threads and have set the timeout for reading and yet the thread still stays at the read() line.

public void readPack() {

socket.setSoTimeout(4*1000);

if (socket.isConnected()) {


     buffer parse = new buffer();
     parse.addByte((byte) skt.getInputStream().read());
     parseIncoming(parse);
}


} catch (Exception e) {}

}
A: 

Call skt.available(), and then call read that many times, or use skt.read(byte[]). Other wise skt.read() will block. The timeout your setting is to connect to the socket, and not a read timeout.

jex
The timeout he is setting is a read timeout, not a connect timeout.
EJP
+1  A: 

Strange code. You create a buffer, read one byte into it, then parse that byte, then repeat the whole process. One byte surely doesn't take much parsing. You are never checking for -1 from the read so this loop will spin endlessly when the peer disconnects. And finally Socket.isConnected() isn't a useful test, and specifically it doesn't detect the peer disconnecting.

EJP
the while is suppose to be an if, just trying to pick up one byte
koolaid2345
It's still wrong, see above.
EJP