Почему socket
не закрывается после закрытия окна? Я не знаю, как закрыть всю программу. stop_thread
не работает должным образом или IDK. Я пытаюсь создать приложение сервера чата с tkinter
, UDP
и threading
.
client = []
global sock
stop_potok = False
def stop_serv():
global stop_potok
global potok
stateLabel.config(text='Сервер не работает', fg = 'red', font = 16)
stop_potok = True
sock.close()
def read_sok():
global stop_potok
while not stop_potok:
data , addres = sock.recvfrom(1024)
print (addres[0], addres[1])
if addres not in client :
client.append(addres)
for clients in client :
if clients == addres :
continue
sock.sendto(data,clients)
sock.close()
def start_serv():
global stop_potok
global sock
global potok
IpADD = IP.get()
PORT = int(port.get())
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((IpADD,PORT))
SOCKET_LIST['Host'] = sock
try:
stateLabel.config(text='Serv works', fg = 'green', font = 16)
except BaseException as excp:
stateLabel.config(text=excp, fg = 'red', font = 16)
stop_potok=False
potok = threading.Thread(target= read_sok)
potok.start()
serverWindow = tk.Tk()
IP = Entry(serverWindow, font={15})
IP.pack(side = RIGHT, pady = 15, padx = 20, fill=X)
port = Entry(serverWindow, font={15})
port.pack(side = RIGHT, pady = 15, padx = 20, fill=X)
Button(serverWindow, text = 'Start', command = start_serv, font = 15, pady = 15, width = 50).pack( pady = 15)
Button(serverWindow, text = 'Stop', command = stop_serv, state=DISABLED, font = 15, pady = 15, width = 50).pack( pady = 15)
stateLabel = Label(serverWindow, pady = 15)
stateLabel.pack(pady = 15)
serverWindow.mainloop()
stop_potok = True
sock.close()