Python:
import tkinter as tk
from tkinter import *
import socket
import threading
client = []
global sock
stop_potok = False
SOCKET_LIST = {}
def stop_serv():
global stop_potok
global potok
start_but.config(state = NORMAL)
stop_but.config(state = DISABLED)
stateLabel.config(text='Сервер не работает', fg = 'red', font = 16)
stop_potok = True
sock.close()
def read_sok():
global stop_potok
while 1:
if stop_potok:
print("стоп")
break
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:
start_but.config(state = DISABLED)
stop_but.config(state = NORMAL)
stateLabel.config(text='Сервер работает', 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()
serverWindow.title("Сервер")
serverWindow.geometry("300x400+200+255")
IPFrame = Frame(serverWindow)
PortFrame = Frame(serverWindow)
Label(IPFrame, text="IP: ", font = 15).pack(side = LEFT, pady = 15, padx = 20)
IP = Entry(IPFrame, font={15})
IP.insert(END, '127.0.0.1')
IP.pack(side = RIGHT, pady = 15, padx = 20, fill=X)
Label(PortFrame, text="PORT: ", font = 15).pack(side = LEFT, pady = 15, padx = 20)
port = Entry(PortFrame, font={15})
port.insert(END, '8080')
port.pack(side = RIGHT, pady = 15, padx = 20, fill=X)
IPFrame.pack()
PortFrame.pack()
start_but = Button(serverWindow, text = 'Старт', command = start_serv, font = 15, pady = 15, width = 50)
start_but.pack( pady = 15)
stop_but = Button(serverWindow, text = 'Стоп', command = stop_serv, state=DISABLED, font = 15, pady = 15, width = 50)
stop_but.pack( pady = 15)
stateLabel = Label(serverWindow, pady = 15)
stateLabel.pack(pady = 15)
serverWindow.mainloop()
stop_potok = True
sock.close()