Search code examples
cmacossocketsnetwork-programmingsendto

sendto returning EINVAL on Mac OS X 10.8


I had a program that worked properly with Mac OS 10.6 but inexplicably fails in 10.8. The gist of it is that sendto is now returning -1 and setting errno to EINVAL (22). What's going wrong?


Solution

  • The address I was giving sendto came from the first result of getaddrinfo. Turns out that first result is now an IPV6 result (using sockaddr_in6). sendto on OS 10.8 (at least for now) only seems to work with sockaddr_in address. Make sure to pass a hint to getaddrinfo that says you only want ipv4 addresses, i.e.

    struct addrinfo hint;
    memset( &hint, 0, sizeof(struct addrinfo));
    hint.ai_family = AF_INET;
    
    struct addrinfo* result;
    int res = getaddrinfo( friendlyHostname, NULL, &hint, &result );
    

    Read http://linux.die.net/man/3/getaddrinfo for more info.