客户端----向服务端发送信息和接收服务端返回的信息
import socket
flag = True
client = socket.socket()
client.connect(('localhost', 8080))#连接服务端口
while flag:
msg = input(">>>").strip()/#获取要发送的信息
if len(msg) == 0: #当信息长度为0
print("Input empty")
continue
client.send(msg.encode())
data = client.recv(1024)
print(data)
client.close()
服务端----接收客户端传来的信息,发送接收成功的信息
import socket
flag = True
server = socket.socket()
server.bind(('localhost', 8080))#开设服务端口
server.listen()#开始监听
print("Waiting for connecting...")
conn, addr = server.accept()
print('Client:Online')
while flag:
data = conn.recv(1024).decode()
print('Receive:', data)
conn.send('Successful'.encode())#向客户端发送信息接收成功的信息
server.close()#关闭服务器