谷歌大叔想出了用户数据报客户端和服务器
用户数据报客户端和服务器
用户数据报协议 (UDP) 的工作方式与 TCP/IP 不同。TCP 是面向流的协议,确保所有数据都以正确的顺序传输,而 UDP 是面向消息的协议。UDP 不需要长连接,因此设置 UDP 套接字要简单一些。另一方面,UDP 消息必须适合单个数据包(对于 IPv4,这意味着它们只能容纳 65,507 字节,因为 65,535 字节的数据包还包含标头信息),并且无法像 TCP 那样保证传送。
回声服务器
由于没有连接,服务器本身不需要监听和接受连接。它只需要使用 bind() 将其套接字与端口相关联,然后等待各个消息。
import socket
import sys
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind the socket to the port
server_address = ('localhost', 10000)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)
使用 recvfrom() 从套接字读取消息,它返回数据以及发送数据的客户端地址。
while True:
print >>sys.stderr, '\nwaiting to receive message'
data, address = sock.recvfrom(4096)
print >>sys.stderr, 'received %s bytes from %s' % (len(data), address)
print >>sys.stderr, data
if data:
sent = sock.sendto(data, address)
print >>sys.stderr, 'sent %s bytes back to %s' % (sent, address)
回声客户端
UDP 回显客户端类似于服务器,但不使用 bind() 将其套接字附加到地址。它使用 sendto() 将其消息直接传递到服务器,并使用 recvfrom() 接收响应。
import socket
import sys
# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('localhost', 10000)
message = 'This is the message. It will be repeated.'
try:
# Send data
print >>sys.stderr, 'sending "%s"' % message
sent = sock.sendto(message, server_address)
# Receive response
print >>sys.stderr, 'waiting to receive'
data, server = sock.recvfrom(4096)
print >>sys.stderr, 'received "%s"' % data
finally:
print >>sys.stderr, 'closing socket'
sock.close()
客户端和服务器一起
运行服务器会产生:
$ python ./socket_echo_server_dgram.py
starting up on localhost port 10000
waiting to receive message
received 42 bytes from ('127.0.0.1', 50139)
This is the message. It will be repeated.
sent 42 bytes back to ('127.0.0.1', 50139)
waiting to receive message
客户端输出是:
$ python ./socket_echo_client_dgram.py
sending "This is the message. It will be repeated."
waiting to receive
received "This is the message. It will be repeated."
closing socket
$