服务端流程
sockfd = socket(AF_INET,SOCK_DGRAM)
sockfd.bind(addr)
data,addr = sockfd.recvfrom(buffersize)
n = sockfd.sendto(data,addr)
4.关闭套接字
示例
from socket import *
sockfd = socket(AF_INET,SOCK_DGRAM)
server_addr = ('0.0.0.0',8888)
sockfd.bind(server_addr)
while True:
data,addr = sockfd.recvfrom(1024)
print('Msg from %s:%s' %(addr,data.decode()))
sockfd.sendto(b'Thanks',addr)
sockfd.close()
from socket import *
ADDR = ('127.0.0.1',8888)
sockfd = socket(AF_INET,SOCK_DGRAM)
while True:
data = input('Msg>>')
if not data:
break
sockfd.sendto(data.encode(),ADDR)
msg,addr = sockfd.recvfrom(1024)
print('From server:',msg.decode())
sockfd.close()