質問編集履歴
8
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -218,7 +218,7 @@
|
|
218
218
|
|
219
219
|
# 追記
|
220
220
|
|
221
|
-
try-except追加、daemon化
|
221
|
+
try-except追加、daemon化、二重ループに再変更
|
222
222
|
|
223
223
|
```Python
|
224
224
|
|
@@ -334,9 +334,7 @@
|
|
334
334
|
|
335
335
|
self.s.listen(1)
|
336
336
|
|
337
|
-
|
337
|
+
|
338
|
-
|
339
|
-
# https: // techacademy.jp/magazine/19147
|
340
338
|
|
341
339
|
while loop_flag:
|
342
340
|
|
@@ -346,15 +344,15 @@
|
|
346
344
|
|
347
345
|
print(con, adr)
|
348
346
|
|
349
|
-
|
347
|
+
while loop_flag:
|
350
|
-
|
348
|
+
|
351
|
-
data = con.recv(1024)
|
349
|
+
data = con.recv(1024)
|
352
|
-
|
350
|
+
|
353
|
-
print(data)
|
351
|
+
print(data)
|
354
|
-
|
352
|
+
|
355
|
-
|
353
|
+
if not data:
|
356
|
-
|
354
|
+
|
357
|
-
|
355
|
+
break
|
358
356
|
|
359
357
|
print("recv")
|
360
358
|
|
@@ -374,4 +372,6 @@
|
|
374
372
|
|
375
373
|
ctrlr = Controller()
|
376
374
|
|
375
|
+
|
376
|
+
|
377
377
|
```
|
7
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -366,8 +366,6 @@
|
|
366
366
|
|
367
367
|
print(e)
|
368
368
|
|
369
|
-
return
|
370
|
-
|
371
369
|
|
372
370
|
|
373
371
|
|
6
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -218,7 +218,7 @@
|
|
218
218
|
|
219
219
|
# 追記
|
220
220
|
|
221
|
-
c
|
221
|
+
try-except追加、daemon化
|
222
222
|
|
223
223
|
```Python
|
224
224
|
|
@@ -252,7 +252,7 @@
|
|
252
252
|
|
253
253
|
def main(self):
|
254
254
|
|
255
|
-
self.thread = threading.Thread(target=self.loop)
|
255
|
+
self.thread = threading.Thread(target=self.loop, daemon=True)
|
256
256
|
|
257
257
|
self.thread.start()
|
258
258
|
|
@@ -286,7 +286,7 @@
|
|
286
286
|
|
287
287
|
self.svr.s.close()
|
288
288
|
|
289
|
-
self.thread.join()
|
289
|
+
#self.thread.join()
|
290
290
|
|
291
291
|
self.win.destroy()
|
292
292
|
|
@@ -334,24 +334,40 @@
|
|
334
334
|
|
335
335
|
self.s.listen(1)
|
336
336
|
|
337
|
+
# 参考
|
338
|
+
|
339
|
+
# https: // techacademy.jp/magazine/19147
|
340
|
+
|
337
|
-
while
|
341
|
+
while loop_flag:
|
342
|
+
|
338
|
-
|
343
|
+
try:
|
344
|
+
|
339
|
-
con, adr = self.s.accept()
|
345
|
+
con, adr = self.s.accept()
|
340
|
-
|
346
|
+
|
341
|
-
print(con, adr)
|
347
|
+
print(con, adr)
|
342
|
-
|
348
|
+
|
343
|
-
while loop_flag:
|
349
|
+
#while loop_flag:
|
344
350
|
|
345
351
|
data = con.recv(1024)
|
346
352
|
|
353
|
+
print(data)
|
354
|
+
|
347
|
-
if not data:
|
355
|
+
# if not data:
|
348
|
-
|
356
|
+
|
349
|
-
break
|
357
|
+
# break
|
350
358
|
|
351
359
|
print("recv")
|
352
360
|
|
353
361
|
rcv_data.put()
|
354
362
|
|
363
|
+
except Exception as e:
|
364
|
+
|
365
|
+
print(type(e))
|
366
|
+
|
367
|
+
print(e)
|
368
|
+
|
369
|
+
return
|
370
|
+
|
355
371
|
|
356
372
|
|
357
373
|
|
5
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -218,7 +218,7 @@
|
|
218
218
|
|
219
219
|
# 追記
|
220
220
|
|
221
|
-
|
221
|
+
close使うように変更
|
222
222
|
|
223
223
|
```Python
|
224
224
|
|
@@ -238,6 +238,8 @@
|
|
238
238
|
|
239
239
|
|
240
240
|
|
241
|
+
|
242
|
+
|
241
243
|
class Controller:
|
242
244
|
|
243
245
|
def __init__(self):
|
@@ -270,6 +272,8 @@
|
|
270
272
|
|
271
273
|
self.svr.start(self.rcv_data)
|
272
274
|
|
275
|
+
|
276
|
+
|
273
277
|
def close(self):
|
274
278
|
|
275
279
|
global loop_flag
|
@@ -280,12 +284,16 @@
|
|
280
284
|
|
281
285
|
|
282
286
|
|
287
|
+
self.svr.s.close()
|
288
|
+
|
283
289
|
self.thread.join()
|
284
290
|
|
285
291
|
self.win.destroy()
|
286
292
|
|
287
293
|
|
288
294
|
|
295
|
+
|
296
|
+
|
289
297
|
class View:
|
290
298
|
|
291
299
|
def __init__(self, win):
|
@@ -300,6 +308,10 @@
|
|
300
308
|
|
301
309
|
# https://techacademy.jp/magazine/19147
|
302
310
|
|
311
|
+
|
312
|
+
|
313
|
+
|
314
|
+
|
303
315
|
class Server:
|
304
316
|
|
305
317
|
def __init__(self, rcv_data):
|
@@ -316,29 +328,29 @@
|
|
316
328
|
|
317
329
|
global loop_flag
|
318
330
|
|
319
|
-
|
331
|
+
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
320
|
-
|
332
|
+
|
321
|
-
|
333
|
+
self.s.bind((self.ip_adr, self.port))
|
322
|
-
|
334
|
+
|
323
|
-
|
335
|
+
self.s.listen(1)
|
324
|
-
|
336
|
+
|
325
|
-
|
337
|
+
while True:
|
326
|
-
|
338
|
+
|
327
|
-
|
339
|
+
con, adr = self.s.accept()
|
328
|
-
|
340
|
+
|
329
|
-
|
341
|
+
print(con, adr)
|
330
|
-
|
342
|
+
|
331
|
-
|
343
|
+
while loop_flag:
|
332
|
-
|
344
|
+
|
333
|
-
|
345
|
+
data = con.recv(1024)
|
334
|
-
|
346
|
+
|
335
|
-
|
347
|
+
if not data:
|
336
|
-
|
348
|
+
|
337
|
-
|
349
|
+
break
|
338
|
-
|
350
|
+
|
339
|
-
|
351
|
+
print("recv")
|
340
|
-
|
352
|
+
|
341
|
-
|
353
|
+
rcv_data.put()
|
342
354
|
|
343
355
|
|
344
356
|
|
4
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -218,7 +218,7 @@
|
|
218
218
|
|
219
219
|
# 追記
|
220
220
|
|
221
|
-
フラグをグローバル変数に変更、
|
221
|
+
フラグをグローバル変数に変更、サーバのstart()呼び出し位置変更
|
222
222
|
|
223
223
|
```Python
|
224
224
|
|
@@ -252,6 +252,8 @@
|
|
252
252
|
|
253
253
|
self.thread = threading.Thread(target=self.loop)
|
254
254
|
|
255
|
+
self.thread.start()
|
256
|
+
|
255
257
|
self.win = tk.Tk()
|
256
258
|
|
257
259
|
self.view = View(self.win)
|
@@ -266,7 +268,7 @@
|
|
266
268
|
|
267
269
|
self.svr = Server(self.rcv_data)
|
268
270
|
|
269
|
-
self.svr.start()
|
271
|
+
self.svr.start(self.rcv_data)
|
270
272
|
|
271
273
|
def close(self):
|
272
274
|
|
3
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -266,7 +266,7 @@
|
|
266
266
|
|
267
267
|
self.svr = Server(self.rcv_data)
|
268
268
|
|
269
|
-
self.
|
269
|
+
self.svr.start()
|
270
270
|
|
271
271
|
def close(self):
|
272
272
|
|
2
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -308,8 +308,6 @@
|
|
308
308
|
|
309
309
|
self.buffer_size = 1024
|
310
310
|
|
311
|
-
self.start(rcv_data)
|
312
|
-
|
313
311
|
|
314
312
|
|
315
313
|
def start(self, rcv_data):
|
1
追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -213,3 +213,139 @@
|
|
213
213
|
AttributeError: 'Controller' object has no attribute 'svr'
|
214
214
|
|
215
215
|
```
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
# 追記
|
220
|
+
|
221
|
+
フラグをグローバル変数に変更、スレッドのスタート位置を変更
|
222
|
+
|
223
|
+
```Python
|
224
|
+
|
225
|
+
from tkinter import ttk
|
226
|
+
|
227
|
+
import queue
|
228
|
+
|
229
|
+
import socket
|
230
|
+
|
231
|
+
import threading
|
232
|
+
|
233
|
+
import tkinter as tk
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
loop_flag = True
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
class Controller:
|
242
|
+
|
243
|
+
def __init__(self):
|
244
|
+
|
245
|
+
self.rcv_data = queue.Queue()
|
246
|
+
|
247
|
+
self.main()
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
def main(self):
|
252
|
+
|
253
|
+
self.thread = threading.Thread(target=self.loop)
|
254
|
+
|
255
|
+
self.win = tk.Tk()
|
256
|
+
|
257
|
+
self.view = View(self.win)
|
258
|
+
|
259
|
+
self.win.protocol("WM_DELETE_WINDOW", self.close)
|
260
|
+
|
261
|
+
self.win.mainloop()
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
def loop(self):
|
266
|
+
|
267
|
+
self.svr = Server(self.rcv_data)
|
268
|
+
|
269
|
+
self.thread.start()
|
270
|
+
|
271
|
+
def close(self):
|
272
|
+
|
273
|
+
global loop_flag
|
274
|
+
|
275
|
+
# サーバ切断したい
|
276
|
+
|
277
|
+
loop_flag = False
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
self.thread.join()
|
282
|
+
|
283
|
+
self.win.destroy()
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
class View:
|
288
|
+
|
289
|
+
def __init__(self, win):
|
290
|
+
|
291
|
+
self.win = win
|
292
|
+
|
293
|
+
self.win.geometry("400x300")
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
# ソケット通信参考
|
298
|
+
|
299
|
+
# https://techacademy.jp/magazine/19147
|
300
|
+
|
301
|
+
class Server:
|
302
|
+
|
303
|
+
def __init__(self, rcv_data):
|
304
|
+
|
305
|
+
self.ip_adr = "127.0.0.1"
|
306
|
+
|
307
|
+
self.port = 50000
|
308
|
+
|
309
|
+
self.buffer_size = 1024
|
310
|
+
|
311
|
+
self.start(rcv_data)
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
def start(self, rcv_data):
|
316
|
+
|
317
|
+
global loop_flag
|
318
|
+
|
319
|
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
320
|
+
|
321
|
+
s.bind((self.ip_adr, self.port))
|
322
|
+
|
323
|
+
s.listen(1)
|
324
|
+
|
325
|
+
while True:
|
326
|
+
|
327
|
+
con, adr = s.accept()
|
328
|
+
|
329
|
+
print(con, adr)
|
330
|
+
|
331
|
+
while loop_flag:
|
332
|
+
|
333
|
+
data = con.recv(1024)
|
334
|
+
|
335
|
+
if not data:
|
336
|
+
|
337
|
+
break
|
338
|
+
|
339
|
+
print("recv")
|
340
|
+
|
341
|
+
rcv_data.put()
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
if __name__ == "__main__":
|
348
|
+
|
349
|
+
ctrlr = Controller()
|
350
|
+
|
351
|
+
```
|