18_TCP服务器端代码案例
一、TCP服务器端代码实例
二、源代码
server_socket = socket(AF_INET,SOCK_STREAM)
host_port = ('',8088)
server_socket.bind(host_port)
server_socket.listen(5)
new_socket,client_addr = server_socket.accept()
data = new_socket.recv(1024)
print("服务器端接受的数据是:",data.decode('utf-8'))
new_socket.send('Thank you!'.encode('utf-8'))
new_socket.close()
server_socket.close()
19_TCP客户端代码案例
一、代码实例
二、源代码
from socket import *
client_socket = socket(AF_INET,SOCK_STREAM)
server_ip_port = ('192.168.43.234',8088)
client_socket.connect(server_ip_port)
send_data = input("请输入:")
client_socket.send(send_data.encode('utf-8'))
recv_data = client_socket.recv(1024)
print("客户端接受到的服务器的数据为:",recv_data.decode('utf-8'))
client_socket.close()
三、运行结果
1、客户端发送数据
①发送数据
③收到服务器回复数据
2、服务器接收数据
②接收数据,回复数据
|