Esempio n. 1
0
int main(int argc, char *argv[]) {
	int server_sock = 0;
	
	int client1_sock = 0;
	int client2_sock = 0;

	if(argc == 2 && strcmp("-d", argv[1]) == 0) {
		printf("Running in debug mode.\n");
		debug = 1;
	}

	signal(SIGCHLD, reap_terminated_child);

	//set up the server
	dprintf("Initializing server.\n");
	server_sock = get_server_socket(HOST, HTTPPORT);
	
	if(start_server(server_sock, BACKLOG) == -1) {
		printf("Error starting server: %s.\n", strerror(errno));
		exit(1);
	}

	while(1) {
		//client 1 has already connected to the server
		if(client1_sock != 0) {
			client2_sock = accept_client(server_sock);
			dprintf("Received connection from second client.\n");
			
			//fork for subserver
			if (!fork()) { // child process, so start the subserver
			
			   close(server_sock); //no longer needed in child process
			   dprintf("Preparing to play.\n");
			   subserver(client1_sock, client2_sock);
			   
			} else { //parent process
			
				//reset client sockets for more connections
			   close(client1_sock);
			   close(client2_sock);
			   client1_sock = 0;
			   client2_sock = 0;
			}
			
		} else { //client 1 has not connected yet
			client1_sock = accept_client(server_sock);
			dprintf("Received connection from first client.\n");

			char msg = P_WAIT; //tell the client just to wait
			if(send(client1_sock, &msg, sizeof(msg), 0) < 0) {
				printf("Unable to send: %s\n", strerror(errno));
				exit(1);
			}
		}
	}
}
Esempio n. 2
0
File: init.c Progetto: kyeah/VizChat
int main(){
  SDL_Init(SDL_INIT_EVERYTHING);
  signal(SIGINT, sighandler);

  Room *rooms;
  SDL_sem **semaphores;
  int* rooms_opened; //= (int*)malloc(sizeof(int));
  
  int rooms_id = shmget(68476, sizeof(Room) * 10, 0666 | IPC_CREAT);
  int sem_id = shmget(85938, sizeof(SDL_sem*) * 10, 0666 | IPC_CREAT);

  int rooms_opened_id = shmget(75286, sizeof(int), 0666 | IPC_CREAT);
  //Should have a semaphore for this int

  rooms = (Room*)shmat(rooms_id, NULL, 0);
  semaphores = (SDL_sem**)shmat(sem_id, NULL, 0);
  rooms_opened = (int*)shmat(rooms_opened_id, NULL, 0);

  *rooms_opened = 0;

  int i, b, socket_client;
  char buffer[256];  

  //SELECT variables
  fd_set readers;
  struct timeval timeout;
  timeout.tv_sec = 0;
  timeout.tv_usec = 0;  


  /*------------------
    SERVER PREPARATION
    ------------------*/

  //SOCKET variables
  struct sockaddr_in server;
  socklen_t socket_length;

  //make the server socket for reliable IPv4 traffic 
  socket_id = socket( AF_INET, SOCK_STREAM, 0);
  printf("Socket file descriptor: %d\n", socket_id);

  //set up the server socket struct
  //Use IPv4 
  server.sin_family = AF_INET;

  //This is the server, so it will listen to anything coming into the host computer
  server.sin_addr.s_addr = INADDR_ANY;
  
  //set the port to listen on, htons converts the port number to network format
  server.sin_port = htons(24601);
  
  //bind the socket to the socket struct
  i= bind( socket_id, (struct sockaddr *)&server, sizeof(server) );
  //wait for any connection
  i =  listen( socket_id, 1 );

  /*---------------
    SERVER IS READY
    ---------------*/


  //Add rooms automatically because I always forget to when testing
  addRoom(rooms, semaphores, *rooms_opened);
  *rooms_opened += 1;
  addRoom(rooms, semaphores, *rooms_opened);
  *rooms_opened += 1;


  /*-------------------------------
    ACCEPT CONNECTIONS CONTINUOUSLY
    -------------------------------*/

  while(1) {
    FD_ZERO(&readers);
    FD_SET(STDIN_FILENO, &readers);
    FD_SET(socket_id, &readers);

    printf("Accepting a connection. Options: [add room] [exit]\n");
    select(socket_id + 1, &readers, 0, 0, 0);


    /*-----------
      USER INPUT
      ----------*/

    //The user has ordered us to do something on the server! Oh boy!
    if(FD_ISSET(STDIN_FILENO, &readers)){
      fgets(buffer, sizeof(buffer), stdin);

      //Add room
      if(!strncmp(buffer, "add room", 8)){
	if(*rooms_opened < 10){
	  addRoom(rooms, semaphores, *rooms_opened);
	  *rooms_opened += 1;
	}
	else
	  puts("Maximum number of rooms has been reached.\n");
      }
      
      //Exit
      else if(!strncmp(buffer, "exit", 4)){
	close(socket_id);

	for(i = 0; i < *rooms_opened; i++)
	  SDL_DestroySemaphore(semaphores[i]);

	SDL_Quit();
	break;
      }
    }

    /*-----------------
      SOCKET CONNECTION
      -----------------*/

    //Somebody wants to connect! Alright!
    
    if(FD_ISSET(socket_id, &readers)){      
    
      //set socket_length after the connection is made
      socket_length = sizeof(server); 
      
      //accept the incoming connection, create a new file desciprtor for the socket to the client
      socket_client = accept(socket_id, (struct sockaddr *)&server, &socket_length);
      printf("accepted connection %d\n\n",socket_client);
      
      //Fork off a subserver and close the client off from the main server
      i = fork();
      if ( i == 0 ) {
	subserver(rooms, semaphores, socket_client, rooms_opened);
      }
      else 
	close(socket_client);
      
      
    }
  }

  return 1;
}