sessions, fonctionnent (dans certains cas précis, de manière limitée dans le temps).
65 rindas
1,4 KiB
Java
65 rindas
1,4 KiB
Java
package reseau;
|
|
import java.net.*;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.io.*;
|
|
|
|
|
|
|
|
public class TCPServer {
|
|
ServerSocket socket;
|
|
private String host;
|
|
private int port;
|
|
private List<Socket> clients = new ArrayList<Socket>();
|
|
private InetAddress address;
|
|
|
|
public TCPServer (String host, int backlog, int port) {
|
|
this.port = port;
|
|
this.host = host;
|
|
try {
|
|
this.address = InetAddress.getByName(host);
|
|
socket = new ServerSocket(port, backlog, this.address);
|
|
}
|
|
catch (UnknownHostException e) {
|
|
System.out.print("Could not find " + host);
|
|
this.address = null;
|
|
this.socket = null;
|
|
}
|
|
catch (IOException e) {
|
|
System.out.print("Could not create socket");
|
|
this.socket = null;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public Socket accept() {
|
|
Socket ssocket = null;
|
|
try {
|
|
System.out.print("En attente de connexions sur le port "+ this.port +", pour l'adresse " + host + "\n");
|
|
ssocket = this.socket.accept();
|
|
}
|
|
catch (IOException e) {
|
|
System.out.print("Erreur lors de la connexion avec un client");
|
|
}
|
|
return ssocket;
|
|
}
|
|
|
|
public void createThread(Socket ssocket) {
|
|
new TCPServerThread(ssocket, true);
|
|
this.clients.add(ssocket);
|
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
TCPServer server = new TCPServer("localhost", 5, 1999);
|
|
|
|
while(true) {
|
|
Socket ssocket = server.accept();
|
|
server.createThread(ssocket);
|
|
}
|
|
}
|
|
|
|
}
|