Bonjour,
Je suis novice en programmation de socket et pour un besoin au boulot entre un as400 et une appli pc en java je dois dévolopper des sockets.
Pour le moment j'ai fais un test en local avec java sur win7. J'ai trouvé un tuto simple en deux classe qui communique (qui devrait) entre elle.
Le problème sur situe sur la classse cliente qui attend indéfiniment sur un redLine alors que la classe serveur a envoyé plusieurs données.
voici les deux classes:
Il ne semble pas que le firewall interdise le port 4001.
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 //Classe serveur. package SocketSrv2; import java.net.*; import java.io.*; public class Srv2 { public static void main(String[] args) { int port = 4001; ServerSocket socketEcoute; try { socketEcoute = new ServerSocket(port); doService(socketEcoute.accept()); } catch(IOException ioe) { ioe.printStackTrace(); } } static void doService(Socket clientSocket) { BufferedReader in; PrintWriter out; try { in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); out = new PrintWriter(clientSocket.getOutputStream()); System.out.println("Client connecté..."); System.out.println("SR Ecriture 1 ..."); out.println("Bonjour..."); System.out.println("SR Ecriture 2 ..."); out.println("Comment ça va ?"); System.out.println("SR Lecture 1 ..."); String ligne = in.readLine(); System.out.println("<" + ligne + ">"); System.out.println("SR Ecriture 3 ..."); out.println(ligne); } catch(IOException ioe) { ioe.printStackTrace(); } } } //Classe cliente. package SocketCli; import java.net.*; import java.io.*; public class Cli2 { public static void main(String[] args) { String ligne=null; int port = 4001; BufferedReader in=null; PrintWriter out=null; Socket maSocket=null; try { maSocket = new Socket("127.0.0.1", port); in = new BufferedReader( new InputStreamReader(maSocket.getInputStream()) ); out = new PrintWriter(maSocket.getOutputStream()); System.out.println("Cl Lecture 1..."); ligne = in.readLine(); //<--- Blocage !!! System.out.println(ligne); System.out.println("CL Lecture 2..."); ligne = in.readLine(); System.out.println(ligne); System.out.println("CL Ecriture 1..."); out.write("quit\n"); System.out.println("CL Lecture 3..."); ligne = in.readLine(); System.out.println(ligne); } catch(UnknownHostException uhe) { uhe.printStackTrace(); } catch(IOException ioe) { ioe.printStackTrace(); } } }
Je ne vois pas bien d'où vien le souci.
Pourriez vous svp jeter un oeil sur le code j'ai peut-être omi quelque chose.
Par avance merci.
Partager