- 论坛徽章:
- 1
|
回复 8# lxyscls - int clientArray[256];
- int clientConnected;
- int listenFd ,clientFd;
- fd_set fds;
- listenFd = socket_listen(socketFile);
-
- while(1){
- FD_ZERO(&fds);
- FD_SET(listenFd, &fds);
- maxfdsn = listenFd;
-
- for( i=0;i<clientConnected;i++ ){
- FD_SET(clientArray[i], &fds);
- if( clientArray[i] > maxfdsn )
- maxfdsn = clientArray[i];
- }
-
- ret = select(maxfdsn+1, &fds, NULL, NULL, NULL);
- if(ret<0){
- //*********
- for (i = 0; i < clientConnected; i++) {
- if (FD_ISSET(clientArray[i], &fds)) {
- //返回-1的时候可读事件没有触发
- memset(&data,0,sizeof( jss_monitor_t ));
- recvLen = read(clientArray[i],&data,sizeof( jss_monitor_t));
- sprintf(sysBuf, "select return error fd %d len {%d} \n",clientArray[i],recvLen);
- write(logFd,sysBuf,strlen(sysBuf));
- }
- }
- continue;
- }
-
- if(FD_ISSET(listenFd,&fds)){
- memset(&un,0,sizeof(struct sockaddr_un));
- addrLen = sizeof(struct sockaddr_un);
- clientFd = accept(listenFd,(struct sockaddr *)&un, (socklen_t *)&addrLen);
- if (clientFd <= 0) {
- continue;
- }
-
- setsockopt(clientFd ,SOL_SOCKET,SO_RCVTIMEO,&timeout,sizeof(timeout));
- //add to overlook array
- clientArray[clientConnected] = clientFd;
- clientConnected++;
- }
- // check every client fd in the set
- for (i = 0; i < clientConnected; i++) {
- if (FD_ISSET(clientArray[i], &fds)) {
- memset(&data,0,sizeof( jss_monitor_t ));
- recvLen = read(clientArray[i],&data,sizeof( jss_monitor_t));
-
- if (recvLen <= 0) {
- FD_CLR(clientArray[i], &fds);
- close(clientArray[i]);
- for( j=i;j<clientConnected;j++){
- clientArray[ j ]=clientArray[ j+1 ];
- }
- close(clientArray[i]);
- clientConnected--;
- break;
- }
- }//end while read
- }
-
- }// end death loop
复制代码 |
|