linux__select__操作

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;
 }
以后增加。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值