Jensen
Jensen

Reputation: 53

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). im using Mqtt.fx aswell as a second client to look at if the published messages are being recieved.

i was using "iot.eclipse.org" before i tried using the local hivemq broker and it worked fine. so im not too sure what the problem is. maybe im just forgetting something.

im using the paho mqtt module. and my library code is heavily ripped from here: https://pypi.python.org/pypi/paho-mqtt

im using a library/module and then referencing that in another .py file. 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:

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.

i think the virtual machine im using is ubuntu if that helps!

Thank you for your time in advance!

EDITED

added ifconfig output:

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:

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

Upvotes: 3

Views: 21129

Answers (1)

hardillb
hardillb

Reputation: 59618

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.

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:

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

Upvotes: 3

Related Questions