Python IPC 1: TCP-Based Socket Communication Example

Application Scenario

Inter-process communication across networks, with connection and reliable transmission.

TCP-Based Socket Communication Process

TCP Server TCP Client

1、socket() 1、socket()

2、bind()

3、listen()

4、accept() <———————– 2、connect()

5、recv() <———————— 3、send()

6、send() ————————-> 4、recv()

7、close() 5、close()

Description:

socket(): Create a socket.

bind(): The server binds the IP and port,called only by the server.

listen(): The server starts listening for client connection requests,called only by the server.

accept(): The server blocks and waits for a client connection,called only by the server.

connect(): The client connects to the server,called only by the client.

send() / sendall(): Send byte data.

recv(): Block and receive data.

close(): Close the connection and the socket.The client calls close(), and the server will receive empty data, indicating that the client has disconnected.

Server Example Code

import socket
HOST = '127.0.0.1'
PORT = 5000
# Create socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind address and port
server.bind((HOST, PORT))
# Start listening, ready to accept client requests
server.listen()
print("Server started, waiting for client connection...")
# Block and wait for client connection
conn, addr = server.accept()
print("Client connected:", addr)
while True:
    data = conn.recv(1024)
    if not data:
        print("Client disconnected")
        break
    print("Client sent content:", data.decode())
    # Send message back to client
    conn.sendall(b"Server received: " + data)
conn.close()
server.close()

Client Example Code

import socket
HOST = '127.0.0.1'
PORT = 5000
# Create socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to server
client.connect((HOST, PORT))
while True:
    msg = input("Enter the message to send:")
    if msg == "quit":
        break
    client.sendall(msg.encode())
    # Receive server reply
    data = client.recv(1024)
    print("Server reply content:", data.decode())
client.close()

Verification

1、Start the server.

python tcp_server.py

2、Start the client.

python tcp_client.py

3、The client inputs data and checks the printout.

Sequentially input 1, 100, hello, this is a test.

The client terminal prints as follows:

Enter the message to send: 1

Server reply content: Server received: 1

Enter the message to send: 100

Server reply content: Server received: 100

Enter the message to send: hello

Server reply content: Server received: hello

Enter the message to send: this is a test

Server reply content: Server received: this is a test

4、The server receives data and checks the printout.

The server terminal prints as follows:

Server started, waiting for client connection…

Client connected: (‘127.0.0.1’, 14297)

Client sent content: 1

Client sent content: 100

Client sent content: hello

Client sent content: this is a test

5、The client inputs “quit” to exit the program.

Leave a Comment