Arm Mbed and Pelion Device Management support forum

TLS socket connect, none string based function

This produces string based API warning.

    virtual nsapi_error_t connect_socket(char *host, uint16_t port) {
    return ((TLSSocket*)_socket)->connect(host, port);
}

How would I change this for none string based?

Thank you

Paul

you need to construct a SocketAddress first and pass the SocketAddress to the connect():

    virtual nsapi_error_t connect_socket(const char *host, uint16_t port) {
      SocketAddress remote(host, port);
    return ((TLSSocket*)_socket)->connect(&remote);
}
1 Like

Hi @star297
You could also look at the example:

  SocketAddress addr;
    result = net->gethostbyname("ifconfig.io", &addr);
    if (result != NSAPI_ERROR_OK) {
	printf("Error! DNS resolution for ifconfig.io failed with %d\n", result);
    }
    addr.set_port(443);

    TLSSocket *socket = new TLSSocket;
    result = socket->open(net);
    if (result != NSAPI_ERROR_OK) {
        printf("Error! socket->open() returned: %d\n", result);
        return result;
    }

    socket->set_hostname("ifconfig.io");

    result = socket->set_root_ca_cert(cert);
    if (result != NSAPI_ERROR_OK) {
        printf("Error: socket->set_root_ca_cert() returned %d\n", result);
        return result;
    }

    result = socket->connect(addr);
    if (result != NSAPI_ERROR_OK) {
        printf("Error! socket->connect() returned: %d\n", result);
        goto DISCONNECT;
    }

This example, in addition to what was stated correctly by @JojoS, also set the CA root certificate and hostname for verification.
Regards

Thanks guy’s

I did think your snip would work Johannes, but I get this error.

virtual nsapi_error_t connect_socket(const char *host, uint16_t port) {
  SocketAddress remote(host, port);
return ((TLSSocket*)_socket)->connect(&remote);
}

Error: No matching member function for call to ‘connect’

Any suggestions?

BR
Paul

yes, sorry, connect takes a reference, not a pointer:

connect(remote);

hope this works.

Thank you Johannes,

Yes it did work, no warnings, however its impacted on another part that uses that function;

HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, FirebaseUrl);

Error: Allocating an object of abstract class type ‘HttpsRequest’

After quite a long time :yawning_face: I managed to work it out;

Either this…

virtual nsapi_error_t connect_socket(char *host, uint16_t port) {        
        SocketAddress addr;
        addr.set_port(port);    
        return ((TLSSocket*)_socket)->connect(host);
    }    

or this;

virtual nsapi_error_t connect_socket(char *host, uint16_t port) {    
        ((TLSSocket*)_socket)->set_hostname(host);    
        SocketAddress addr;
        addr.set_port(port);    
        return ((TLSSocket*)_socket)->connect(addr);
    } 

I don’t know which method would be best. But as of today I have no warnings :joy:
Lets hope Mr. Takalo doesn’t have any more depreciating ideas! and focus on what is not currently working.

Thank’s to both of you for your help

Regards

Paul