59 lines
1.5 KiB
Java
59 lines
1.5 KiB
Java
package fr.univ.lyon1.client;
|
|
|
|
import java.io.IOException;
|
|
import java.io.ObjectInputStream;
|
|
import java.net.Socket;
|
|
import java.net.SocketException;
|
|
|
|
public class ClientReceive implements Runnable {
|
|
private final Client client;
|
|
private ObjectInputStream in;
|
|
private final Socket socket;
|
|
|
|
public ClientReceive(Client client, Socket socket) {
|
|
this.client = client;
|
|
this.socket = socket;
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
try {
|
|
in = client.getIn();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
return;
|
|
}
|
|
|
|
while(true) {
|
|
Object data;
|
|
try {
|
|
data = in.readObject();
|
|
} catch (ClassNotFoundException|IOException e) {
|
|
if (e instanceof SocketException) {
|
|
System.err.println("Connexion closed");
|
|
break;
|
|
}
|
|
System.err.println("Fail to read object !");
|
|
e.printStackTrace();
|
|
try {
|
|
Thread.sleep(1000);
|
|
} catch (InterruptedException ex) {
|
|
break;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (data == null)
|
|
break;
|
|
|
|
this.client.action(data);
|
|
}
|
|
|
|
try {
|
|
client.disconnectedServer();
|
|
} catch (IOException e) {
|
|
System.err.println("Fail to disconnect !");
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|