简体   繁体   English

使用Winsock在C中进行套接字编程

[英]Using winsock for socket programming in c

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. 我正在用Winsock编写用C编写的程序,并且正在使用命令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

I am including the winsock2.h header file in my code as follows 我在代码中包含winsock2.h头文件,如下所示

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

Please help me out. 请帮帮我。 Thanks in advance. 提前致谢。

I think on Windows you need to use ioctlsocket rather than fcntl() . 我认为在Windows上,您需要使用ioctlsocket而不是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 . 代替EWOULDBLOCK使用WSAEWOULDBLOCK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM