标签:add from serve set == 消息 lis 接收 服务器
#服务端
import socket from socket import SOL_SOCKET,SO_REUSEADDR sk = socket.socket() #创建服务端socket套接字 sk.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) #解决地址和端口被占用的情况 sk.bind(("127.0.0.1",8080)) #绑定服务器地址,端口号 sk.listen() #监听 等待消息 conn,adree = sk.accept() #接收客户端的连接 while 1: ret = conn.recv(1024).decode("utf-8") print(ret) if ret == "再见": conn.send(bytes("再见",encoding="utf-8")) break info = input(">>>") conn.send(bytes(info,encoding="utf-8")) # ret = conn.recv(1024) #接收客户端信息 参数一般为1024的整数倍 # print(ret) # conn.send(b"server") #向客户端发送消息 # ret = conn.recv(1024) # print(ret.decode("utf-8")) # conn.send(bytes("来吧来吧!",encoding="utf-8")) conn.close() #关闭客户连接 sk.close() #关闭服务套接字(可选)
#客户端
import socket sk = socket.socket() #创建客户套接字 sk.connect(("127.0.0.1",8080)) #尝试连接服务 while 1: info = input(">>>") sk.send(bytes(info,encoding="utf-8")) ret = sk.recv(1024).decode("utf-8") print(ret) if ret == "再见": sk.send(bytes("再见",encoding="utf-8")) break # sk.send(b"client") # ret = sk.recv(1024) # print(ret) # sk.send(bytes("约会不?",encoding="utf-8")) # ret = sk.recv(1024) # print(ret.decode("utf-8")) sk.close()
标签:add from serve set == 消息 lis 接收 服务器
原文地址:https://www.cnblogs.com/superniao/p/10085508.html