客户端:
代码语言:javascript
复制import socket
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_socket.connect(('127.0.0.1', 12345))
while True:
tcp_socket.send(input('你输入要回显的数据:').encode())
print('回显数据:', tcp_socket.recv(1024).decode())
服务端:
代码语言:javascript
复制import socket
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcp_socket.bind(('',12345))
tcp_socket.listen(128)
tcp_server, client_addr = tcp_socket.accept()
while True:
print(client_addr, '连接成功')
serve_recv = tcp_server.recv(1024)
tcp_server.send(serve_recv)