1 网络通信协议
Tcp udp的区别 重点(*****)
Tcp三次握手四次挥手(******)
udp客户端多人聊天
import socket
udp_client = socket.socket(type=socket.SOCK_DGRAM)
ip_port = ('127.0.0.1', 8002)
while 1:
to_server_msg = input('客户端说:')
udp_client.sendto(to_server_msg.encode('utf-8'), ip_port)
from_server_msg, server_addr = udp_client.recvfrom(1024)
print(from_server_msg.decode('utf-8'), server_addr)
udp_client.close()
udp服务端多人聊天
import socket
udp_server = socket.socket(type=socket.SOCK_DGRAM) #datagram
ip_port = ('127.0.0.1',8002)
udp_server.bind(ip_port)
while 1:
from_client_msg, client_addr = udp_server.recvfrom(1024)
print(from_client_msg.decode('utf-8'), client_addr)
to_client_msg = input('服务端说:')
udp_server.sendto(to_client_msg.encode('utf-8'), client_addr)
udp_server.close()