Berkeley sockets: connect() returns -1 with errno set to ENOENT

1.4k views Asked by At

I have this code:

bool CBSocketConnect(uint64_t socketID,uint8_t * IP,bool IPv6,uint16_t port){
    // Create sockaddr_in6 information for a IPv6 address
    int res;
    if (IPv6) {
        struct sockaddr_in6 address;
        memset(&address, 0, sizeof(address)); // Clear structure.
        address.sin6_family = AF_INET6;
        memcpy(&address.sin6_addr, IP, 16); // Move IP address into place.
        address.sin6_port = htons(port); // Port number to network order
        res = connect((evutil_socket_t)socketID, (struct sockaddr *)&address, sizeof(address));
    }else{
        struct sockaddr_in address;
        memset(&address, 0, sizeof(address)); // Clear structure.
        address.sin_family = AF_INET;
        memcpy(&address.sin_addr, IP + 12, 4); // Move IP address into place. Last 4 bytes for IPv4.
        address.sin_port = htons(port); // Port number to network order
        res = connect((evutil_socket_t)socketID, (struct sockaddr *)&address, sizeof(address));
    }
    if (NOT res || errno == EINPROGRESS)
        return true;
    return false;
}

With IPv6 set to false, the IP set to ::ffff:127.0.0.1 (IPv4 loopback address) and the port number set to 45562, res is set to -1 and errno is set to ENOENT (2). Why would this be?

The platform I am on is OSX Mountain Lion. I'm using the sockets with libevent version "2.0.19-stable".

Thank you.

1

There are 1 answers

0
Matthew Mitchell On BEST ANSWER

I found the problem: LLDB

LLDB was telling me errno was ENOENT but when I use GDB (As I clearly should) it told me errno was EINPROGRESS! It was OK all along. It was all the debugger's fault.

Lesson: Use GDB and never LLDB.