Python implements udp chat window

To communicate with each other, there are two ways of udp and tcp. Like our qq, udp and tcp coexist, but now qq is gradually being transformed into a tcp server.

Below is the chat window implemented with udp.

import socket
def send_msg(upd_socket):"""send messages"""
 # Get what to send
 dest_ip =input("Please enter the other party's ip address:")
 dest_port =int(input("Please enter the other party's port number:"))
 send_data =input("Please enter the message to be sent")
 upd_socket.sendto(send_data.encode("utf-8"),(dest_ip, dest_port))
def recv_msg(upd_socket):
 # Receive data and display
 recv_data = upd_socket.recvfrom(1024)print("%s:%s"%(recv_data[0].decode("utf-8"),str(recv_data[1])))
def main():
 # Create socket
 upd_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 # Binding information
 upd_socket.bind("",7788)
 # Loop to process things
 while True:send_msg(upd_socket)recv_msg(upd_socket)if __name__ =="__main__":main()

I recommend a format for writing code, like this, we first build the basic framework

def main():
 pass
 # 1. Create socket
 # 2. Bind local information
 # 3. Know the destination address and port number
 # 4. Receive data and display
 # 5. Close socket
if __name__ =="__main__":main()
  1. This is the basic step. We first conceived it, so we started to write the representative. The code is relatively fixed. What we need to question is whether we use utf-8 or gbk when sending and receiving data. Suppose We are a linux system and the target is a Windows system, so the data we send needs to be encoded with .encode ("gbk"), and when we receive the data, it is decoded with .decode ("gbk") so that it can be correctly accepted Chinese characters.

  2. Then, to make our main program look clearer, we will send and receive messages and wrap them into two functions, which are ** def send_msg(upd_socket): ** and ** def recv_msg(upd_socket): **Note , Whenever we create a new function, we must think about whether this function requires parameters.

  3. You may see that at the end, I did not write udp_socket.close() to close the socket, because we will find at the end that we don't need to call close.

  4. In pyhton, when we use a loop, don't write 1, but write True

For more wonderful articles about python chat function, please click the topic: python chat function summary

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python implements udp chat window
Python implements Super Mario
Python implements tic-tac-toe game
Python implements tic-tac-toe game
Python implements man-machine gobang
Python implements Tetris game
Python implements image stitching
Python implements minesweeper game
Python implements scanning tools
Python implements threshold regression
Python implements minesweeper games
Python implements electronic dictionary
Python implements guessing game
Python implements WeChat airplane game
Python implements word guessing game
Python implements a guessing game
Python implements parking management system
Python implements digital bomb game
Python implements TCP file transfer
Python numpy implements rolling case
OpenCV Python implements puzzle games
Python implements simple tic-tac-toe game
Python implements password strength verification
Python implements car management system
Python implements code block folding
Python implements panoramic image stitching
Python implements SMTP mail sending
Python implements multi-dimensional array sorting
How Python implements FTP function
Python implements mean-shift clustering algorithm
Python implements verification code recognition
Python implements gradient descent method
Python implements text version minesweeper
Python implements image stitching function
Python implements the brick-and-mortar game
Python implements student performance evaluation system
How Python implements the mail function
Python simply implements the snake game
Python3 implements the singleton design pattern
Python implements exchange rate conversion operations
Python implements string and number splicing
Python implements ten classic sorting algorithms
Python implements a universal web framework
Python implements 126 mailboxes to send mail
Python implements AI face change function
Python implements the steepest descent method
Python implements the actual banking system
Python implements digital bomb game program
Python implements ftp file transfer function
Python realizes udp transmission picture function
Python implements username and password verification
How Python implements the timer function
Python implements the aircraft war project
Compare Excel, learn Python window functions
Python implements horizontal stitching of pictures
Python implements GIF graph upside down