Je dois convertir ce serveur mono-thread en multithread, donc je suis en mesure de traiter une demande multiple d'un serveur:Single Threaded Server dans multithread en JAVA
public class YASGP {
public static void main(String args[]) throws IOException {
ServerSocket server;
try{
server = new ServerSocket(5559);
System.out.println("Listening for connection on port 5559 ....");
while (true) {
Socket clientSocket = server.accept();
InputStreamReader isr = new InputStreamReader(clientSocket.getInputStream());
BufferedReader reader = new BufferedReader(isr);
String line = reader.readLine();
new Thread(new WorkerRunnable(clientSocket)).start();
while (!line.isEmpty()) {
System.out.println("----- " + line);
if (!line.contains("OPTIONS")) {
// System.out.println("Non c'è nulla!!!");
} else {
timeS = line.substring(line.indexOf("timeS=") + 6, line.indexOf("&url"));
url = line.substring(line.indexOf("url=") + 4, line.lastIndexOf("¶m"));
param = line.substring(line.indexOf("¶m=") + 7, line.indexOf("HTTP"));
}
line = reader.readLine();
}
}
}
}catch (IOException e) {
System.out.println("Could not listen on port: 4001");
}
}
private static class RequestHandlingClass {
public RequestHandlingClass(Socket clientSocket) {
}
}
}
Comment puis-je convertir ? Merci à tous
1. Un fil peut être fait pour gérer les connexions clientes mutiple. L'application évoluera mieux si vous ne démarrez pas un nouveau thread pour chaque client. 2. java et javascript sont 2 langues différentes. S'il vous plaît n'en choisir qu'un. –
Le code actuel fonctionne-t-il? C'est étrange comment vous créez un nouveau thread pour le clientSocket. Quel est le but? –
je commence à modifier le code, oui ça marche pour 1 thread, l'autre non. Un conseil? Je suis débutant dans le codage :) – Luke