質問編集履歴
2
プログラムを該当箇所に絞りました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -48,13 +48,7 @@
|
|
48
48
|
|
49
49
|
|
50
50
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
51
|
+
|
58
52
|
|
59
53
|
def broadcast_quiz(sock_list_q, msg):
|
60
54
|
|
@@ -66,6 +60,8 @@
|
|
66
60
|
|
67
61
|
for sock in socklist:
|
68
62
|
|
63
|
+
#send_to でソケットに対し、メッセージを送信
|
64
|
+
|
69
65
|
if not send_to(sock,msg):
|
70
66
|
|
71
67
|
sock_list_q.deque(sock)
|
@@ -82,9 +78,9 @@
|
|
82
78
|
|
83
79
|
|
84
80
|
|
85
|
-
quiz_list = ["
|
81
|
+
quiz_list = ["問題1",
|
86
|
-
|
82
|
+
|
87
|
-
"
|
83
|
+
"問題2"]
|
88
84
|
|
89
85
|
broadcast_quiz(sock_list_q, quiz_list[i])
|
90
86
|
|
@@ -184,7 +180,7 @@
|
|
184
180
|
|
185
181
|
|
186
182
|
|
187
|
-
|
183
|
+
|
188
184
|
|
189
185
|
else:
|
190
186
|
|
@@ -216,8 +212,6 @@
|
|
216
212
|
|
217
213
|
sock_list.remove(server_sock)
|
218
214
|
|
219
|
-
broadcast(sock_list, str(sender_port) + ":" + msg)
|
220
|
-
|
221
215
|
sock_list.append(server_sock)
|
222
216
|
|
223
217
|
except:
|
@@ -228,8 +222,6 @@
|
|
228
222
|
|
229
223
|
sock_list.remove(server_sock)
|
230
224
|
|
231
|
-
broadcast(sock_list, "someone disconnected")
|
232
|
-
|
233
225
|
sock_list.append(server_sock)
|
234
226
|
|
235
227
|
|
1
プログラムを該当箇所に絞りました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -50,162 +50,92 @@
|
|
50
50
|
|
51
51
|
|
52
52
|
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
def broadcast_quiz(sock_list_q, msg):
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
socklist = list(deque(sock_list_q))
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
for sock in socklist:
|
68
|
+
|
53
|
-
|
69
|
+
if not send_to(sock,msg):
|
70
|
+
|
71
|
+
sock_list_q.deque(sock)
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
def questions(sock_list_q,i):
|
76
|
+
|
77
|
+
def inner():
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
sock_list_q.get()
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
quiz_list = ["間口が狭く奥行きの広い建物のことを、ある動物に例えて「何の寝床」という?",
|
86
|
+
|
87
|
+
"世界で一番高い山はエベレストですが、日本で一番高い山は何でしょう?"]
|
88
|
+
|
89
|
+
broadcast_quiz(sock_list_q, quiz_list[i])
|
90
|
+
|
91
|
+
return inner
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
def tkinter(sock_list_q):
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
bt = tk.Button(frame, text="問題(" + str(i+1) + ")",height = 3,bg="skyblue",command = questions(sock_list_q,i))#ボタンを押すとクライアントに問題を出力
|
104
|
+
|
105
|
+
buttons.append(bt)
|
106
|
+
|
107
|
+
bt.pack(fill=tk.X)
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
root.mainloop()
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
def socket_communication(sock_list_q):
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
host = 'localhost'
|
122
|
+
|
123
|
+
port = 50001
|
124
|
+
|
125
|
+
backlog = 10
|
126
|
+
|
127
|
+
bufsize = 4096
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
132
|
+
|
133
|
+
print("socket is created")
|
134
|
+
|
135
|
+
|
54
136
|
|
55
137
|
try:
|
56
138
|
|
57
|
-
sock.send(msg.encode())
|
58
|
-
|
59
|
-
return True
|
60
|
-
|
61
|
-
except:
|
62
|
-
|
63
|
-
sock.close()
|
64
|
-
|
65
|
-
return False
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
def broadcast(sock_list, msg):
|
70
|
-
|
71
|
-
for sock in sock_list:
|
72
|
-
|
73
|
-
if not send_to(sock,msg):
|
74
|
-
|
75
|
-
sock_list.remove(sock)
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
def broadcast_quiz(sock_list_q, msg):
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
socklist = list(deque(sock_list_q))
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
for sock in socklist:
|
88
|
-
|
89
|
-
if not send_to(sock,msg):
|
90
|
-
|
91
|
-
sock_list_q.deque(sock)
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
def questions(sock_list_q,i):
|
96
|
-
|
97
|
-
def inner():
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
sock_list_q.get()
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
quiz_list = ["間口が狭く奥行きの広い建物のことを、ある動物に例えて「何の寝床」という?",
|
106
|
-
|
107
|
-
"世界で一番高い山はエベレストですが、日本で一番高い山は何でしょう?"]
|
108
|
-
|
109
|
-
broadcast_quiz(sock_list_q, quiz_list[i])
|
110
|
-
|
111
|
-
return inner
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
def tkinter(sock_list_q):
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
root = tk.Tk()
|
122
|
-
|
123
|
-
root.title("Quiz System")
|
124
|
-
|
125
|
-
root.geometry("400x300")
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
# Canvas Widget を生成
|
130
|
-
|
131
|
-
canvas = tk.Canvas(root)
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
# Scrollbar を生成して配置
|
136
|
-
|
137
|
-
bar = tk.Scrollbar(root, orient=tk.VERTICAL)
|
138
|
-
|
139
|
-
bar.pack(side=tk.RIGHT, fill=tk.Y)
|
140
|
-
|
141
|
-
bar.config(command=canvas.yview)
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
# Canvas Widget を配置
|
146
|
-
|
147
|
-
canvas.config(yscrollcommand=bar.set)
|
148
|
-
|
149
|
-
canvas.config(scrollregion=(0,0,400,500)) #スクロール範囲
|
150
|
-
|
151
|
-
canvas.pack(side=tk.LEFT, fill=tk.BOTH)
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
# Frame Widgetを 生成
|
156
|
-
|
157
|
-
frame = tk.Frame(canvas)
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
# Frame Widgetを Canvas Widget上に配置
|
162
|
-
|
163
|
-
canvas.create_window((0,0), window=frame, anchor=tk.NW, width=canvas.cget('width'))
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
# 複数の Button Widget を生成し、Frame上に配置
|
168
|
-
|
169
|
-
buttons=[]
|
170
|
-
|
171
|
-
for i in range(2):
|
172
|
-
|
173
|
-
bt = tk.Button(frame, text="問題(" + str(i+1) + ")",height = 3,bg="skyblue",command = questions(sock_list_q,i))#ボタンを押すとクライアントに問題を出力
|
174
|
-
|
175
|
-
buttons.append(bt)
|
176
|
-
|
177
|
-
bt.pack(fill=tk.X)
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
root.mainloop()
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
def socket_communication(sock_list_q):
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
host = 'localhost'
|
192
|
-
|
193
|
-
port = 50001
|
194
|
-
|
195
|
-
backlog = 10
|
196
|
-
|
197
|
-
bufsize = 4096
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
202
|
-
|
203
|
-
print("socket is created")
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
try:
|
208
|
-
|
209
139
|
server_sock.bind((host, port))
|
210
140
|
|
211
141
|
print("socket bind")
|
@@ -338,194 +268,10 @@
|
|
338
268
|
|
339
269
|
```
|
340
270
|
|
341
|
-
### quiz_client.py
|
342
|
-
|
343
|
-
```python
|
344
|
-
|
345
|
-
import tkinter as tk
|
346
|
-
|
347
|
-
import socket, select
|
348
|
-
|
349
|
-
import threading
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
def send_msg(ev=None):
|
354
|
-
|
355
|
-
if len(entered_txt.get()) <= 0:
|
356
|
-
|
357
|
-
return
|
358
|
-
|
359
|
-
sock.send(entered_txt.get().encode())
|
360
|
-
|
361
|
-
etr.delete(0,tk.END)
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
def receive_msg(msg):
|
366
|
-
|
367
|
-
if text_w is None:
|
368
|
-
|
369
|
-
return
|
370
|
-
|
371
|
-
text_w.configure(state=tk.NORMAL)
|
372
|
-
|
373
|
-
text_w.insert(tk.END, msg + "\n")
|
374
|
-
|
375
|
-
text_w.configure(state=tk.DISABLED)
|
376
|
-
|
377
|
-
text_w.see(tk.END)
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
def stock_msg(msg):
|
382
|
-
|
383
|
-
stocked_msg.append(msg)
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
def check_msg():
|
390
|
-
|
391
|
-
while len(stocked_msg) > 0:
|
392
|
-
|
393
|
-
receive_msg(stocked_msg.pop(0))
|
394
|
-
|
395
|
-
#after(time,func)
|
396
|
-
|
397
|
-
#funcの実行(呼び出し)をtime[ミリ秒]ごとに行う
|
398
|
-
|
399
|
-
text_w.after(200, check_msg)
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
root = tk.Tk(None)
|
408
|
-
|
409
|
-
root.title("サンプルチャット")
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
frame = tk.Frame(master=root, width=480, height=320)
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
label1 = tk.Label(master=frame, text="サンプルチャット", font=('メイリオ', '12'), bg="#cccccc")
|
418
|
-
|
419
|
-
label1.place(relx=0, rely=0, relwidth=1.0, relheight=0.1)
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
text_w = tk.Text(master=frame, state=tk.DISABLED, font=('メイリオ', '10'), bg="white")
|
424
|
-
|
425
|
-
text_w.place(relx=0.05, rely=0.1, relwidth=0.85, relheight=0.7)
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
sb_y = tk.Scrollbar(master=frame, orient=tk.VERTICAL, command=text_w.yview)
|
430
|
-
|
431
|
-
sb_y.place(relx=0.90, rely=0.1, relwidth=0.05, relheight=0.7)
|
432
|
-
|
433
|
-
text_w.config(yscrollcommand=sb_y.set)
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
entered_txt = tk.StringVar()
|
438
|
-
|
439
|
-
etr = tk.Entry(master=frame, width=30,textvariable = entered_txt)
|
440
|
-
|
441
|
-
etr.bind('<Return>',send_msg)
|
442
|
-
|
443
|
-
etr.place(relx=0.05, rely=0.85, relwidth=0.65, relheight=0.1)
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
bt = tk.Button(master=frame,text="送信",bg="skyblue",command = send_msg)
|
448
|
-
|
449
|
-
bt.place(relx=0.75, rely=0.85, relwidth=0.20, relheight=0.1)
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
frame.pack()
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
host = 'localhost'
|
458
|
-
|
459
|
-
port = 50001
|
460
|
-
|
461
|
-
bufsize = 4096
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
stocked_msg = []
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
def listen():
|
474
|
-
|
475
|
-
try:
|
476
|
-
|
477
|
-
sock.connect((host,port))
|
478
|
-
|
479
|
-
while True:
|
480
|
-
|
481
|
-
r_ready_sockets, w_readysockets, e_readysockets = select.select([sock],[],[])
|
482
|
-
|
483
|
-
try:
|
484
|
-
|
485
|
-
recev_msg = sock.recv(bufsize).decode()
|
486
|
-
|
487
|
-
except:
|
488
|
-
|
489
|
-
break
|
490
|
-
|
491
|
-
stock_msg(recev_msg)
|
492
|
-
|
493
|
-
except Exception as e:
|
494
|
-
|
495
|
-
print(e)
|
496
|
-
|
497
|
-
finally:
|
498
|
-
|
499
|
-
sock.close()
|
500
|
-
|
501
|
-
receive_msg("サーバとの接続が切断されました.")
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
check_msg()
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
thrd = threading.Thread(target=listen)
|
510
|
-
|
511
|
-
thrd.start()
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
root.mainloop()
|
516
|
-
|
517
|
-
```
|
518
|
-
|
519
271
|
|
520
272
|
|
521
273
|
### 補足情報
|
522
274
|
|
523
275
|
現在、チャット機能を拡張しながら実装を行っているため、チャット機能+サーバーのボタンによる問題のチャット欄への出力を目的としています。
|
524
276
|
|
525
|
-
|
526
|
-
|
527
|
-
後に、クライアント側を編集し、問題出力とチャット部分の分離
|
528
|
-
|
529
|
-
チャット機能を正誤判定機能に変更したいと考えています。
|
530
|
-
|
531
277
|
アドバイスいただけると幸いです。
|