我正在尝试接收通过套接字从客户端发送的消息。下面是我用来接收该消息的代码
def recieveData(serverPort):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s
s.bind((socket.gethostbyname(socket.gethostname()), serverPort)) #server bind to port
s.listen(5) #listen for connections
while True:
c, addr = s.accept() #accept client connection
dataRecieved = c.recv(1024).decode() #get data sent from client
c.close()
return dataRecieved
这在我的windows机器上运行,但在mac上不返回任何内容。我还注意到在我的mac上没有使用变量addr
,但它在我的windows计算机上使用。还有以下内容
print(socket.gethostbyaddr(socket.gethostbyname(socket.gethostname())))
在我的mac上产生以下错误,但它在windows上工作
socket.herror: [Errno 1] Unknown host`
为什么会这样?如果我能得到警告说addr
没有使用,我想它会起作用。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐