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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Serveur1 {
public static void main(String[] zero) {
ServerSocket socketserver1;
ServerSocket socketserver2;
ServerSocket[] socketserver_t;
Socket socket1 = null;
Socket socket2 = null;
Socket socket_t[];
int nombre_client = 0;
try {
socketserver1 = new ServerSocket(2009);
socketserver2 = new ServerSocket(2010);
socketserver_t = new ServerSocket[4];
socket_t = new Socket[4];
socketserver_t[0] = socketserver1;
socketserver_t[1] = socketserver2;
Thread t1 = new Thread(new declencher_client(socketserver_t,0,socket_t,nombre_client));
Thread t2 = new Thread(new declencher_client(socketserver_t,1,socket_t,nombre_client));
t1.start();
t2.start();
System.out.println("attente");
while (true)
{
}
} catch (IOException ex) {
Logger.getLogger(Serveur1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
class declencher_client implements Runnable {
int nombre_client;
private ServerSocket socketserver;
private int numero_client;
Socket[] socket_t;
public declencher_client(ServerSocket s[],int n, Socket[] socket_t, int nombre_client){
numero_client = n;
socketserver = s[n];
this.socket_t = socket_t;
this.nombre_client = nombre_client;
}
public void run() {
try {
while (true){
System.out.println("Le serveur est à l'écoute du port "+socketserver.getLocalPort());
socket_t[numero_client] = socketserver.accept();
System.out.println("le client numéro"+numero_client+"s'est connecté");
BufferedReader in[] = new BufferedReader[nombre_client];
PrintWriter out[] = new PrintWriter[nombre_client];
BufferedReader in1;
PrintWriter out1 = null;
String message = new String();
for (int i =0;i<nombre_client;i++){
in[i] = new BufferedReader (new InputStreamReader (socket_t[i].getInputStream()));
out[i] = new PrintWriter(socket_t[i].getOutputStream());
}
String message_distant;
while (true){
message_distant = in[0].readLine();
System.out.println(message_distant);
out[0].println(message_distant);
out[0].flush();
}
}
/*if (message_distant.compareTo("deconn")==0){
out.print("deconn");
out.flush();
socket.close();
System.out.println("déconnection de serveur");
socketserver.close();
}*/
}
catch (IOException e)
{
e.printStackTrace();
}
}
} |
Partager