请问socket中select函数的用法是怎样的?它通常是用在什么地方?
int select(
int nfds,
fd_set FAR *readfds,
fd_set FAR *writefds,
fd_set FAR *exceptfds,
const struct timeval FAR *timeout
);
它的每个参数又是怎样呢?请大虾们能不能详细地说一下select函数的问题!!!!
谢谢
int select(
int nfds,
fd_set FAR *readfds,
fd_set FAR *writefds,
fd_set FAR *exceptfds,
const struct timeval FAR *timeout
);
它的每个参数又是怎样呢?请大虾们能不能详细地说一下select函数的问题!!!!
谢谢
The Windows Sockets select function determines the status of one or more sockets, waiting if necessary, to perform synchronous I/O.int select (
int nfds,
fd_set FAR * readfds,
fd_set FAR * writefds,
fd_set FAR * exceptfds,
const struct timeval FAR * timeout
);
Parameters
nfds
[in] This parameter is ignored; it is included only for compatibility with Berkeley sockets.
readfds
[in/out] An optional pointer to a set of sockets to be checked for readability.
writefds
[in/out] An optional pointer to a set of sockets to be checked for writability
exceptfds
[in/out] An optional pointer to a set of sockets to be checked for errors.
timeout
[in] The maximum time for select to wait, or NULL for blocking operation. Return Values
The select function returns the total number of socket handles that are ready and contained in the FD_SET structures, zero if the time limit expired, or SOCKET_ERROR if an error occurred. If the return value is SOCKET_ERROR, WSAGetLastError can be used to retrieve a specific error code.Error Codes
WSANOTINITIALISED
A successful WSAStartup must occur before using this function.
WSAEFAULT The Windows Sockets implementation was unable to allocate needed resources for its internal operations, or the readfds, writefds, exceptfds, or timeval parameters are not part of the user address space. WSAENETDOWN The network subsystem has failed. WSAEINVAL The timeout value is not valid, or all three descriptor parameters were NULL. WSAEINTR A blocking Windows Socket 1.1 call was canceled through WSACancelBlockingCall. WSAEINPROGRESS A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function. WSAENOTSOCK One of the descriptor sets contains an entry that is not a socket.
可以用recieve,recievefrom,或者响应Onrecieve函数利用串行化读数据
给你个具体的调用:
struct timeval stime;
struct fd_set sdfds;
stime.tv_sec=1000;
stime.tv_usec=0;l_iFlag=select(sendSocket,(fd_set *)NULL,&sdfds,(fd_set *)NULL,(struct timeval *) &stime);不过这之前你要调用诸如bind、listen、accept之类的函数