コード例 #1
0
ファイル: xsmfns.c プロジェクト: timmartin/remacs
static void
ice_conn_watch_CB (IceConn iceConn, IcePointer clientData,
                   int opening, IcePointer *watchData)
{
  if (! opening)
    {
      ice_connection_closed ();
      return;
    }

  ice_fd = IceConnectionNumber (iceConn);
  add_read_fd (ice_fd, x_session_check_input, NULL);
}
コード例 #2
0
ファイル: select_server.c プロジェクト: IreanLau/select_tcp
int main(int argc,char* argv[])
{
	if(argc != 3 )
	{
		usage(argv[0]);
		exit(1);
	}


	int port = atoi(argv[2]);
	int listen_sock = start(argv[1],port);

	if(listen_sock < 0)
		exit(2);

	select_fd_t select_set;
	init_select_set(&select_set,listen_sock);
	fd_set r_set;//read file fd set

	while(1)
	{
		FD_ZERO(&r_set);
		add_read_fd(&r_set,&select_set); //将参数2 的值添加到参数1中
		struct timeval timeout ={3,500};//fei zuse
		 switch (select(select_set.max_fd+1,&r_set,NULL,NULL,&timeout) )
		 {
			case 0:
				perror("timeout");
				break;
			case -1:
				perror("select");
				break;
			default://success
				{ 
					int i = 0;
					for(;i<_FD_NUM_;++i)
					{
						int fd = select_set.fd_arr[i];
						if(0==i && FD_ISSET(fd,&r_set) )//new connect
						{
							struct sockaddr_in client;
							socklen_t len = sizeof(client);
							int new_fd = accept(fd,(struct sockaddr_in*)&client,&len);

							if(new_fd == -1)
							{
								perror("accept");
								continue;//失败,继续下次循环
							}

							if ( 0 == add_new_fd(&select_set,new_fd) )//add yes
							{
								//do nothing
							}
							else//add failed,array is full
							{
								close(fd);  //资源不足,暂时请求失败
							}
							continue;
						}
						//不是监听sock
						if( fd != _FD_DEFAULT_VAL_ && FD_ISSET(fd,&r_set) )
						{//read 就绪
							if( 0 == read_data_show(fd) )
							{
								delete_new_fd(&select_set,fd);
								close(fd);
							}
							else
							{
								//do nothing
							}
						}
					}
					
				}
				break;
						
		 }

	}

	return 0;
}