简体   繁体   English

套接字错误:Errno 111 连接被拒绝

[英]Socket Error: Errno 111 Connection Refused

im trying to connect to a locally hosted HiveMQ broker using a RaspberryPi Virtual Machine (Using Oracle VM VirtualBox).我正在尝试使用 RaspberryPi 虚拟机(使用 Oracle VM VirtualBox)连接到本地托管的 HiveMQ 代理。 im using Mqtt.fx aswell as a second client to look at if the published messages are being recieved.我使用 Mqtt.fx 以及第二个客户端来查看是否收到了发布的消息。

i was using "iot.eclipse.org" before i tried using the local hivemq broker and it worked fine.在尝试使用本地 hivemq 代理之前,我使用的是“iot.eclipse.org”,它运行良好。 so im not too sure what the problem is.所以我不太确定问题是什么。 maybe im just forgetting something.也许我只是忘记了一些东西。

im using the paho mqtt module.我正在使用 paho mqtt 模块。 and my library code is heavily ripped from here: https://pypi.python.org/pypi/paho-mqtt我的库代码从这里被大量撕掉: https : //pypi.python.org/pypi/paho-mqtt

im using a library/module and then referencing that in another .py file.我使用一个库/模块,然后在另一个 .py 文件中引用它。 This is the code from the library/module:这是库/模块中的代码:

import paho.mqtt.client as mqtt

client = mqtt.Client()                       

#Called on connection to server/broker
def on_connect(client, userdata, rc):                                               
    print("connected with result code"+str(rc))                                 

#Called when new message published to subscribed topic
def on_message(client, userdata, msg):          
    print("NEW PUBLISH: "+msg.topic+" "+str(msg.payload))

#configure connection to the broker
def setup():                                    
    client.on_connect = on_connect
    client.on_message = on_message

#Subscribe
def subscribe(topic):
    print("subscribing to topic: " +topic)
    client.subscribe(topic)

#Connect to broker
def connect():
    client.connect("127.0.0.1", 1883, 60)

def publish(topic, msg):
    client.publish(topic, msg)

and this is the code im actually running.这是我实际运行的代码。 Just 2 simple publishes:只需 2 个简单的发布:

import mqttLib as owly

owly.setup()
owly.connect()
owly.publish("test/library", "test okay :)")

num1 = input("1st number: ")
num2 = input("2nd number: ")
mult = num1 * num2

owly.publish("test/mult", mult)

This is the message i get after running it:这是我运行后得到的消息:

 Traceback (most recent call last):
  File "mqttTest.py", line 6, in <module>
  owly.connect()
  File "/home/rpi/python/MQTT/mqttLib.py", line 27, in connect
  client.connect("127.0.0.1", 1883, 60)
  File "/usr/local/lib/python2.6/dist-packages/paho/mqtt/client.py", line 612, In connect
  return self.reconnect()
  File "/usr/local/lib/python2.6/dist-packages/paho/mqtt/client.py", line 732, in reconnect
  sock = socket.create_connection((self._host, self._port))
  File "/usr/lib/python2.6/socket.py", line 514, in create_connection
  raise error, msg
  socket.error: [Errno 111] Connection refused

The fact it works on a remotely hosted broker.它适用于远程托管代理的事实。 and not locally confuses me.而不是本地让我感到困惑。

i have stripped off most of the plugins on HiveMQ and left the message log plugin on HiveMQ aswell so i dont think its anything to do with authentication.我已经去掉了 HiveMQ 上的大部分插件,并在 HiveMQ 上留下了消息日志插件,所以我认为它与身份验证无关。

i think the virtual machine im using is ubuntu if that helps!如果有帮助,我认为我使用的虚拟机是 ubuntu!

Thank you for your time in advance!提前感谢您的时间!

EDITED已编辑

added ifconfig output:添加 ifconfig 输出:

rpi@RaspberryPi:~$ ifconfig 
eth0      Link encap:Ethernet  HWaddr 08:00:27:5b:26:64  
      inet addr:192.168.56.101  Bcast:192.168.56.255  Mask:255.255.255.0
      inet6 addr: fe80::a00:27ff:fe5b:2664/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:607 errors:0 dropped:0 overruns:0 frame:0
      TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:82290 (82.2 KB)  TX bytes:1920 (1.9 KB)

lo    Link encap:Local Loopback  
      inet addr:127.0.0.1  Mask:255.0.0.0
      inet6 addr: ::1/128 Scope:Host
      UP LOOPBACK RUNNING  MTU:16436  Metric:1
      RX packets:0 errors:0 dropped:0 overruns:0 frame:0
      TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0 
      RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

added netstat -anp output:添加了 netstat -anp 输出:

rpi@RaspberryPi:~$ netstat -anp
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
 Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State PID/Program name
udp        0      0 0.0.0.0:68              0.0.0.0:*                               -               
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name    Path
unix  2      [ ACC ]     STREAM     LISTENING     5465     1017/lxterminal     /tmp/.lxterminal-socket:0.0-rpi
unix  2      [ ACC ]     STREAM     LISTENING     2354     -                   @/com/ubuntu/upstart
unix  2      [ ACC ]     STREAM     LISTENING     3653     -                   /tmp/.X11-unix/X0
unix  2      [ ACC ]     STREAM     LISTENING     3652     -                   @/tmp/.X11-unix/X0
unix  2      [ ]         DGRAM                    2462     -                   @/org/kernel/udev/udevd
unix  2      [ ACC ]     STREAM     LISTENING     4693     891/dbus-daemon     @/tmp/dbus-DYueHXFGGf
unix  2      [ ACC ]     STREAM     LISTENING     4524     814/gnome-keyring-d /tmp/keyring-OM3Fh4/control
unix  2      [ ACC ]     STREAM     LISTENING     5019     913/gam_server      @/tmp/fam-rpi-
unix  2      [ ]         DGRAM                    5068     -                   @/org/freedesktop/hal/udev_event
unix  2      [ ACC ]     STREAM     LISTENING     4680     -                   /tmp/ssh-ESoUnvu822/agent.822
unix  2      [ ACC ]     STREAM     LISTENING     5037     -                   @/var/run/hald/dbus-ejQUT9OaXl
unix  2      [ ACC ]     STREAM     LISTENING     4832     904/pcmanfm         /tmp/.pcmanfm-socket:0.0-rpi
unix  2      [ ACC ]     STREAM     LISTENING     3497     -                   /var/run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     4834     909/menu-cached     /tmp/.menu-cached-:0-rpi
unix  2      [ ACC ]     STREAM     LISTENING     4966     814/gnome-keyring-d /tmp/keyring-OM3Fh4/pkcs11
unix  6      [ ]         DGRAM                    3504     -                   /dev/log
unix  2      [ ACC ]     STREAM     LISTENING     5014     814/gnome-keyring-d /tmp/keyring-OM3Fh4/ssh
unix  2      [ ACC ]     STREAM     LISTENING     5042     -                   @/var/run/hald/dbus-9woYq1KdE9
unix  3      [ ]         STREAM     CONNECTED     5468     -                   
unix  3      [ ]         STREAM     CONNECTED     5467     1017/lxterminal     
unix  3      [ ]         STREAM     CONNECTED     5463     -                   @/tmp/.X11-unix/X0
unix  3      [ ]         STREAM     CONNECTED     5462     1017/lxterminal     
unix  2      [ ]         DGRAM                    5365     -                   
unix  3      [ ]         STREAM     CONNECTED     5288     -                   @/var/run/hald/dbus-ejQUT9OaXl
unix  3      [ ]         STREAM     CONNECTED     5287     -                   
unix  3      [ ]         STREAM     CONNECTED     5283     -                   @/var/run/hald/dbus-ejQUT9OaXl
unix  3      [ ]         STREAM     CONNECTED     5239     -                   
unix  3      [ ]         STREAM     CONNECTED     5237     -                   /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     5236     -                   
unix  3      [ ]         STREAM     CONNECTED     5144     -                   @/var/run/hald/dbus-ejQUT9OaXl
unix  3      [ ]         STREAM     CONNECTED     5134     -                   
unix  3      [ ]         STREAM     CONNECTED     5063     -                   @/var/run/hald/dbus-9woYq1KdE9
unix  3      [ ]         STREAM     CONNECTED     5062     -                    
unix  3      [ ]         STREAM     CONNECTED     5039     -                   /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     5038     -                   
unix  3      [ ]         STREAM     CONNECTED     5025     -                   /var/run/dbus/system_bus_socket
 unix  3      [ ]         STREAM     CONNECTED     5024     904/pcmanfm         
 unix  3      [ ]         STREAM     CONNECTED     5022     913/gam_server      @/tmp/fam-rpi-
 unix  3      [ ]         STREAM     CONNECTED     5021     904/pcmanfm         
 unix  2      [ ]         DGRAM                    4969     814/gnome-keyring-d 
 unix  3      [ ]         STREAM     CONNECTED     4964     891/dbus-daemon     @/tmp/dbus-DYueHXFGGf
 unix  3      [ ]         STREAM     CONNECTED     4963     814/gnome-keyring-d 
 unix  3      [ ]         STREAM     CONNECTED     4842     909/menu-cached     /tmp/.menu-cached-:0-rpi
 unix  3      [ ]         STREAM     CONNECTED     4838     902/lxpanel         
 unix  3      [ ]         STREAM     CONNECTED     4830     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4829     904/pcmanfm         
 unix  3      [ ]         STREAM     CONNECTED     4796     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4795     901/xscreensaver    
 unix  3      [ ]         STREAM     CONNECTED     4788     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4787     902/lxpanel         
 unix  3      [ ]         STREAM     CONNECTED     4762     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4761     897/openbox         
 unix  3      [ ]         STREAM     CONNECTED     4701     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4700     822/lxsession       
 unix  3      [ ]         STREAM     CONNECTED     4697     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4696     890/dbus-launch     
 unix  3      [ ]         STREAM     CONNECTED     4695     891/dbus-daemon     
 unix  3      [ ]         STREAM     CONNECTED     4694     891/dbus-daemon     
 unix  3      [ ]         STREAM     CONNECTED     4683     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4682     890/dbus-launch     
 unix  3      [ ]         STREAM     CONNECTED     4673     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4672     882/VBoxClient      
 unix  3      [ ]         STREAM     CONNECTED     4671     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4670     882/VBoxClient      
 unix  3      [ ]         STREAM     CONNECTED     4666     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4665     875/VBoxClient      
 unix  3      [ ]         STREAM     CONNECTED     4664     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4663     875/VBoxClient      
 unix  3      [ ]         STREAM     CONNECTED     4656     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4655     866/VBoxClient      
 unix  3      [ ]         STREAM     CONNECTED     4527     -                   /var/run/dbus/system_bus_socket
 unix  3      [ ]         STREAM     CONNECTED     4526     -                   
 unix  3      [ ]         STREAM     CONNECTED     4440     -                   /var/run/dbus/system_bus_socket
 unix  3      [ ]         STREAM     CONNECTED     4439     -                   
 unix  3      [ ]         STREAM     CONNECTED     4415     -                   /var/run/dbus/system_bus_socket
 unix  3      [ ]         STREAM     CONNECTED     4414     -                   
 unix  3      [ ]         STREAM     CONNECTED     4402     -                   /var/run/dbus/system_bus_socket
 unix  3      [ ]         STREAM     CONNECTED     4401     -                   
 unix  2      [ ]         DGRAM                    4400     -                   
 unix  2      [ ]         DGRAM                    4355     -                   
 unix  3      [ ]         STREAM     CONNECTED     4356     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     3663     -                   
 unix  3      [ ]         STREAM     CONNECTED     3517     -                   /var/run/dbus/system_bus_socket
 unix  3      [ ]         STREAM     CONNECTED     3516     -                   
 unix  3      [ ]         STREAM     CONNECTED     3515     -                   
 unix  3      [ ]         STREAM     CONNECTED     3514     -                   
 unix  3      [ ]         DGRAM                    2494     -                   
 unix  3      [ ]         DGRAM                    2493     -               unix  3      [ ]         STREAM     CONNECTED     2445     -                   @/com/ubuntu/upstart
 unix  3      [ ]         STREAM     CONNECTED     2444     -                   

r r

As mentioned in the comments, 127.0.0.1 is the local address of the VM, what you need to use is the IP address of the host where the broker is running.正如评论中提到的,127.0.0.1是VM的本地地址,您需要使用的是运行代理的主机的IP地址。

In this case the broker host is on the VM host, you can get address of the VM host using the ip route command on the VM, it's output it will look something like this:在这种情况下,代理主机在 VM 主机上,您可以在 VM 上使用ip route命令获取 VM 主机的ip route ,它的输出将如下所示:

default via 192.168.56.100 dev eth0 
192.168.56.0/24 dev eth0  proto kernel  scope link  src 192.168.56.100

The IP address you want to use for your broker should be the one in the line that starts with default, in the example case 192.168.56.100您要用于代理的 IP 地址应该是以 default 开头的行中的 IP 地址,在示例中为192.168.56.100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM