UPD通信
一、UDP通信
udp是无链接的,启动服务之后可以直接接受消息,不需要提前建立链接
二、UDP基本使用
server:
1 import socket 2 3 server = socket.socket(type=socket.SOCK_DGRAM) # UDP协议 4 server.bind(('127.0.0.1',8080)) 5 # UDP不需要设置半连接池 它也没有半连接池的概念 6 7 # 因为没有双向通道 不需要accept 直接就是通信循环 8 while True: 9 data, addr = server.recvfrom(1024) 10 print('数据:',data) # 客户端发来的消息 11 print('地址:',addr) # 客户端的地址 12 server.sendto(data.upper(),addr)
client:
import socket client = socket.socket(type=socket.SOCK_DGRAM) # 不需要建立连接 直接进入通信循环 server_address = ('127.0.0.1',8080) while True: client.sendto(b'hello',server_address) data, addr = client.recvfrom(1024) print('服务端发来的数据',data) print('服务端的地址',addr)
三、TCP与UDP之间的区别
数据报协议(自带报头),所以不会有粘包问题
server:
1 import socket 2 3 4 server = socket.socket(type=socket.SOCK_DGRAM) 5 server.bind(('127.0.0.1',8080)) 6 7 8 # while True: 9 data, addr = server.recvfrom(1024) 10 print(data) 11 data, addr1 = server.recvfrom(1024) 12 print(data) 13 data, addr2 = server.recvfrom(1024) 14 print(data)
client:
import socket client = socket.socket(type=socket.SOCK_DGRAM) server_address = ('127.0.0.1',8080) client.sendto(b'what u doing?',server_address) client.sendto(b'what u doing?',server_address) client.sendto(b'what u doing?',server_address)
四、socketserver模块
TCP协议中的使用:
server:
1 import socketserver 2 3 class MyServer(socketserver.BaseRequestHandler): 4 def handle(self): 5 while True: 6 data = self.request.recv(1024) 7 print(self.client_address) #客户端地址 8 print(data.decode('utf-8')) 9 self.request.send(data.upper()) 10 11 if __name__ == '__main__': 12 ''' 13 只要有客户端连接,会自动交给自定义类中的handle方法去处理 14 ''' 15 server = socketserver.ThreadingTCPServer(('127.0.0.1',8080),MyServer)#创建 16 server.serve_forever() #启动该服务对象
client:
1 import socket 2 3 client = socket.socket() 4 client.connect(('127.0.0.1',8080)) 5 6 while True: 7 client.send(b'hello') 8 data = client.recv(1024) 9 print(data.decode('utf-8'))
UDP协议中的使用:
server:
1 import socketserver 2 3 class MyServer(socketserver.BaseRequestHandler): 4 def handle(self): 5 while True: 6 data,sock = self.request 7 print(self.client_address)#客户端的地址 8 print(data.decode('utf-8')) 9 sock.sendto(data.upper(),self.client_address) 10 11 if __name__ == '__main__': 12 ''' 13 只要有客户端连接 会自动交给自定义的handle方法去处理 14 ''' 15 server = socketserver.ThreadingUDPServer(('127.0.0.1',8080),MyServer) #创建一个基于TCP的对象 16 server.serve_forever() #启动该服务对象
client:
1 import socket 2 import time 3 4 client = socket.socket(type = socket.SOCK_DGRAM) 5 server_address = ('127.0.0.1',8080) 6 7 while True: 8 client.sendto(b'hey boy',server_address) 9 data,addr = client.recvfrom(1024) 10 print(data.decode('utf-8'),addr) 11 time.sleep(1)
五、实现一个简易版本的QQ
server:
1 import socket 2 server = socket.socket(type=socket.SOCK_DGRAM) 3 server.bind(('127.0.0.1',8080)) 4 5 while True: 6 data,addr = server.recvfrom(1024) 7 print(data.decode('utf-8')) 8 msg = input('>>>: ').strip() 9 server.sendto(msg.encode('utf-8'),addr)
client1:
1 import socket 2 3 client = socket.socket(type=socket.SOCK_DGRAM) 4 server_address = ('192.168.12.58',8080) 5 6 while True: 7 msg = input('>>>:') 8 msg = "来自客户端1的消息: %s"%msg 9 client.sendto(msg.encode('utf-8'),server_address) 10 data,addr = client.recvfrom(1024) 11 print(data.decode('utf-8'))
client2:
1 import socket 2 3 client = socket.socket(type=socket.SOCK_DGRAM) 4 server_address = ('192.168.12.58',8080) 5 6 while True: 7 msg = input('>>>:') 8 msg = "来自客户端2的消息: %s"%msg 9 client.sendto(msg.encode('utf-8'),server_address) 10 data,addr = client.recvfrom(1024) 11 print(data.decode('utf-8'))
client3:
1 import socket 2 3 client = socket.socket(type=socket.SOCK_DGRAM) 4 server_address = ('192.168.12.58',8080) 5 6 while True: 7 msg = input('>>>:') 8 msg = "来自客户端3的消息: %s"%msg 9 client.sendto(msg.encode('utf-8'),server_address) 10 data,addr = client.recvfrom(1024) 11 print(data.decode('utf-8'))