import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class Serveur { String nomServeur; int nbMaxClient = 3; MySocket tabSocket[] = new MySocket[nbMaxClient]; ServerSocket serversocket; Connection cnx; int nbClient = 0; public Serveur(String nom) { this.nomServeur = nom; try { serversocket = new ServerSocket(9000); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } cnx = new Connection(); cnx.start(); } public void EnvoieTous(String msg) { System.out.println("Envoie du message " + msg); for(int i = 0 ; i < nbClient ; i++) { tabSocket[i].getOut().println(msg); tabSocket[i].getOut().flush(); } } public void DeconnecteTous() { System.out.println("Arret de toutes les sockets"); for(int i = 0 ; i < nbClient ; i++) { System.out.println(tabSocket[i].getSocket()); } } public static void main(String[] args) { Serveur s = new Serveur("serveur num 15651"); System.out.println("ok"); } public class Connection extends Thread { public Connection() { } public void run() { System.out.println("En attente..."); try { while(nbClient < nbMaxClient) { tabSocket[nbClient] = new MySocket(serversocket.accept(), "Client " + nbClient); tabSocket[nbClient].start(); nbClient++; } System.out.println("Plus de connection accepte !"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public class MySocket extends Thread { PrintWriter out; BufferedReader in; Socket socket; String nomMySocket; public MySocket(Socket s,String nom) { System.out.println("init MySocket"); this.nomMySocket = nom; this.socket = s; try { in = new BufferedReader (new InputStreamReader (socket.getInputStream())); out = new PrintWriter(socket.getOutputStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void run() { try { String message_distant; while(!(message_distant = in.readLine()).equals("DeconnexionClient")) { if(this.nomMySocket.equals("Client " + (nbMaxClient-1))) { System.out.println(this.nomMySocket + " peut couper le serveur"); if(message_distant.equals("DeconnexionServeur")) { System.out.print("Arręt serveur"); EnvoieTous("DeconnexionServeur"); serversocket.close(); break; } else { EnvoieTous(this.nomMySocket + "dit : " +message_distant); } } else if(message_distant.equals("DeconnexionServeur")){ EnvoieTous("Pas les droits de couper le serveur !"); } else { EnvoieTous(this.nomMySocket + "dit : " +message_distant); } } if((message_distant.equals("DeconnexionClient"))) { System.out.print("Arręt socket" + this.nomMySocket); out.println("DeconnexionServeur"); out.flush(); sleep(1000); socket.close(); } } catch (InterruptedException | IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String getNom() { return this.nomMySocket; } public PrintWriter getOut() { return this.out; } public Socket getSocket() { return this.socket; } } }