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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
//....
public class ServiceServeur extends Thread{
private ServerSocketChannel serverSocketChannel;
public ServerSocket serverSocket;
private Socket clientServiceSocket;
boolean arret = false;
//....
public ServiceServeur(int port) {
//...
try {
this.serverSocket = new ServerSocket(port);
} catch (IOException ex) {
System.out.println("Impossible de démarrer l'écoute du serveur, port " + port + " occupé.");
}
}
//....
/**
* Démarre l'écoute du serveur
* (cette solution je l'ai trouvé dans le poste que j'ai cité)
*/
public synchronized void run() {
try {
SocketAddress address = new InetSocketAddress(9999);
serverSocketChannel = ServerSocketChannel.open();
serverSocket = serverSocketChannel.socket();
serverSocket.bind(address);
while(!arret) {
try {
this.clientServiceSocket = this.serverSocket.accept();
ServiceClient monService = new ServiceClient(this.clientServiceSocket, this); //je crée un client
monService.start(); //et je le démarre
System.out.println("monService.start");
this.listThread.add(monService);
} catch (ClosedByInterruptException lException) {
arret = true;
System.out.println("Connection au serveur impossible.");
//quand j'arrete le serveur j'avais ce message qui s'affiche en boucle à l'infini
//puis (j'ai rien modifié) maintenant j'ai l'erreur : java.nio.channels.AsynchronousCloseException
}
}
} catch (IOException lException) {
lException.printStackTrace();
} finally {
try {
if (serverSocket!=null) {
serverSocket.close();
}
} catch (Exception lException) {}
try {
if (serverSocketChannel!=null) {
serverSocketChannel.close();
}
} catch (Exception lException) {}
}
}
//... |
Partager