|
#!/usr/bin/python3
# 文件名:server.py
# 导入 socket、sys 模块
import socket
import sys
import time
# 创建 socket 对象
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 获取本地主机名
host = socket.gethostname()
print(host)
myaddr = socket.gethostbyname(host)
print(myaddr)
port = 9999
# 绑定端口号
serversocket.bind(('192.168.1.197', port))
# 设置最大连接数,超过后排队
serversocket.listen(5)
while True:
# 建立客户端连接
clientsocket, addr = serversocket.accept()
print("连接地址: %s" % str(addr))
msg = '欢迎访问格物斯坦机器人!' + "\r\n"
clientsocket.send(msg.encode('utf-8'))
clientsocket.close()
#!/usr/bin/python3
# 文件名:client.py
# 导入 socket、sys 模块
import socket
import sys
# 创建 socket 对象
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 获取本地主机名
host = socket.gethostname()
print(host)
# 获取本机ip
myaddr = socket.gethostbyname(host)
print(myaddr)
# 设置端口号
port = 9999
severIP='192.168.1.197'
# 连接服务,指定主机和端口
s.connect((severIP, port))
# 接收小于 1024 字节的数据
msg = s.recv(1024)
print(msg.decode('utf-8'))
s.close()
|
|