[python] Development of TCP client and server

1. The development process of the TCP client program

insert image description here
1. Create a client socket object (buy a phone)
2. Establish a connection with the server socket (call)
3. Send data (talk)
4. Receive data (answer)
5. Close the client socket ( Hang up the phone)
It is generally the client program that initiates the connection establishment request.

import socket
if __name__ == '__main__':
# 1.创建客户端套接字对象(买电话)
tcp_client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# 2.和服务端套接字建立连接(打电话)
tpc_client_socket.connect(("172.18.18.24",8080))
# 3,发送数据(说话)
tcp_client_socket.send("nihao".encode(encoding='utf-8'))
# 4.接收数据(接听)
recv_data = tcp_client_socket.recv(1024)
# 5.关闭客户端套接字(挂电话)
tcp_client_socket.close()

Guess you like

Origin blog.csdn.net/weixin_40293999/article/details/130277058