예제 #1
0
int initServer(char * servidor,int numPort,int longMax){

	if(daemonizar(servidor)==ERROR){
		printf("Error al daemonizar\n");
		return ERROR;
	}

	if((socketId = openSocket())<0){
		printf("Error al crear el socket\n");
		exit(ERROR);
	}
	if(assignSocket(socketId,numPort)<0){
		printf("Error al asignar el puerto al socket\n");
		exit(ERROR);
	}

	//abrira la cola de procesos
	if(openQueue(socketId,longMax) !=0){
		printf("Error al abrir la cola\n");
		exit(ERROR);
	}
	return OK;
}
예제 #2
0
파일: ssh.c 프로젝트: anatoliy-sh/Ssh
int main (int argc, char *argv[]){
          /*int c;
          FILE *pp;
          extern FILE *popen();
          if ( !(pp=popen("ls -l", "r")) ) 
            return 1;
 
          while ( (c=fgetc(pp)) != EOF ) {
          putc(c, stdout); 
          fflush(pp);
          }   
          pclose(pp);*/

  //пользовательскике данные
  struct UserParams userParams;
  Dict login = DictCreate();
  Dict fLogin = DictCreate();
  Dict location = DictCreate();;
  userParams.login = login;
  userParams.fLogin = fLogin;
  userParams.location = location;

  //параметры адреса
  printf("starting server\n");
  struct addrinfo hints; 
  memset(&hints, 0, sizeof hints);
  hints.ai_flags =  AI_PASSIVE;
  hints.ai_family = AF_UNSPEC; 
  hints.ai_socktype = SOCK_STREAM; 
  hints.ai_protocol = IPPROTO_TCP; 
  
  // получаение списка доступных адресов
  char* port = "8080";
  struct addrinfo* addresses = getAvilableAddresses(&hints, port); 
  if(!addresses)
    return EXIT_FAILURE;
  
  //коннект сокета
  int socketfd = assignSocket(addresses);
  if(!socketfd)
    return EXIT_FAILURE;


  if (setNonBlock(socketfd) == -1){
    perror("non_block");
    return EXIT_FAILURE;
  }


  if (listen (socketfd, SOMAXCONN) == -1){
    perror ("listen");
    return EXIT_FAILURE;
  }
  printf("listening port %s\n", port);
  
  //создание epoll
  int epollfd = epoll_create1(0);
  if (epollfd == -1){
    perror ("epoll_create");
    return EXIT_FAILURE;
  }


  if (addToEpoll(epollfd, socketfd) == -1)
    return EXIT_FAILURE;
  
  //регистрация обработчика событий
  signal(SIGINT, handleSignal);
    
  int maxEventNum = 8;
  struct epoll_event events[maxEventNum * sizeof(struct epoll_event)];
  //заполнение параметров для потоков
  struct Params params;
  pthread_mutex_init(&params.mutex, NULL);
  pthread_cond_init(&params.condvar, NULL); 
  params.end = 0;
  params.currrent = 0;
  params.epollfd = epollfd;
  params.events = events;
  params.socketfd = socketfd;
  params.userParams = userParams;
  pthread_t working[WORKING_THREADS_COUNT]; 
  for(int i = 0; i<WORKING_THREADS_COUNT; i++){   
    pthread_create(&working[i], NULL, workThread, &params);
  } 

  //обработка событий
  int timeout = -1 ;
  while(!done){
    if(params.currrent >= params.end){
      printf("waiting new events\n");
      int eventsNumber = epoll_wait(epollfd, events, maxEventNum, timeout);
      params.currrent = 0;
      params.end = eventsNumber;
      printf("Send\n");
      pthread_cond_signal(&params.condvar); 
    }
  }
  
  printf("server is going down\n");
  printf("closing connections\n");
  close(socketfd);
  close(epollfd);
  printf("done\n");
  return EXIT_SUCCESS;
}