Esempio n. 1
0
int main(int argc, char** argv) {
	// create server
	int servsock;
	struct sockaddr_in servaddr;
	unsigned short port = argc >= 2 ? strtoport(argv[PORTNUM]) : DEFPORT;
	createserver(&servsock, &servaddr, port);

	printf("Server running on port %u.\n", port);
	// server will begin accepting clients again after each game ends
	for (EVER) {
		puts("\nWaiting for players to connect...");

		// wait for players to connect, then assign appropriate values
		int sockarr[NUMPLAYERS];
		struct sockaddr_in addrarr[NUMPLAYERS];
		waitforplayers(servsock, sockarr, addrarr);

		// parent will close sockets and wait for other players to connect
		// child will serve the current game and then terminate
		pid_t pid = fork();
		if (pid < 0) {
			exitwerror("fork", EXIT_ERRNO);
		} else if (pid > 0) {
			int i;
			for (i = 0; i < NUMPLAYERS; ++i) close(sockarr[i]);
			continue;
		}

		// notify players of their player numbers
		sendplayernums(sockarr);

		// inital direction values
		char dirbuffer[SC_STDSIZE];
		dirbuffer[PLAYER_1] = RIGHT;
		dirbuffer[PLAYER_2] = LEFT;

		// keep recieving and sending until a collision signal is recieved
		int collided = -1;
		while (collided == -1) {
			sendvars(sockarr, dirbuffer);				// send directions
			collided = recvclientsignal(dirbuffer, sockarr);	// recieve client information coming back to server socket
		}

		// determine winner
		char winner;
		switch (collided) {
			case PLAYER_1: winner = PLAYER_2; break;
			case PLAYER_2: winner = PLAYER_1; break;
			default: winner = 3;
		}

		// game over, notify clients
		endclients(sockarr, winner);
		break;
	}

	return EXIT_SUCCESS;
}
Esempio n. 2
0
int main(int argc, char *argv[])
{
  socket_t servsock;
  int maxfd;
  fd_set readfds;
  fd_set writefds;
  struct timeval seltimeout;
  int running = 1;

  /* initially we support only one client */
  socket_t clientsock = SOCKET_BAD;

  (void)argc;
  (void)argv;

  /* Create port socket */
  servsock = createserver(SERVER_PORT);

  /* Initialize maxfd for use by select() */
  maxfd = servsock;

  while (running) {
    int rc;
    FD_ZERO(&readfds);
    FD_ZERO(&writefds);
    FD_SET(STDIN_FILENO, &readfds);

    /* right now we only allow one client to connect, so when there's a client
       we don't listen for new connects */
    if (SOCKET_BAD != clientsock) {
      FD_SET(clientsock, &readfds);

      /* if there is anything to write, wait to send */
      FD_SET(clientsock, &writefds);
    }
    else
      FD_SET(servsock, &readfds);

    seltimeout.tv_sec =  TIMEOUT_SECS;
    seltimeout.tv_usec = 0;

    rc = select(maxfd + 1, &readfds, &writefds, NULL, &seltimeout);
    if (!rc)
      errorout("timeout expired!\n");
    else {
      if (FD_ISSET(STDIN_FILENO, &readfds)) {
        /* read stdin and pass as data to clients */
      }

      if (clientsock > 0) {
        if (FD_ISSET(clientsock, &readfds)) {
          /* client is readable */
          if (handleclient(&cl) < 0) {
            sclose(clientsock);
            clientsock = SOCKET_BAD;
          }
        }
        if (FD_ISSET(clientsock, &writefds)) {
          /* client is writeable */
        }
      }

      if (FD_ISSET(servsock, &readfds)) {
        printf("New client connects on port %d - assume fine TLS-NPN",
               SERVER_PORT);
        clientsock = acceptclient(servsock);

        /* create a spindly handle for the physical connection */
        cl.phys_server = spindly_phys_init(SPINDLY_SIDE_SERVER,
                                           SPINDLY_DEFAULT, NULL);
        cl.sock = clientsock; /* store the socket */
        maxfd = clientsock;
      }
    }
  }

  /* Close server socket */
  sclose(servsock);

  exit(0);
}