Python-based TCP and UDP (detailed description) and pythontcpudp
In python, UDP and TCP communication is implemented through socket (socket. There are two types of sockets for connection and connectionless, namely TCP socket and UDP socket.
TCP Communication Model
Create a TCP Server
Pseudocode:
Ss = socket () # create a server socket ss. bind () # bind the socket to the address to ss. listen () # Listening to inf_loop: # server infinite loop cs = ss. accept () # accept client connection comm_loop: # communication loop cs. recv ()/cs. send () # receive/send a dialog) cs. close () # close the client socket ss. close () # close server socket # (optional)
TCP timestamp Server:
Import socketfrom time import ctimeHOST = ''PORT = 8099 BUFSIZE = 1024 ADDR = (HOST, PORT) tcpSerSock = socket. socket (socket. AF_INET, socket. SOCK_STREAM) tcpSerSock. bind (ADDR) tcpSerSock. listen (5) while True: print ("waiting for connection ...... ") tcpCliSock, addr = tcpSerSock. accept () print ("... received connection: ", addr) while True: data = tcpSerSock. recv (BUFSIZE) if not data: break tcpCliSock. send ('[% s] % s' % (bytes (ctime (), 'utf-8'), data) tcpCliSock. close () tcpSerSock. close ()
Create a TCP client:
Pseudocode:
Cs = socket () # create a client socket cs. connect () # Try to connect to the server comm_loop: # communication loop cs. send ()/cs. recv () # Send/receive cs. close () # close the client socket
TCP timestamp client:
import socketHOST = 'localhost'PORT = 8099BUFSIZE = 1024ADDR = (HOST, PORT)tcpCliSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)tcpCliSock.connect(ADDR)while True: data = input("> ") if not data: break tcpCliSock.send(data) data = tcpCliSock.recv(BUFSIZE) if not data: break print(data)tcpCliSock.close()
UDP Communication Model
Create a UDP Server
Pseudocode:
Ss = socket () # create a server socket ss. bind () # bind the server socket inf_loop: # server infinite loop cs = ss. recvfrom ()/ss. sendto () # disable (receive/send) ss. close () # close the server socket
UDP timestamp Server:
Import socketfrom time import ctimeHOST = ''PORT = 8099 BUFSIZE = 1024 ADDR = (HOST, PORT) udpSerSock = socket. socket (socket. AF_INET, socket. SOCK_DGRAM) udpSerSock. bind (ADDR) while True: print ("waiting for connection ...... ") data, addr = udpSerSock. recvfrom (BUFSIZE) udpSerSock. sendto ('[% s] % s' % (ctime (), data), addr) print ("... received connection: ", addr) udpSerSock. close ()
Create a UDP client
Pseudocode:
Cs = socket () # create client socket comm_loop: # communication loop cs. sendto ()/cs. recvfrom () # Send/receive cs. close () # close the client socket
UDP timestamp client:
import socketHOST = 'localhost'PORT = 8099BUFSIZE = 1024ADDR = (HOST, PORT)udpCliSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)udpCliSock.connect(ADDR)while True: data = input("> ") if not data: break udpCliSock.sendto(data, ADDR) data, ADDR = udpCliSock.recvfrom(BUFSIZE) if not data: break print(data)udpCliSock.close()
The above article is based on the TCP and UDP (detailed description) in python. I hope to give you a reference and support for the customer's house.