''' import socket # (1) 构建套接字对象,确定通信协议 sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM) # (2) 绑定IP和端口 ip_port = ("127.0.0.1", 9999) sock.bind(ip_port) # (3) 监听最大排队数 sock.listen(2) # (4) 阻塞等待客户端连接 while 1: print("server is waiting...") conn, addr = sock.accept() # print("conn", conn) # print("addr", addr) while 1: # 接受来自客户端的消息 data = conn.recv(1024) # recv是一个阻塞函数 print("data",data,type(data)) if data == b"" or data == b"q": break print("接受来自客户端的消息:", data.decode()) # 给给客户端发送消息:注意 send发送的字节 conn.send(data.decode().upper().encode()) '''
''' import socket # (1) 构建套接字对象,确定通信协议 sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM) ip_port = ("127.0.0.1", 9999) sock.connect(ip_port) while 1: # 客户端给服务端发送消息 data = input("请输入:") sock.send(data.encode()) if data == "q": break data = sock.recv(1024) print("服务端响应消息:", data.decode()) '''