未使用TCP连接客户端IP地址

2024-09-29 18:35:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试接收通过套接字从客户端发送的消息。下面是我用来接收该消息的代码

 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没有使用,我想它会起作用。你知道吗


Tags: 代码client消息客户端bindwindowsmacsocket

热门问题