5

I tried to create a UDP socket on mingw, but socket() returns -1, with errno = 0. Strange. I have included winsock2.h. Initially I had compilation error undefined reference to socket@12, after setting -lws2_32 and -lwsock32 to Linker Settings of Code::Block, compilation success.

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
RDF_LOG(kDEBUG, "sockfd %d ", sockfd);
if (sockfd < 0){
    RDF_LOG(kERROR, "ERROR: %s , errno %d\n", strerror(errno), errno);
}

Result --> sockfd -1 ERROR: No error , errno 0


OK, I change RDF_LOG to fprintf instead.

int tmp = 0;

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
tmp = errno;
fprintf(stderr, "sockfd %d ", sockfd);
if (sockfd < 0){
    fprintf(stderr, "socket: %s , errno %d\n", strerror(tmp), tmp);
}

The result returned is, still, --> sockfd -1 socket: No error , errno 0 Is it possible that mingw does not support errno??

Ahmed Can Unbay
  • 2,694
  • 2
  • 16
  • 33
twfx
  • 1,664
  • 9
  • 29
  • 56

4 Answers4

8

The first thing I would do is this:

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
int tmp = errno;
RDF_LOG(kDEBUG, "sockfd %d ", sockfd);
if (sockfd < 0){
    RDF_LOG(kERROR, "ERROR: %s , errno %d\n", strerror(tmp), tmp);
}

I have no idea what RDF_LOG may be doing to the errno variable and this will tell you whether it changes it or not.

Another thing to look for is that you have successfully performed your WSAStartup. The following minimal program should hopefully show you how to do this, and provide a starting point for debugging:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <winsock2.h>

int main (void) {
    WSADATA wsaData;
    int listenFd;

    if (WSAStartup(MAKEWORD(1,1), &wsaData) == SOCKET_ERROR) {
        printf ("Error initialising WSA.\n");
        return -1;
    }

    listenFd = socket (AF_INET, SOCK_STREAM, 0);
    if (listenFd  < 0) {
        printf ("Error %d opening socket.\n", errno);
        return -1;
    }

    return 0;
}
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
7

Because you are working with Windows sockets, you will need to use the WSAGetLastError() function to check the error code. Windows sockets functions do not set errno, which is why you are seeing errno with a value of 0.

This old MinGW wiki page has a list of differences between UNIX sockets and Windows sockets, mentioning the errno vs WSAGetLastError() difference in item 5: http://oldwiki.mingw.org/index.php/sockets

dgraves
  • 351
  • 1
  • 2
  • 4
3

The function/macro RDF_LOG is likely calling some other function in the C runtime library that is resetting errno to 0. You need to capture the value of errno immediately after socket fails in order for it to be accurate.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
0

Didn't see this mentioned in another answer, but there's another problem with the code above. Winsock socket descriptors are of type SOCKET, which in my MinGW winsock2.h is defined as unsigned int.

If you are assuming Winsock sockets are of type int like Unix file descriptors, checking for a negative error return status may lead to false error reporting, since Winsock makes no guarantee that a socket descriptor value will map to a positive signed integer value.

Winsock functions that return a socket descriptor (socket(), accept(), etc.) instead return SOCKET_INVALID on an error, which is defined as (SOCKET) ~0. As mentioned above, you should then use WSAGetLastError() to get the system error number.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms740516%28v=vs.85%29.aspx

rkuczwara
  • 1
  • 1