1? 服务器端
代码:
'''GUI MAIN'''
import tkinter
from tkinter import END, messagebox
import os
import time
import threading
import socket
fileKey=""
filePath=""
#server thread
def serverThread():
"""UDP服务器线程函数"""
messagebox.showinfo("notice","thread is beginning")
BUFSIZE = 1024
ip_port = ('127.0.0.1', 9999)
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # udp协议
server.bind(ip_port)
lb.insert("end","begin")
while True:
data,client_addr = server.recvfrom(BUFSIZE)
lb.insert(END,time.strftime("(%H:%M:%S)")+str(client_addr)+":"+str(data)[1:])
lb.see(lb.size())
server.sendto(b'success',client_addr)
server.close()
messagebox.showinfo("tt","thread is over")
#server over
def BeginUDPserver():
"""启动线程"""
exe_thread= threading.Thread(target=serverThread)
exe_thread.setDaemon(True)
exe_thread.start()
b.configure(state=tkinter.DISABLED)
win=tkinter.Tk()
win.option_add("*Font", "宋体")
win.title("UDP服务器")
win.geometry("800x600")
#--------第1行---------------------------------
Lab=tkinter.Label(win,text="信息(UDP服务器端口为9999)")
Lab.grid(row=1,column=1)
#
b=tkinter.Button(win,text="启动服务",command=BeginUDPserver)
b.grid(row=1,column=2)
#---------第2行--------------------------------
sc = tkinter.Scrollbar(win)
sc.grid(row=2,column=2)
lb=tkinter.Listbox(win,width=80,height=28,selectmode="single",yscrollcommand=sc.set)
lb.grid(row=2,column=1)
sc.config(command=lb.yview)
win.mainloop()
2? UDP客户端
代码
'''GUI MAIN'''
import tkinter
from tkinter import messagebox
from tkinter import ttk
import time
import threading
import socket
#按钮消息
def Submit():
"""UDP客户端发送数据"""
MyIP=ServerIP.get()
MyPORT=ServerPort.get()
msg =entry.get()
if len(MyIP.strip())<=0:
messagebox.showerror("提醒","服务器IP不能为空")
return
if len(MyPORT.strip())<=0:
messagebox.showerror("提醒","服务器端口不能为空")
return
if len(msg)<=0:
messagebox.showerror("提醒","发送内容不能为空")
return
BUFSIZE = 1024
client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
ip_port = (MyIP, int(MyPORT))
client.sendto(msg.encode('utf-8'),ip_port)
data,server_addr = client.recvfrom(BUFSIZE)
entry2.delete(0,"end")
entry2.insert("end",str(data)[1:])
#print('客户端recvfrom ',data,server_addr)
client.close()
win=tkinter.Tk()
win.option_add("*Font", "宋体")
win.title("UDP客户端")
win.geometry("800x600")
#--------第1行----------------------------
Lab1=tkinter.Label(win,text="服务器IP")
Lab1.grid(row=1,column=1)
#
ServerIP=tkinter.Entry(width=50)
ServerIP.grid(row=1,column=2)
ServerIP.insert(0,"127.0.0.1")
#--------第2行----------------------------
Lab2=tkinter.Label(win,text="服务器PORT")
Lab2.grid(row=2,column=1)
#
ServerPort=tkinter.Entry(width=50)
ServerPort.grid(row=2,column=2)
ServerPort.insert(0,"9999")
#---------第3行----------------------------
Lab3=tkinter.Label(win,text="发送内容")
Lab3.grid(row=3,column=1)
#
entry=tkinter.Entry(width=50)
entry.grid(row=3,column=2)
#
btsearch=tkinter.Button(win,text="发送数据",command=Submit)
btsearch.grid(row=3,column=3)
#----------第4行---------------------------
Lab4=tkinter.Label(win,text="反馈信息")
Lab4.grid(row=4,column=1)
#
entry2=tkinter.Entry(width=50)
entry2.grid(row=4,column=2)
win.mainloop()
3效果图
? ? ? ?
?
?
|