Bonjour tt le monde !
Voilà j'ai un problème pour mettre en place une communication client/serveur qui interagissent entre eux. J'ai essayé donc avec des threads pour la saisie (avec un scanner) et la récupération du msg.
En gros, le problème c'est que un client envoie son msg, et puis il rentre dans une phase "bloquante", il faut que le serveur lui répond une fois pour pouvoir "débloquer" le client.
Voici mes classes
ClientEcho.java
Voici la classe ServerEcho.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import java.io.*; import java.net.*; import java.util.Scanner; public class ClientEcho { public static void main(String[] args) throws IOException { if (args.length != 1) { System.out.println("Usage : java ClientEcho hostName" ); System.exit(1); } String hostName = args[0]; Socket sock = null; PrintWriter sockOut = null; BufferedReader sockIn = null; try { sock = new Socket(hostName, 7777); sockOut = new PrintWriter(sock.getOutputStream(), true); sockIn = new BufferedReader(new InputStreamReader( sock.getInputStream())); } catch (UnknownHostException e) { System.err.println("host non atteignable : "+hostName); System.exit(1); } catch (IOException e) { System.err.println("connection impossible avec : "+hostName); System.exit(1); } System.out.println("tapez q pour terminer"); String recu = ""; Scanner scan = new Scanner(System.in); new Thread(new Runnable(){ public void run(){ try { if(scan.next() != null){ message = scan.next(); sockOut.println(message); } }catch(Exception e){ } } }).start(); new Thread(new Runnable(){ public void run(){ try { while (! message.equals("q")) { sockOut.println(message); try{ recu = sockIn.readLine(); }catch( java.net.SocketException se){ } System.out.println("echo serveur: " + recu.trim()); if (recu == null) { System.out.println("erreur de connection"); break; } } }catch(Exception e){ } } }).start(); sockOut.close(); sockIn.close(); sock.close(); } }
Merci de votre aide
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import java.net.*; import java.io.*; import java.util.Scanner; class ServerEcho { public static void main(String args[]) { ServerSocket server = null; BufferedReader console = null; try { server = new ServerSocket(7777); while (true) { Socket sock = server.accept(); System.out.println("accept"); PrintWriter sockOut = new PrintWriter(sock.getOutputStream(), true); BufferedReader sockIn = new BufferedReader(new InputStreamReader(sock.getInputStream())); String recu; String message=""; Scanner scan = new Scanner(System.in); new Thread(new Runnable(){ public void run(){ try { if(scan.next() != null){ try{ scan.notify(); }catch(java.lang.IllegalMonitorStateException i){ } message = scan.next().toLowerCase(); sockOut.println(message); } } catch(Exception e){ } } }).start(); new Thread(new Runnable(){ public void run(){ try { while ((recu = sockIn.readLine()) != null) { System.out.println("recu client:"+recu); if (recu == null) { System.out.println("erreur de connection"); break; } } } } }).start(); sockOut.close(); sock.close(); } } catch (IOException e) { System.out.println(e.getMessage()); } finally { try { server.close(); } catch (IOException e2) { System.out.println(e2.getMessage()); } } } }
Partager