Bonjour,
J'écris une communication client-serveur : le client envoie une image au serveur. Ce dernier doit exécuter un fichier python et renvoyer au client une chaîne de caractère. Sauf que j'ai un soucis, le client ne reçoit pas cette chaîne de caractères.
Voici le code du client :
Et voici le code du serveur :
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 public static void main(String[] args) { Socket sc = null; try { sc = new Socket(InetAddress.getLocalHost(),2001); OutputStream oS=sc.getOutputStream(); PrintWriter pw = new PrintWriter(oS); InputStream inS = sc.getInputStream(); DataInputStream dIS = new DataInputStream(inS); pw.println("5"); pw.flush(); byte buf[] = new byte[1024]; FileInputStream in = new FileInputStream("data/starwars.jpg"); int n = in.read(buf); while(n!=-1) { oS.write(buf,0,n); oS.flush(); n=in.read(buf); } in.close(); BufferedReader bf2 = new BufferedReader(new InputStreamReader(inS)); String msg=bf2.readLine(); //programme bloque car ne reçoit jamais le message du serveur System.out.println(msg); } catch(Exception e) { e.printStackTrace(); } finally { try { sc.close(); } catch(Exception e) {} } }
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 @Override public void run() { try { InputStream inS = socket.getInputStream(); OutputStream oS = socket.getOutputStream(); InputStreamReader in = new InputStreamReader(inS); BufferedReader bf = new BufferedReader(in); String message = bf.readLine(); int i = Integer.parseInt(message); Bdd bdd = new Bdd(); DataOutputStream dOS= new DataOutputStream(oS); byte buf[] = new byte[1024]; FileOutputStream fOS = new FileOutputStream("data/image.jpg"); int n = inS.read(buf); while(n!=-1) { fOS.write(buf,0,n); fOS.flush(); n=inS.read(buf); } fOS.close(); dOS.close(); String pythonScriptPath = "data/test.py"; String[] cmd = new String[3]; cmd[0] = "python"; cmd[1] = pythonScriptPath; cmd[2] = "19"; Runtime rt = Runtime.getRuntime(); //exécution script python BufferedReader bf2=null; String msg="0"; Process pr = rt.exec(cmd); if(pr.waitFor()==0) { bf2 = new BufferedReader(new FileReader("data/newfile.txt")); //pour l'instant on récupère ce que renvoie le fichier python dans un fichier texte msg=bf2.readLine(); System.out.println(msg); bf2.close(); } /* J'arrive pas à envoyer pw3 */ PrintWriter pw3 = new PrintWriter(oS); pw3.println(msg); pw3.flush(); pw3.close(); } catch(Exception e) { e.printStackTrace(); } finally { try { sc.close(); } catch(Exception e) {} } }
Les problèmes sont je crois ligne 53 du serveur et donc la ligne 32 du client qui bloque.
Avez vous une idée de pourquoi ça bloque ?
Partager