Based on the knowledge of process and thread learned before, and the principles of network communication tcp and udp, you can write a small program that mimics qq.
1. Operating Interface: (Simple, you can change the style according to your own requirements)
1. Server interface:
2. Client Interface
3. Start the server first: (port number is set by itself)
4. Start client connection to server
Enter information, click and log in
At this point, the host connected to the server can be seen on the console of the server:
5. Restart the two hosts to connect to the server in the same way:
7. Communication can be realized by inputting data in input box and sender.
2. Code implementation:
1. Server:
#192.168.1.103 import tkinter import socket import threading win = tkinter.Tk() win.title("Chat Server") win.geometry("400x100+200+50") users = {} def run(ck,ca): print("*********************") UserName = ck.recv(1024) users[UserName.decode("utf-8")] = ck print(users) while True: rData = ck.recv(1024) dataStr = rData.decode("utf-8") #dx:hello infolist = dataStr.split(":") users[infolist[0]].send((UserName.decode("utf-8")+"say:"+infolist[1]).encode("utf-8")) def start(): ipStr = eip.get() portStr = eport.get() # Create a socket server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Binding IP and Port server.bind((ipStr, int(portStr))) # Monitor server.listen(10) printStr ="Server Start Successfully" text.insert(tkinter.INSERT, printStr) while True: # Waiting for links ck, ca = server.accept() # Print ("% s -% s link succeeded" (str (client Socket), client Address)) t = threading.Thread(target=run, args=(ck, ca)) t.start() def startServer(): s = threading.Thread(target=start) s.start() labelIP = tkinter.Label(win,text="ip" ).grid(row=0,column=0) labelPort = tkinter.Label(win,text="port" ).grid(row=1,column=0) eip = tkinter.Variable() eport = tkinter.Variable() entryIP = tkinter.Entry(win,textvariable=eip).grid(row=0,column=1) entryPort = tkinter.Entry(win,textvariable=eport).grid(row=1,column=1) button = tkinter.Button(win,text="start-up",command=startServer).grid(row=4,column=1) text = tkinter.Text(win,width=30,height=1) text.grid(row=5,column=0) win.mainloop()
2. Client:
import tkinter import socket import threading win = tkinter.Tk() win.title("forms") win.geometry("400x400+200+50") ck = None def getInfo(): while True: data = ck.recv(1024) text.insert(tkinter.INSERT,data.decode("utf-8")) #Sign in def login(): global ck #Get ip ipStr = eIP.get() #Get the port value portStr = eport.get() #Getting the Send Content userStr = euser.get() #Connecting servers client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((ipStr, int(portStr))) client.send(userStr.encode('utf-8')) ck = client #Waiting to receive data t = threading.Thread(target=getInfo) text.insert(tkinter.INSERT,userStr+"Enter the chat room") t.start() #Send message def sendMessage(): friend = efriend.get() sendStr = esend.get() sendStr = friend + ":" +sendStr ck.send(sendStr.encode("utf-8")) labelUser = tkinter.Label(win,text="UserName" ).grid(row=0,column=0) euser = tkinter.Variable() entryUser = tkinter.Entry(win,textvariable=euser).grid(row=0,column=1) labelIP = tkinter.Label(win,text="ip" ).grid(row=1,column=0) eIP = tkinter.Variable() entryIP = tkinter.Entry(win,textvariable=eIP).grid(row=1,column=1) labelPort = tkinter.Label(win,text="port" ).grid(row=2,column=0) eport = tkinter.Variable() entryPort = tkinter.Entry(win,textvariable=eport).grid(row=2,column=1) button = tkinter.Button(win,text="Sign in",command=login).grid(row=3,column=1) text = tkinter.Text(win,width=30,height=5) text.grid(row=4,column=0) labelsend= tkinter.Label(win,text="Please enter the sending content" ).grid(row=6,column=0) esend = tkinter.Variable() entrySend = tkinter.Entry(win,textvariable=esend).grid(row=6,column=1) labelfriend = tkinter.Label(win,text="Please enter the sending address" ).grid(row=7,column=0) efriend= tkinter.Variable() entryFriend = tkinter.Entry(win,textvariable=efriend).grid(row=7,column=1) button2 = tkinter.Button(win,text="Send out",command=sendMessage).grid(row=8,column=1) win.mainloop()