2
votes

I am writing a program in C that uses winsock and I am using the command fcntl to make the receive call nonblocking and I am getting the following errors.

warning C4013: 'fcntl' undefined; assuming extern returning int 
error C2065: 'F_SETFL' : undeclared identifier
error C2065: 'F_GETFL' : undeclared identifier
error C2065: 'F_SETFL' : undeclared identifier
error C2065: 'O_NDELAY' : undeclared identifier
error C2065: 'EWOULDBLOCK' : undeclared identifierenter code here

I am including the winsock2.h header file in my code as follows

#pragma comment(lib,"ws2_32.lib")
#include <winsock2.h>

Please help me out. Thanks in advance.

1

1 Answers

7
votes

I think on Windows you need to use ioctlsocket rather than fcntl().

To make non-blocking:

unsigned long on = 1;
if (0 != ioctlsocket(socket_fd, FIONBIO, &on))
{
    /* Handle failure. */
}

To make blocking:

unsigned long off = 0;
if (0 != ioctlsocket(socket_fd, FIONBIO, &off))
{
    /* Handle failure. */
}

Instead of EWOULDBLOCK use WSAEWOULDBLOCK.