当前位置: 技术问答>linux和unix
高分求教select问题(linux下)
来源: 互联网 发布时间:2015-09-05
本文导语: 不明白在linux下为什么select返回永远是0?请看代码。去掉select判断则可以正常接收数据。 int main(int argc, char **argv) { int sock; int i=0; int timeout_conut=30; fd_set fdRead; struct sockaddr_in serverAddr; struct sockaddr_i...
不明白在linux下为什么select返回永远是0?请看代码。去掉select判断则可以正常接收数据。
int main(int argc, char **argv)
{
int sock;
int i=0;
int timeout_conut=30;
fd_set fdRead;
struct sockaddr_in serverAddr;
struct sockaddr_in s_DstAddr;
memset(&serverAddr,0,sizeof(serverAddr));
memset(&s_DstAddr,0,sizeof(s_DstAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(12345);
serverAddr.sin_addr.s_addr = INADDR_ANY;
if((sock = socket(AF_INET,SOCK_DGRAM,0)) ==-1)
{
printf("socket init failuren");
close(sock);
return 0;
}
int val = 1;
if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val))==-1)
{
printf("SO_REUSEADDR failuren");
return 0;
}
/**/
int iRet;
if(bind(sock,(struct sockaddr *)&serverAddr, sizeof(serverAddr))==-1)
{
printf("bind socket failuren");
close(sock);
return 0;
}
iRet =100*1024;
setsockopt(sock,SOL_SOCKET,SO_RCVBUF,(char *)&iRet,sizeof(iRet));
struct timeval timeout;
timeout.tv_sec=1;
timeout.tv_usec=0;
int nDataRecv=0;
int NameLen=sizeof(struct sockaddr_in);
char recbuffer[300];
memset(recbuffer,0,sizeof(recbuffer));
printf("start recv msgn");
for(i=0;i
select(sock+1,&fdRead,NULL,NULL,&timeout);
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *errorfds, struct timeval *timeout);
The nfds argument specifies the range of file descriptors to
be tested. The select() function tests file descriptors in
the range of 0 to nfds-1.
int main(int argc, char **argv)
{
int sock;
int i=0;
int timeout_conut=30;
fd_set fdRead;
struct sockaddr_in serverAddr;
struct sockaddr_in s_DstAddr;
memset(&serverAddr,0,sizeof(serverAddr));
memset(&s_DstAddr,0,sizeof(s_DstAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(12345);
serverAddr.sin_addr.s_addr = INADDR_ANY;
if((sock = socket(AF_INET,SOCK_DGRAM,0)) ==-1)
{
printf("socket init failuren");
close(sock);
return 0;
}
int val = 1;
if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val))==-1)
{
printf("SO_REUSEADDR failuren");
return 0;
}
/**/
int iRet;
if(bind(sock,(struct sockaddr *)&serverAddr, sizeof(serverAddr))==-1)
{
printf("bind socket failuren");
close(sock);
return 0;
}
iRet =100*1024;
setsockopt(sock,SOL_SOCKET,SO_RCVBUF,(char *)&iRet,sizeof(iRet));
struct timeval timeout;
timeout.tv_sec=1;
timeout.tv_usec=0;
int nDataRecv=0;
int NameLen=sizeof(struct sockaddr_in);
char recbuffer[300];
memset(recbuffer,0,sizeof(recbuffer));
printf("start recv msgn");
for(i=0;i
select(sock+1,&fdRead,NULL,NULL,&timeout);
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *errorfds, struct timeval *timeout);
The nfds argument specifies the range of file descriptors to
be tested. The select() function tests file descriptors in
the range of 0 to nfds-1.
|
select第一个参数是0,当然select不到了。