select 一般用在多线程中的阻塞检测一定的动作发生后执行的,如果只是在两个函数中,func1(select();) func2(select();)如果这两个函数依次调用,则先阻塞第一个函数,然后再次阻塞第二个函数。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/poll.h>
#include <linux/soundcard.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>
/*--------------------------------------------------------------------------------------------*/
void CTcpServer::findMaxClientsockId()
{
CycleList *pList=CmdSockList.GetHead();
int maxid=0;
maxid=m_ServerSockId;
while(pList!=NULL)
{
if(pList->lSocket>=maxid)
{
maxid=pList->lSocket;
}
pList=CmdSockList.GetNext(pList);
}
m_maxClientsockId = maxid;
}
extern int gTcpSvrQuit;
int CTcpServer:: CreateTcpServer()
{
fd_set acp_fdsr; //文件描述符集的定义
struct timeval tv;
m_CurrentConnCnt =0;
sin_size = sizeof(client_addr);
m_maxClientsockId = m_ServerSockId;
CycleList *pList=CmdSockList.GetHead();
while(gTcpSvrQuit)
{
FD_ZERO(&fdsr);/// acp_fdsr
FD_SET(m_ServerSockId,&fdsr);
tv.tv_sec = 30;
tv.tv_usec =0;
pList=CmdSockList.GetHead();
while(pList!=NULL)
{
FD_SET(pList->lSocket,&fdsr);
/// printf("lSocket is %d\n",pList->lSocket);
pList=CmdSockList.GetNext(pList);
}
ret = select(m_maxClientsockId +1,&fdsr,NULL,NULL,&tv);
if(ret <0)
{
perror("select error!\n");
break;
}
else if(ret ==0)
{
/// printf("timeout \n");
continue;
}
while(pList!=NULL)
{
if(FD_ISSET(pList->lSocket,&fdsr))
{
}
pList=pList->lNext;
}
if(FD_ISSET(m_ServerSockId,&fdsr))
{
new_fd = accept(m_ServerSockId,(struct sockaddr *)&client_addr,&sin_size);
if(new_fd <=0)
{
perror("accept error::");
continue;
}
}
}
return 0;
}
以后增加。。。