How Python uses UDP to implement client and server communication

WBOY
Release: 2023-05-06 22:58:24
forward
2000 people have browsed it

UDP Client

A client sample code using the UDP protocol to implement continuous dialogue. Note that UDP is a connectionless protocol, so special care needs to be taken when implementing continuous conversations.

The following is the sample code:

import socket # 客户端配置 HOST = 'localhost' PORT = 12345 # 创建UDP套接字 client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) while True: # 获取用户输入 message = input("请输入要发送的消息:") # 发送消息到服务器 client_socket.sendto(message.encode("utf-8"), (HOST, PORT)) # 注意,在不同操作系统上编码可能不同 # 接收服务器传回的消息 data, server_address = client_socket.recvfrom(1024) print(f"收到来自{server_address}的消息:{data.decode('utf-8')}") # 关闭套接字 client_socket.close()
Copy after login

In this example, we create a UDP socket and utilizesendto()andrecvfrom()Function to send and receive data. The program keeps sending messages via user input and then waits for the server to respond and displays its results. Note that since UDP is a connectionless protocol, message reliability and orderliness cannot be guaranteed. During actual development, you may need to consider these factors and write more robust code.

UDP server

The following is a server-side sample code using the UDP protocol to implement continuous conversations:

import socket # 服务器配置 HOST = 'localhost' PORT = 12345 # 创建UDP套接字 server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 绑定到地址和端口 server_socket.bind((HOST, PORT)) while True: # 接收客户端传来的消息 data, client_address = server_socket.recvfrom(1024) print(f"来自{client_address}的消息:{data.decode('utf-8')}") # 获取用户输入 message = input("请输入要发送的消息:") # 发送消息到客户端 server_socket.sendto(message.encode("utf-8"), client_address) # 关闭套接字 server_socket.close()
Copy after login

In this example, we create a UDP socket and bind it to the specified address and port. We then receive the message from the client via therecvfrom()function and send the response back to the client via thesendto()function. The program continuously receives and sends data through a loop, thus achieving a continuous conversation function.

Please note that since UDP is a connectionless protocol, the reliability and order of messages cannot be guaranteed. During actual development, you may need to consider these factors and write more robust code.

Notes

1. When running the code, the server code must be started first;

2. Pay attention to the decoding and encoding. On different operating systems, The encoding may cause exception messages to be received (Mac:utf-8Windows:gbk).

The above is the detailed content of How Python uses UDP to implement client and server communication. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!