SocketTimeOutException vs IOEXCEPTION sur une operation read()
Bonjour,
J'ai un serveur qui crée un thread aussitôt qu'il y a un client connecte, j’ai ajouté un TimeOut sur le socket pour débloquer la fonction read(buf,0 BUFFER.SIZE).
L’opération read se fait comme il faut et j'arrive à traiter la requête du client. Quand le socket atteint le TimeOut de la socket, IOEXCEPTION est capture au lieu de SocketTimeOut.
voilà un extrait du code.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public static void main(String[] args)
{
try {
InetAddress address = InetAddress.getLocalHost();
int port = 10555;
ServerSocket ss = new ServerSocket(port, 16, address);
while (true) {
Socket s = ss.accept();
s.setSoTimeout(2000);
System.out
.println("--------------------------------------------------------------------------------");
System.out.println("Accepted " + s);
ServerThread t = new ServerThread(s);
t.start();
}
catch (IOException e)
{
System.out.println("cought exception " );
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
static class ServerThread extends Thread { private final Socket s;
ServerThread(Socket s)
{
this.s = s;
}
public void run()
{
int read = -1;
try {
byte[] buf = new byte[8192];
read = inputstream.read(buf, 0, 8192);
}catch (SocketTimeoutException e) {
System.out.println("time out excception");
}
catch (InterruptedIOException e) {
System.out.println("time out excception");
}
catch (IOException e) {
System.out.println("io excception");
}
catch (Exception e) {
System.out.println("generic excception");
}
if (read == -1) {
// socket was been closed
System.out.println("socket closed");
}
finally
{
s.close();
}
}
} |
Question 1 : pourquoi IOEXCEPTION est soulevé
QUESTION 2 : je sais qu'après le time out le socket reste valide, comment je pourrais le réutiliser ?
Merci a l'avance