在c中使用winsock进行套接字编程

use*_*254 2 c winsock winsock2

我在C中编写一个使用winsock的程序,我使用命令fcntl使接收调用非阻塞,我收到以下错误.

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
Run Code Online (Sandbox Code Playgroud)

我在我的代码中包含winsock2.h头文件,如下所示

#pragma comment(lib,"ws2_32.lib")
#include <winsock2.h>
Run Code Online (Sandbox Code Playgroud)

请帮帮我.提前致谢.

hmj*_*mjd 7

我认为在Windows上你需要使用ioctlsocket而不是fcntl().

要进行非阻塞:

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

要阻止:

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

而不是EWOULDBLOCK使用WSAEWOULDBLOCK.