UDP/TCP-based Socket programming demo
UDP Client/server
A simple UDP protocol-based process communication for client and server applications.
Logic:
The client sends a lowercase English letter to the server, the server accepts it, converts it to uppercase and returns it to the client, and the client outputs the set of numbers. Since Python3 has a clear distinction between the Str class and the bytes class, the bytes are sent.
UDP Server:
from socket import *udp_server_socket = socket(AF_INET, SOCK_DGRAM)udp_server_port = 9600name = gethostname()udp_server_socket.bind((‘‘, udp_server_port))while True: print(‘The Sever is ready to receive‘) # 这段代码会一直处于阻塞状态,除非收到了响应 message, client_address = udp_server_socket.recvfrom(2048) print(‘Client Address:‘, str(client_address), ‘Coming!‘) modified_message = message.upper() udp_server_socket.sendto(modified_message, client_address)
UDP client
from socket import *server_ip = ‘127.0.0.1‘server_port = 9600udp_client_socket = socket(AF_INET, SOCK_DGRAM)message = b‘hello‘# 发送给服务器udp_client_socket.sendto(message, (server_ip, server_port))# 接受服务器的返回内容modified_message, sever_address = udp_client_socket.recvfrom(2048)print(modified_message)udp_client_socket.close()
TCP Client/server
A simple TCP protocol-based process communication for client and server applications.
TCP Server
from socket import *tcp_server_socket = socket(AF_INET, SOCK_STREAM)tcp_server_port = 9700tcp_server_socket.bind((‘127.0.0.1‘, tcp_server_port))tcp_server_socket.listen(1)while True: # 等待连接 print(‘Waiting for connecting!‘) # 建立TCP连接 connection_socket, addr = tcp_server_socket.accept() print(‘Connected from:‘, addr) message = connection_socket.recv(2048) print(‘The Sever is ready to receive‘) upper_message = message.upper() print(upper_message) connection_socket.send(upper_message) connection_socket.close()
TCP Client
from socket import *server_ip = ‘127.0.0.1‘server_port = 9700tcp_client_socket = socket(AF_INET, SOCK_STREAM)tcp_client_socket.connect((server_ip, server_port))message = b‘hello‘# 发送给服务器tcp_client_socket.send(message)# 接受服务器的返回内容modified_message = tcp_client_socket.recv(2048)print(‘Modified Message:‘, modified_message)tcp_client_socket.close()
Summary
At the time of writing this demo, I made a few minor mistakes:
In the TCP server socket, my recv method was not connection_socket
operational on the connection, resulting in an error: Transport endpoint is not connected
Similar: https://stackoverflow.com/questions/35969714/error-transport-endpoint-is-not-connected-python-sockets
In the TCP client socket, my recv method takes two parameters for granted, resulting in an error. This is shown because there is no difference between the TCP and UDP sockets.
Since TCP is connection-oriented, each time the socket object is read and written, it does not have to fill in the destination host address, only need to read and write the data directly, so can only accept one parameter (data).
And UDP is no connection, each need to send the data need to fill in the destination host address, acceptance is always two parameters: data and address.
Python Socket Programming Server/Client