1. 程式人生 > >網路通訊協議tcp,udp區別

網路通訊協議tcp,udp區別

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