Skip to content

NSAPI_ERROR_NO_CONNECTION when connected to a socket via IP #30

Open
@martinbydefault

Description

@martinbydefault

I'm having this following problem:
When I connect to a socket using a hostname, the call to MQTTClient::connect works correctly.
But if I connect to the socket using the IP, MQTTClient::connect gives me NSAPI_ERROR_NO_CONNECTION.

Here is my code:
Note that iface configuration is not included.

NetworkInterface *iface;
...
nsapi_size_or_error_t rc;
const char *hostname = "broker.hivemq.com";
const char *ip = "18.158.154.65";
const int port = 1883;

const char* topic = "topic/subject";
TCPSocket socket;
SocketAddress a;
MQTTClient client(&socket);
socket.open(iface);
// iface->gethostbyname(hostname, &a);
a.set_ip_address(ip);
a.set_port(port);
socket.connect(a);

MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.MQTTVersion = 3;
data.clientID.cstring = (char *) "mbed-sample";

if ((rc = client.connect(data)) != 0)
    print_function("rc from MQTT connect is %d\r\n", rc);

When I comment a.set_ip_address(ip); and uncomment iface->gethostbyname(hostname, &a); I get no error when doing the MQTT connection, and I can correctly subscribe to a topic for example.

But if I run the code as it is, i get the error: rc from MQTT connect is -3004 where -3004 is NSAPI_ERROR_NO_CONNECTION.

I also checked the logs when connecting to my own MQTT server and got this:

1604583169: New connection from 167.116.30.185 on port 1883.
1604583169: Socket error on client <unknown>, disconnecting

So the connection is created but then some socket error occurs client side.

I even added some debug code to check the socket connection:

rc = socket.open(iface);
if (rc != NSAPI_ERROR_OK) {
    print_function("TCPSocket.open() fails, code: %d\n", rc);
    return -1;
}
...
rc = socket.connect(a);
if (rc < 0) {
    print_function("TCPSocket.connect() fails, code: %d\n", rc);
    return -1;
} else {
    print_function("TCP: connected with %s server\n", socket.get_ip_address());
}

And I got this log:

TCP: connected with <MY-IP> server
Connecting to MQTT...rc from MQTT connect is -3004

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions