import socket
server = socket.socket() # 1、新建socket
server.bind(('10.20.132.191', 8999)) # 2、绑定ip和端口
server.listen(5) # 3、监听连接
s, addr = server.accept() # 4、接受连接
print('connect addr: {}'.format(addr)) # 连接地址, connect:连接,addr:地址
while True:
content = s.recv(1024) # recv(receive):接收
if len(content) == 0:
break
s.send(content)
print(str(content, encoding='utf-8')) # 接受来自客户端的消息,并编码打印出来
s.close()
import socket
client = socket.socket() # 1、新建socket
client.connect(('127.0.0.1', 8999)) # connect: 连接。连接服务端,注意 IP和端口要和服务端一致
while True:
content = input('Please send to server, input "break" exit:') # content:内容
if len(content) == 0:
break
client.send(bytes('a', encoding='utf-8')) # 发送内容,注意发送的是字节字符串
content = client.recv(1024) # recv=receive:接收,接收内容的最大限制1024k
print(str(content, encoding='utf-8'))
client.close()