1

Im trying to connect to a small server(broker), HiveMQ, with a TCP socket connection. When I ping the server via cmd I get the message:

TCP 0.0.0.0:1883 0.0.0.0:0 LISTENING

The connection code in C is like this:

addr.sin_family = SL_AF_INET; addr.sin_port = sl_Htons(1883); addr.sin_addr.s_addr = sl_Htonl("0.0.0.0");

And I get this error:

Error [-111] at line [926] in function [ConnectionToHiveMq]

which indicates an error whith my socket connection:

rc = sl_Connect(mysock, (SlSockAddr_t *) &addr, sizeof(addr));

Can someone help me with this? I have tried many different portnumbers and IP:s but still doesn't work. Itis working if I connect to an application client on the web, where I can subscribe messages from this code. But when Itry with my lokal (broker) server, it dosent...

Ouizzo
  • 5
  • 5

1 Answers1

0

The 0.0.0.0:1883 in the netstat(8) output TCP 0.0.0.0:1883 0.0.0.0:0 LISTENING just means that the server is listening on port 1883 on all available interfaces on that box. You still need a valid IP address to connect to it though (something like 192.168.0.1), which would be the address of one of those interfaces (see ifconfig(8)), and is 127.0.0.1 for the loopback interface, if you are on the same machine.

Also, your code line addr.sin_addr.s_addr = sl_Htonl("0.0.0.0"); is completely wrong. You need to convert the string representation of the IP address to its integer form with, e.g., inet_pton(3).

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • Ok, thanks alot Nikolai N Fetissov!, that explain it very good! What is meant by "loopback interface"? Is it the interface I sent data(subscribe) from? – Ouizzo May 06 '15 at 15:04
  • Loopback is a *virtual* network interface that always means *this computer*. It would be the interface named `lo` in the `ifconfig` output if you are on a Linux box. – Nikolai Fetissov May 06 '15 at 15:07
  • Ok, I guess that's the same if you run it on a windows machine? – Ouizzo May 06 '15 at 15:10
  • Yes, interface name will be different, but the IP `127.0.0.1` is standard across the board. On Windows you want to use `ipconfig` command instead of Unix `ifconfig`. – Nikolai Fetissov May 06 '15 at 15:14
  • I have one more question that I need some answer to. Im, as I ment earlier, using a broker (a small server that just passing the data thrue subscribe and publish messages). Do I have to connect to the broker with this TCP code. Or to the client, that I wish to subscribe to? – Ouizzo May 06 '15 at 15:15
  • That I have no idea about - you will have to look into HiveMQ documentation. Also note that you can **accept** useful answers here :) – Nikolai Fetissov May 06 '15 at 15:16
  • Ok, but anyway. Thanks alot for your help, that clarify it very much! – Ouizzo May 06 '15 at 15:20