1、代码如下:
import socket
socket_server=socket.socket()
#绑定IP地址和端口
socket_server.bind(('localhost',8888))
#监听1个端口
socket_server.listen(1)
#等待客户端连接
result=socket_server.accept()
#客户端和服务端的链接对象
conn=result[0]
#客户端的链接地址信息
address=result[1]
print(conn)
print(address)
data=conn.recv(1024).decode("utf-8")
print("客户端发来的信息是:{}".format(data))
#发送回复消息
msg=input("请输入你要和客户端回复的消息:")
#encode可以将字符串编码转换为字节数组对象
conn.sendall(msg.encode("utf-8"))
#关闭链接
conn.close()
socket_server.close()2、运行代码
3、下载网络助手的客户端
下载网址为:
https://github.com/nicedayzhu/netAssist/releases
4、网络助手的客户端发送消息
5、服务器端发送消息
下图的:客户端你好啊
