server类
import socket
sk = socket.socket() #创建socket
#添加地址bind绑定
address = ("127.0.0.1",9595)
sk.bind(address)
#支持多少人
sk.listen(2)
#接收客户端信息
while True:
conn,addr = sk.accept()
print(addr,"这个位置是下一次另一个客户端打开后能自动接收")
while True:
try:
cr = conn.recv(1024)
except Exception as e: #如果直接点击客户端直接点击
print("jimao------",e)
break
# print(cr, "--nn------------") # 判断客户端停止后返回值
if not cr:break
print(cr, "--------------") # 判断客户端停止后返回值
print(str(cr, "utf8"))
# print(str(cr)) #这个不能接收中文,只能接收字母数字
# 发送信息
inp = input("服务的输入:")
if inp == "88":
break
else:
conn.send(bytes(inp, "utf8"))
client类
import socket
sk = socket.socket() #创建socket对象
#连接服务端
address = ("127.0.0.1",9595)
sk.connect(address)
#发送信息到服务端
while True:
inp = input("这是客户端:")
if inp == "88":
break
else:
sk.send(bytes(inp,"utf8"))
#接收信息
sr = sk.recv(1024)
print("-----------",sr)
if sr:
print(str(sr, "utf8"))
else:
break