implement TCP socket communication simple cycle (Python)

A, socket communication process

TCP server creates a socket -> IP address and port bindings -> listen for client connection requests -> incoming client connections (client connection) -> block until a client connects -> processing request (communication cycle) -> close the connection -> Close the socket

TCP client socket created -> IP and port connected to the server -> processing request (communication cycle) -> Close the socket

Second, the socket functions

1. The server socket function

s.bind(address)

The socket is bound to address at AF_INET, expressed as a tuple address (host, port) of.

s.listen(backlog)

Start listening for incoming TCP connections. backlog specified before connection rejection, the operating system can suspend the maximum number of connections. The value of at least 1, most of the application is set to 5 on it.

s.accept()

And returned to accept the TCP connection (conn, address), where conn is a new socket object, it can be used to receive and transmit data. address is the address of the connecting client.

2. The client socket functions

s.connect(address)

Connected to the socket at the address. The general format of address tuples (hostname, port), if the connection error, an error return socket.error.

3. Public socket functions

s.recv(bufsize[,flag])

Accept TCP socket data. Data is returned as a string, bufsize specify the maximum amount of data to be received. flag provides additional information about the message can usually be ignored.

s.send(string[,flag])

Send TCP data. The transmission data string is attached to the socket. The return value is the number of bytes to be transmitted, this number may be smaller than the size in bytes of the string.

s.close()

Closes the socket.

Third, code implementation (version a)

Achieve results: the client unilaterally to send a message, the server displays the message sent by the client

server.py

import socket

# 创建套接字
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 绑定ip地址与端口号
sock.bind(('127.0.0.1', 8080))

# 建立监听,最大连接数为5个
sock.listen(5)

# 接收客户端的连接
conn, client_addr = sock.accept()

# 通讯循环
while True:
    # 接收消息
    msg = conn.recv(1024)  # 最大接收字节数为1024
    if not msg:
        continue
    print('client is sending: ' + msg.decode('utf-8'))

# 断开连接
conn.close()

# 关闭套接字
sock.close()

client.py

import socket

# 创建套接字
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 连接服务端
sock.connect(('127.0.0.1', 8080))

# 建立通讯循环
while True:
    msg = input('Enter your message: ')
    if msg == 'quit':
        print('communication ended')
        break
    sock.send(msg.encode('utf-8'))  # Mac操作系统使用utf-8编码方式

# 断开连接
sock.close()

Run the server, and then run the client

Operating results: (p1 client.py, p2 server.py)

Fourth, code implementation (Second Version)

Client server messaging interoperability

server.py

import socket

# 创建套接字
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 绑定ip地址与端口号
sock.bind(('127.0.0.1', 8081))

# 建立监听,最大连接数为5个
sock.listen(5)

# 接收客户端的连接
conn, client_addr = sock.accept()

# 通讯循环
while True:
    # 接收消息
    msg = conn.recv(1024)  # 最大接收字节数为1024
    if not msg:
        continue
    print('client is sending: ' + msg.decode('utf-8'))
    reply = input('reply: ')
    conn.send(reply.encode('utf-8'))

# 断开连接
conn.close()

# 关闭套接字
sock.close()

client.py

import socket

# 创建套接字
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 连接服务端
sock.connect(('127.0.0.1', 8081))

# 建立通讯循环
while True:
    msg = input('Enter your message: ')
    if msg == 'quit':
        print('communication ended')
        break
    sock.send(msg.encode('utf-8'))  # Mac操作系统使用utf-8编码方式
    reply = sock.recv(1024)
    print('server is sending: ' + reply.decode('utf-8'))

# 断开连接
sock.close()

Run the server, and then run the client

Operating results: (p1 client.py, p2 server.py)


Guess you like

Origin www.cnblogs.com/rainbow-ran/p/12535999.html