当前位置: 技术问答>linux和unix
关于unix网络编程select函数总是返回0的问题
来源: 互联网 发布时间:2017-02-17
本文导语: 服务器端: int StartListen(sLsnPort) char *sLsnPort; { struct sockaddr_in ServiceAddr; struct sockaddr_in ClientAddr; int nLsnSock = 0; int nAcceptSock = 0; int nPid = 0; int status = 0; int nReady = 0; un...
服务器端:
int StartListen(sLsnPort)
char *sLsnPort;
{
struct sockaddr_in ServiceAddr;
struct sockaddr_in ClientAddr;
int nLsnSock = 0;
int nAcceptSock = 0;
int nPid = 0;
int status = 0;
int nReady = 0;
unsigned int nAddrlen = 0;
char sRecvBuffer[500],sSendBuffer[500];
fd_set socks;
struct timeval recvtime;
int n1 = 0,n2 = 0;
/*创建套接字*/
if((nLsnSock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("Create socket error:%srn",strerror(errno));
return -1;
}
bzero(&ServiceAddr,sizeof(ServiceAddr));
ServiceAddr.sin_family = AF_INET;
ServiceAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
ServiceAddr.sin_port = htons(atoi(sLsnPort)); /*输入的端口号*/
/*绑定套接字*/
if(bind(nLsnSock,(struct sockaddr *)&ServiceAddr,sizeof(ServiceAddr)) 0)
{
kill(SIGTERM, nPid);
}
else
{
break;
}
}
FD_ZERO(&socks); /*清空套接字描述符集*/
FD_SET(nLsnSock,&socks); /*在文件描述符集socks中增加一个新的文件描述符nLsnSock*/
nReady = select(nLsnSock+1,&socks,NULL,NULL,&recvtime); /*阻塞*/
if(nReady
int StartListen(sLsnPort)
char *sLsnPort;
{
struct sockaddr_in ServiceAddr;
struct sockaddr_in ClientAddr;
int nLsnSock = 0;
int nAcceptSock = 0;
int nPid = 0;
int status = 0;
int nReady = 0;
unsigned int nAddrlen = 0;
char sRecvBuffer[500],sSendBuffer[500];
fd_set socks;
struct timeval recvtime;
int n1 = 0,n2 = 0;
/*创建套接字*/
if((nLsnSock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("Create socket error:%srn",strerror(errno));
return -1;
}
bzero(&ServiceAddr,sizeof(ServiceAddr));
ServiceAddr.sin_family = AF_INET;
ServiceAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
ServiceAddr.sin_port = htons(atoi(sLsnPort)); /*输入的端口号*/
/*绑定套接字*/
if(bind(nLsnSock,(struct sockaddr *)&ServiceAddr,sizeof(ServiceAddr)) 0)
{
kill(SIGTERM, nPid);
}
else
{
break;
}
}
FD_ZERO(&socks); /*清空套接字描述符集*/
FD_SET(nLsnSock,&socks); /*在文件描述符集socks中增加一个新的文件描述符nLsnSock*/
nReady = select(nLsnSock+1,&socks,NULL,NULL,&recvtime); /*阻塞*/
if(nReady