示例#1
0
void ServPanelist ( Connection panelist, struct sockaddr_in client)
{
    int psd, index=0;
    pid_t childpid;
    socklen_t t = sizeof(panelist.server);
 
    for(;;){
        psd  = accept(panelist.socket_s, (struct sockaddr *) &client, &t);
        EchoServe(psd, client, GETUSERNAME);
        childpid = fork();
        if ( childpid == 0) {
			signal (SIGQUIT, SIG_IGN);
			signal (SIGINT, SIG_IGN);		
			signal (SIGTSTP,SIG_IGN); 

            if (signal(SIGUSR1, displayHandle) == SIG_ERR) {
                fprintf(stderr, "cannot set handler for SIGUSR1\n");
            }

            if (signal(SIGUSR2, SendMessage) == SIG_ERR) {
                fprintf(stderr, "cannot set handler for SIGUSR2\n");
            }

            close (panelist.socket_s);
            EchoServe(psd, client, SERVPANEL);
        }
        else{
            
			signal (SIGQUIT, SIG_IGN);
			signal (SIGINT, SIG_IGN);		
			signal (SIGTSTP,SIG_IGN); 

            cout << "\nNew Panel Client: (" << inet_ntoa(client.sin_addr) << ":" << ntohs(client.sin_port) <<
                    ") [" << client_msg << "]" << endl;
            cout << "Forked Panel Handler, PID = " << childpid << endl;
			
			*sharedInt_index = *sharedInt_index + 1 ;
			index = *sharedInt_index;
			
			//cout << "new parti " << index <<endl;
			list_participants[index].client_type = PARTICIPANT_PAN;
			list_participants[index].pid = childpid;
			list_participants[index].port = client.sin_port;
			list_participants[index].address = client.sin_addr;
			list_participants[index].buf_stream = psd;
			strcpy(list_participants[index].user_name, client_msg);		
			// current date/time based on current system
			list_participants[index].time_in = time (NULL);			 
        }
    }
}
示例#2
0
void ServAudience ( Connection audience, struct sockaddr_in client)
{
    int psd, childpid, index=0;
    socklen_t t = sizeof(audience.server);
	
	    
    for(;;){
        psd  = accept(audience.socket_s, (struct sockaddr *) &client, &t);
        EchoServe(psd, client, GETUSERNAME);
        childpid = fork();
        if ( childpid == 0) {
			signal (SIGQUIT, SIG_IGN);
			signal (SIGINT, SIG_IGN);		
			signal (SIGTSTP,SIG_IGN); 
	       if (signal(SIGUSR1, displayHandle) == SIG_ERR) {
                fprintf(stderr, "cannot set handler for SIGUSR1\n");
            }
            // getting interrupt to send an audience message
            if (signal(SIGUSR2, SendMessage) == SIG_ERR) {
                fprintf(stderr, "cannot set handler for SIGUSR2\n");
            }
            close (audience.socket_s);
            EchoServe(psd, client, SERVAUDIENCE);
        }
        else{
			
			signal (SIGQUIT, SIG_IGN);
			signal (SIGINT, SIG_IGN);		
			signal (SIGTSTP,SIG_IGN); 
			
	 		cout << "\nNew AUDIENCE Client: (" << inet_ntoa(client.sin_addr) << ":" << ntohs(client.sin_port) <<
                    ") [" << client_msg << "]" << endl;
            cout << "Forked AUDIENCE Handler, PID = " << childpid << endl;
			
			*sharedInt_index = *sharedInt_index + 1 ;
			index = *sharedInt_index;
			//cout << "new parti " << index <<endl;
			list_participants[index].client_type = PARTICIPANT_AUD;
			list_participants[index].pid = childpid;
			list_participants[index].port = client.sin_port;
			list_participants[index].address = client.sin_addr;
			list_participants[index].buf_stream = psd;
			strcpy(list_participants[index].user_name, client_msg);
			list_participants[index].time_in = time (NULL);
        }
    }
}
示例#3
0
文件: serverselect.c 项目: mtulio/kb
main()
{
   fd_set read_template;//conjunto de descritores de arquivos
   struct timeval wait;//arg do select, diz quanto esperar
		      //para que algum descritor fique pronto	
   struct   sockaddr_in server;
   char   buf[102];
   int   sock, length, s1,s2, nb, cc, ready,eof1,eof2;

//Cria socket 
   sock = socket (PF_INET,SOCK_STREAM,0);
   if ( sock < 0 ) {
      perror("Erro na execucao da chamada socket()");
      exit(0);
   }

//preenche o struct de enderecos PF_INET
//faz o Bind
   server.sin_family = AF_INET;
   server.sin_addr.s_addr = INADDR_ANY;
   server.sin_port = 0;
   if ( bind (sock,&server,sizeof(server)) ) {
      perror("Erro na execucao da chamada bind()");
   }
// Usa a chamada getsockname para descobrir o n. de porta
//associado, haja visto que, server.sin_port = 0
   length = sizeof(server);
   if ( getsockname (sock,&server,&length) ) {
      perror("Erro na execucao da chamada getsockname()");
      exit(0);
   }
   printf("SERVIDOR EXECUTANDO: aguardando conexoes dos clientes\n");
   printf("Numero da porta usada pelo servidor: #%d\n", ntohs(server.sin_port));
   
//inicia a aceitacao de conexoes
//note que existem duas execucoes do accept
//ou seja, no maximo dois sockets de cliente
//poderam ser manipulados pelo processo gerado por srverselect.c

   listen (sock, 5);
   s1= accept(sock,0,0);
   eof1=0;//testa status da conexao
   printf("Cliente 1 conectado\n");
   s2 = accept(sock,0,0);
   eof2=0;
   printf("Cliente 2 conectado\n");
   wait.tv_sec = 1;//struct de tempo duracao da selecao em seg
   wait.tv_usec = 0;//microseg
   //se timeout = 0 situacao de polling
   do {
      FD_ZERO(&read_template);//MACRO que limpa o fdset
      if (!eof1) FD_SET(s1,&read_template);//liga s1 no fdset e testa STATUS da conexao 
      if (!eof2) FD_SET(s2,&read_template);//liga s2 no fdset e testa STATUS da conexao

//executa o select ems1 e s2
//select aguarda que um dos dois mude o STATUS
 nb = select(FD_SETSIZE, &read_template, (fd_set *) 0, (fd_set *) 0, &wait);
// select (tamanho de fdset, ponteiro para leitura de dados, escrita de dados
// ,excecao, timeout);//excecoes: apenas dados outband em sockets
//caso nao queira testar nada e so passar ponteiro nulo.
//sucesso nb = numero de descritores aptos no fdset 
//se retornar = 0 foi devido ao timeout
      if (nb <0) {
         perror("Erro na chamada select()");
         exit(1);
      }
      if (nb != 0) {      /* 0 time out */
         if(FD_ISSET(s1, &read_template)){//testa a existencia de s1 com menbro do fdset
		EchoServe(s1, &eof1);//responde para s1
         }
         if(FD_ISSET(s2, &read_template)){//faz polling s2
		EchoServe(s2, &eof2);//responde s2
         }

      }
   }   while ( !eof1 || !eof2 );
   printf("Exit : finalizando programa\n");//somente se nao tiver nenhuma conexao ativa
   close(s1);
   close(s2);
   exit(0);
}
示例#4
0
void ServAudience ( Connection audience, struct sockaddr_in client)
{
    int psd, childpid, index=0;
    socklen_t t = sizeof(audience.server);
	int *status;
	
    for(;;){
		psd  = accept(audience.socket_s, (struct sockaddr *) &client, &t);
        EchoServe(psd, client, GETUSERNAME);
        childpid = fork();
        if ( childpid == 0) {
			signal (SIGTSTP,SIG_IGN);
			if (signal(SIGUSR1, displayHandle) == SIG_ERR) {
                fprintf(stderr, "cannot set handler for SIGUSR1\n");
            }
            
			// getting interrupt to send an audience message
            if (signal(SIGUSR2, SendMessage) == SIG_ERR) {
                fprintf(stderr, "cannot set handler for SIGUSR2\n");
            }
            
			close (audience.socket_s);
            EchoServe(psd, client, SERVAUDIENCE);
			
        }
        else{
			signal (SIGTSTP,SIG_IGN);
			cout << "\nNew audience Client: (" << inet_ntoa(client.sin_addr) << ":" << ntohs(client.sin_port) <<
                    ") [" << client_msg << "]" << endl;
            cout << "Forked audience Handler, PID = " << childpid << endl;
			*sharedInt_index = *sharedInt_index + 1 ;
				index = *sharedInt_index;
				
				//cout << "new parti " << index <<endl;
				list_participants[index].client_type = PARTICIPANT_AUD;
				list_participants[index].pid = childpid;
				list_participants[index].port = client.sin_port;
				list_participants[index].address = client.sin_addr;
				list_participants[index].buf_stream = psd;
				strcpy(list_participants[index].user_name, client_msg);		
				// current date/time based on current system
				list_participants[index].time_in = time (NULL);	
				
				//if Q&A session opened send a message
				if (*QAsessionFlag == 0)
				{
					sprintf(client_msg,"%s", "We are in Q&A session now!");
					//int signal_sent = kill(list_participants[index].pid , SIGUSR2);
				}
				
		
        }
	
		//Handle the child signal just end. Meaning of someone left the room. Pipe broken.
		int pid=wait(&status);	
		//#define PARTICIPANT_AUD    	   1
		//#define PARTICIPANT_PAN        2
		//checkParticipant(index,PARTICIPANT_AUD);
	 
    }
}