TCP/UDP are both network programming (sockets) based on the C/s structure of the program.
UDP: Non-reliable connection speed, server: Create socket binding IP and port directly from the specified IP and port to receive data without listening; client: Create socket, receive data directly, do not need to establish a connection.
s = socket.socket (socket.af_inet, socket. SOCK_DGRAM)
# Bound Port:
S.bind (' 127.0.0.1 ', 9999)
S.recvfrom (1024x768) #接收数据.
TCP: Reliable connection, the client also needs to establish a connection s.connect ((' 127.0.0.1 ', 9999)), server: Need to listen to S.listen (5),
Next, the server program passes a permanent loop to accept the connection from the client, and accept () waits for and returns a client connection:
While True:
# Accept a new connection:
Sock, addr = S.accept ()
# Create a new thread to handle the TCP connection:
t = Threading. Thread (Target=tcplink, args= (sock, addr))
T.start ()
The tcp/udp of Python learning