TCPSocket connect() always gives -3001 (NSAPI_ERROR_WOULD_BLOCK)

I’m trying to use a normal TCPSocket to connect to a server and download a simple file. I can create the socket just fine and open it, but connecting always fails with -3001 no matter what I do. The error means “would block”, but I don’t make the socket nonblocking anywhere, I even explicitly set it to be blocking. I do use other socket elsewhere and these are also non blocking, so could there be some kind of “leak” where settings leak from one socket to a totally separate instance?

    NetworkInterface* net = NetworkInterface::get_default_instance();
    if (!net) {
        // no network interface
        ...
    }

    TCPSocket *socket = new TCPSocket();

    nsapi_error_t result = socket->open(net);
    if (result != 0) {
        // init failure
        ....
    }

    socket->set_blocking(true);

    result = socket->connect(SocketAddress("192.168.1.109", 1081));
    if (result != 0) {
        // connect error. This is always -3001 
        ....
    }

The error codes are:

num nsapi_error {
    NSAPI_ERROR_OK                  =  0,        /*!< no error */
    NSAPI_ERROR_WOULD_BLOCK         = -3001,     /*!< no data is not available but call is non-blocking */
...

Hello,

I miss this call


nsapi_error_t status = net->connect();

And this could be also handy

void net_status_cb(nsapi_event_t status, intptr_t param)
{
    debug_if(DEBUG,"Connection status - ");
    switch (param) {
        case NSAPI_STATUS_CONNECTING:
            debug_if(DEBUG,"Connecting...\n");
            break;
        case NSAPI_STATUS_LOCAL_UP:
            debug_if(DEBUG,"Local IP address set!\n");
            break;
        case NSAPI_STATUS_GLOBAL_UP:
            debug_if(DEBUG,"Global IP address set!\n");
            break;
        case NSAPI_STATUS_DISCONNECTED:
            debug_if(DEBUG,"No connection to network!\n");
            break;
        default:
            debug_if(DEBUG,"Not supported!\n");
            break;
    }
}

// the function above need to be attached
net->attach(&net_status_cb);

BR, Jan

My code snippet was not complete. That net->connect() call is done during startup. The stack is up and running at this point. The main method of logging for instance is via UDP and the main controlling API is also on UDP (due to TCP being so unstable).