Socket
Résolu
abcene
-
faseldi Messages postés 279 Date d'inscription Statut Membre Dernière intervention -
faseldi Messages postés 279 Date d'inscription Statut Membre Dernière intervention -
Bonjour, j'ai ces deux classe:
et MultijabberClient.java:
Maintenant quand je demarre le serveur, il est correct mais pour la classe client j'ai une affichage dece genre:
Server Started
Echoing: Client 0: 0
Echoing: Client 0: 1
Echoing: Client 0: 2
Echoing: Client 0: 3
Echoing: Client 0: 4
Echoing: Client 0: 5
Echoing: Client 0: 6
Echoing: Client 0: 7
Echoing: Client 0: 8
Echoing: Client 0: 9
Echoing: Client 0: 10
Echoing: Client 0: 11
Echoing: Client 0: 12
Echoing: Client 0: 13
Echoing: Client 0: 14
Echoing: Client 0: 15
Echoing: Client 0: 16
Echoing: Client 0: 17
Echoing: Client 0: 18
Echoing: Client 0: 19
Echoing: Client 0: 20
Echoing: Client 0: 21
Echoing: Client 0: 22
Echoing: Client 0: 23
Echoing: Client 0: 24
...
j'ai une boucle infinie...
//MultiJabberServer.java: import java.io.*; import java.net.*; class ServeOneJabber extends Thread { private Socket socket; private BufferedReader in; private PrintWriter out; public ServeOneJabber(Socket s) throws IOException { socket = s; in = new BufferedReader( new InputStreamReader( socket.getInputStream())); // Autoriser l'auto-vidage: out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream())), true); // Si l'un des appels ci-dessus résulte en une // exception, l'appelant a la responsabilité // de fermer socket. Sinon le thread // s'en chargera. start(); // Appelle run() } public void run() { try { while (true) { String str = in.readLine(); if (str.equals("END")) break; System.out.println("Echoing: " + str); out.println(str); } System.out.println("closing..."); } catch(IOException e) { System.err.println("IO Exception"); } finally { try { socket.close(); } catch(IOException e) { System.err.println("Socket not closed"); } } } } public class MultiJabberServer { static final int PORT = 8080; public static void main(String[]args) throws IOException { ServerSocket s = new ServerSocket(PORT); System.out.println("Server Started"); try { while(true) { //On attend ici jusqu'à avoir //une demande de connexion: Socket socket = s.accept(); try { new ServeOneJabber(socket); } catch(IOException e) { // En cas d'échec, fermer l'objet socket, // sinon le thread le fermera: socket.close(); } } } finally { s.close(); } } }
et MultijabberClient.java:
import java.io.*; import java.net.*; class JabberClientThread extends Thread { private Socket socket; private BufferedReader in; private PrintWriter out; private static int counter = 0; private int id = counter++; private static int threadcount = 0; public static int threadCount() { return threadcount; } public JabberClientThread(InetAddress addr) { System.out.println("Making client " + id); threadcount++; try { socket = new Socket(addr, MultiJabberServer.PORT); } catch(IOException e) { System.err.println("Socket failed"); // Si la création de socket échoue, // il n'y a rien à nettoyer. } try { in = new BufferedReader( new InputStreamReader( socket.getInputStream())); // Autoriser l'auto-vidage du tampon: out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream())), true); start(); } catch(IOException e) { // socket doit être fermé sur n'importe quelle // erreur autre que celle de son constructeur: try { socket.close(); } catch(IOException e2) { System.err.println("Socket not closed"); } } // Sinon socket doit être fermé par // la méthode run() du thread. } public void run() { try { for(int i = 0; i < 25; i++) { out.println("Client " + id + ": " + i); String str = in.readLine(); System.out.println(str); } out.println("END"); } catch(IOException e) { System.err.println("IO Exception"); } finally { // Toujours fermer: try { socket.close(); } catch(IOException e) { System.err.println("Socket not closed"); } threadcount--; // Fin de ce thread } } } public class MultiJabberClient { static final int MAX_THREADS = 40; public static void main(String[] args) throws IOException, InterruptedException { InetAddress addr = InetAddress.getByName(null); while(true) { if(JabberClientThread.threadCount() < MAX_THREADS) new JabberClientThread(addr); Thread.currentThread().sleep(100); } } }
Maintenant quand je demarre le serveur, il est correct mais pour la classe client j'ai une affichage dece genre:
Server Started
Echoing: Client 0: 0
Echoing: Client 0: 1
Echoing: Client 0: 2
Echoing: Client 0: 3
Echoing: Client 0: 4
Echoing: Client 0: 5
Echoing: Client 0: 6
Echoing: Client 0: 7
Echoing: Client 0: 8
Echoing: Client 0: 9
Echoing: Client 0: 10
Echoing: Client 0: 11
Echoing: Client 0: 12
Echoing: Client 0: 13
Echoing: Client 0: 14
Echoing: Client 0: 15
Echoing: Client 0: 16
Echoing: Client 0: 17
Echoing: Client 0: 18
Echoing: Client 0: 19
Echoing: Client 0: 20
Echoing: Client 0: 21
Echoing: Client 0: 22
Echoing: Client 0: 23
Echoing: Client 0: 24
...
j'ai une boucle infinie...
EDIT : Ajout des balises de code (la coloration syntaxique).
Explications disponibles ici : ICI Merci d'y penser dans tes prochains messages. |