Bonjour
Je suis en train de réaliser un petit outil de type client / serveur.
Voici mes class du côté serveur:
class principale Serveur
class C_Accepter_client
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import CONTROLEUR.C_Accepter_client; import java.io.IOException; import java.net.ServerSocket; public class Serveur { public static void main(String[] args) { ServerSocket socketserver ; //Socket socketduserveur ; //BufferedReader in; //PrintWriter out; Thread serveurThread; try { // Lancement du serveur, écoute sur le port 2009 socketserver = new ServerSocket(2009); serveurThread = new Thread(new C_Accepter_client(socketserver)); serveurThread.start(); System.out.println("Lancement du serveur"); } catch (IOException e) {e.printStackTrace();} } }
class C_Chat
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package CONTROLEUR; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class C_Accepter_client implements Runnable { private ServerSocket socketserver; private Socket socket; //private int nbrclient = 1; private Thread threadChat; public C_Accepter_client(ServerSocket s) {this.socketserver = s;} public void run() { try { while(true) { this.socket = this.socketserver.accept(); // Un client se connecte on l'accepte System.out.println("Connexion d'un nouveau client"); this.threadChat = new Thread(new C_Chat(this.socket)); this.threadChat.start(); } } catch (IOException e) {e.printStackTrace();} } }
class C_Chat_Emission
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package CONTROLEUR; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class C_Chat implements Runnable { private Socket socket; private PrintWriter out = null; private BufferedReader in = null; private Thread threadEmission, threadReception; public C_Chat(Socket s) {this.socket = s;} public void run() { try { this.in = new BufferedReader(new InputStreamReader(socket.getInputStream())); this.out = new PrintWriter(socket.getOutputStream()); System.out.println("Lancement du chat pour le nouveau client"); this.threadEmission = new Thread(new C_Chat_Emission(this.out)); this.threadEmission.start(); this.threadReception = new Thread(new C_Chat_Reception(this.in)); this.threadReception.start(); } //catch (IOException e) {e.printStackTrace();} catch (IOException e){System.err.println("s'est déconnecté ");} } }
et class C_Chat_Recepion
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package CONTROLEUR; //import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class C_Chat_Emission implements Runnable { private PrintWriter out; private String message = null; private Scanner sc = null; public C_Chat_Emission(PrintWriter out) { this.out = out; } public void run() { this.sc = new Scanner(System.in); while(true) { System.out.println("Votre message :"); this.message = sc.nextLine(); this.out.println(message); this.out.flush(); } } }
Je ne vous met pas le code côté client mais c'est la même logique...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package CONTROLEUR; import java.io.BufferedReader; import java.io.IOException; public class C_Chat_Reception implements Runnable { private BufferedReader in; private String message = null; public C_Chat_Reception(BufferedReader in) {this.in = in;} public void run() { Boolean active = true; while(active) { try { this.message = this.in.readLine(); System.out.println(this.message); } catch (IOException e) {e.printStackTrace();} } } }
tout fonctionne impec, mes clients et le serveur se parlent sans souci.
Par contre si je coupe un client (CTR+C) mon serveur part en sucette
il m'affiche null en boucle ... Je ne vois pas comment intercepter la fermeture d'un client et stopper la socket dans laquelle il est. Dans chacune de mes boucles (genre dans C_Accepter_client ou C_Chat_Reception j'ai essayé de modifier mon catch pour voir si j'arrivais dedans (un essayant avaec un System.out.println ou autre) mais non...
Merci d'avance pour une aide ou conseil...
Partager