2014-12-04 111 views
1

我无法连接到我自己的套接字本地主机
s.connect(('127.0.0.1', 4458))(或“本地主机”)将只考虑半天,
并最终超时TimeoutError: [Errno 110] Connection timed out连接到本地主机上的套接字

应该打开端口4458,那么另一个脚本会发送一些字符给它。这两个脚本都应该在同一个Raspberry Pi上运行,而'服务器'将使用sudo(用于访问GPIO)执行,另一个脚本不会作为聊天机器人。

我在Pi上运行服务器(使用python 3.4.1)和笔记本电脑上的客户端(mac,python 3.4.2)没有任何问题。

另外,它在反向上工作,笔记本电脑上的服务器脚本和树莓上的客户端。

作为最终测试,它可以与上述macbook上的服务器和客户端一起工作。

Pi上的服务器+客户端无法正常工作。

程序冻结

我的缩短码,如果有帮助:

# $ sudo python3 server.py 

__author__ = 'luckydonald' 
import socket # server 
import time # wait for retry 
import threading 
class Server(threading.Thread): 
    port = 4458; 
    QUIT = False 

    def run(self): 
     s = socket.socket() 
     failed = True 
     print ("Starting Server on Port %d" % (self.port)) 
     while failed: 
      try: 
       s.bind(("", self.port)) 
      except Exception as err: 
       print(err) 
       print("Port assignment Failed. Retring in 1 second.") 
       time.sleep(1) 
      else: 
       failed = False 
       print("Success.") 
     while not self.QUIT: 
      print("Listening!") 
      conn, addr = s.accept() # freezes here 
      print("Got something: %s , %s" %(str(conn), str(addr))) 
      while not self.QUIT: 
       result = conn.recv(1) 
       print("Got result: " + str(result)) 
server = Server(); 
server.daemon = True 
server.run(); 
# server.start(); 

以及客户端:

# python3 cilent.py 
s = socket.socket() 
print("connecting...") 
s.connect(("localhost",4458)) # also tried "172.0.0.1" # freezes here 
print("connected!") 
s.sendall("+".encode("utf-8")) 
s.sendall("-".encode("utf-8")) 
s.close() 

它会导致这样的:

Picture of console output

回答

1

我没想到的是localhost/127.0.0.1没有工作。 Ping results

我在我的hosts文件中有一个格式错误的条目。