Bonjour,
Je n'arrive pas à fermer un Thread à la fermeture de l'application, le problème c'est qu'il bloque sur le serveur socket avec la méthode accept.
J'ai beau fermer le serverSocket rien n'y fait, impossible de stopper le Thread.
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199 import java.io.*; import java.net.*; import java.util.logging.Level; import java.util.logging.Logger; import config.FileProperties; //import org.apache.xmlrpc.client.XmlRpcClient; //import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; import cle.Liberer; import de.timroes.axmlrpc.XMLRPCClient; import de.timroes.axmlrpc.XMLRPCException; import fullscreen.PrendreCle2; public class Serveur2 extends Thread { static final int port = 4080; private Thread tempo; protected volatile boolean running = true; private BufferedReader plec; private PrintWriter pred; private InputStreamReader isr; // private Thread thread; private ServerSocket serverSocket; private Socket soc; private PrendreCle2 prendreCle; public Serveur2(PrendreCle2 prendreCle) { super(); try { serverSocket = new ServerSocket(port); //serverSocket.setSoTimeout(0); } catch (IOException ioe) { System.out.println(ioe); } this.prendreCle = prendreCle; } public void run() { try { soc = serverSocket.accept(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } while (running) { try { System.out.println("0"); System.out.println("1"); isr = new InputStreamReader( soc.getInputStream()); System.out.println("1.1"); plec = new BufferedReader(isr); System.out.println("2"); String str = plec.readLine(); // lecture du message System.out.println("3"); System.out.println("CHAINE :: " + str); if (str != null) { if (str.contains("sessid=") && str.contains("key=")) { String sessid = "sessid="; String eperluete = "&"; String valueSessionID = str.substring( str.indexOf(sessid) + sessid.length(), str.indexOf(eperluete)); System.out.println("SESSION ID :: " + valueSessionID); String key = "key="; String valueKey = str.substring( str.indexOf(key) + key.length(), str.lastIndexOf(key) + key.length() + 1); int _key = Integer.parseInt(valueKey); String resource = "resource="; int index = str.indexOf(resource) + resource.length(); int copieIndex = index; copieIndex += 1; boolean fait = true; while (fait) { try { ; String chaine = str .substring(index, copieIndex); Integer.parseInt(chaine); copieIndex++; } catch (NumberFormatException nfe) { fait = false; } } String valueResource = str.substring(index, copieIndex - 1); // System.out.println("KEY INTEGER : "+_key); // System.out.println("CLE :: "+valueKey); if (MAJBoiteAcle.checkCommand(_key, valueSessionID) == 1) { Liberer.setSessionID(valueSessionID); Liberer.setCle(_key); Liberer.deverouiller(); Liberer.setAuthentifierOpenFlyers(true); Liberer.setResource(valueResource); // affichage de la prise de cle. prendreCle.setRFID(); System.out .println("[Openflyers - Serveur] SessionID = " + Liberer.getSessionID()); System.out.println("[Openflyers - Serveur] Cle = " + Liberer.getCle()); System.out .println("[Openflyers - Serveur] Verouiller = " + Liberer.estVerouiller()); //tempo = new Thread(new Temporisation()); //tempo.start(); } } // fin if } // fin if } catch (IOException ioe) { System.out.println("[Openflyers Serveur] " + ioe); try { serverSocket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }// fin while System.out.println("INTERUPTION"); } public synchronized void stopTempo() { tempo.interrupt(); } public void arret() { //tempo.interrupt(); close(); try { isr.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { soc.close(); System.out.println("CLOSE CONNECTION"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } running = false; // if(this.isInterrupted()) { // System.out.println("INTERROMPU"); // } else { // this.interrupt(); // } } public void close() { try { soc.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { serverSocket.close(); } catch (IOException ioe) { System.out.println(ioe); } } }
Partager