2012-03-02 98 views
2

我正在使用winsock編寫一個C程序,我使用命令fcntl使接收調用非阻塞,並且出現以下錯誤。使用winsock進行套接字編程c

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 

我,包括我的代碼winsock2.h頭文件如下

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

請幫助我。 在此先感謝。

回答

7

我認爲在Windows上你需要使用ioctlsocket而不是fcntl()

要使非阻塞:

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

爲了阻止:

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

代替EWOULDBLOCK使用WSAEWOULDBLOCK