Home  >  Article  >  Backend Development  >  How Python uses UDP to implement client and server communication

How Python uses UDP to implement client and server communication

WBOY
WBOYforward
2023-05-06 22:58:242093browse

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()

In this example, we create a UDP socket and utilize sendto() and recvfrom() 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()

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 the recvfrom() function and send the response back to the client via the sendto() 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-8 Windows: 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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete