Exemplo n.º 1
0
void NetServer::update()
{
	SDLNet_CheckSockets(socketset, 0);

    if(SDLNet_SocketReady(tcpsock)) {
		handleserver();
	}

    for(int i = 0; i < MAXCLIENTS; ++i) {
        if(SDLNet_SocketReady(clients[i].sock)) {
			handleclient(i);
		}
	}
}
Exemplo 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);
}
Exemplo n.º 3
0
int main(void) {
    int clientfd, maxfd, nready;
    struct client *p;
    struct client *head = NULL;
    socklen_t len;
    struct sockaddr_in q;
    struct timeval tv;
    fd_set allset;
    fd_set rset;
    srand (time(NULL));

    int i;

    //Initialize a listening fd of the server.
    int listenfd = bindandlisten();

    // initialize allset and add listenfd to the
    // set of file descriptors passed into select
    FD_ZERO(&allset);
    FD_SET(listenfd, &allset);

    // maxfd identifies how far into the set to search
    maxfd = listenfd;

    while (1) {//keep running
        // make a copy of the set before we pass it into select
        rset = allset;
        /* timeout in seconds (You may not need to use a timeout for
        * your assignment)*/
        tv.tv_sec = 2; // change to 0 from 10
        tv.tv_usec = 0;  /* and microseconds */

        nready = select(maxfd + 1, &rset, NULL, NULL, &tv); //Select a ready fd descriptor(client) to read.
        if (nready == 0) {
            printf("No response from clients in %ld seconds\n", tv.tv_sec);
            continue;
        }

        if (nready == -1) {
            perror("select");
            continue;
        }

        if (FD_ISSET(listenfd, &rset)) {
            printf("a new client is connecting\n");
            len = sizeof(q);
            if ((clientfd = accept(listenfd, (struct sockaddr *)&q, &len)) < 0) {
                perror("accept");
                exit(1);
            }
            FD_SET(clientfd, &allset);
            if (clientfd > maxfd) {
                maxfd = clientfd;
            }
            printf("connection from %s\n", inet_ntoa(q.sin_addr));
            //sprintf("what is your name?");
            head = addclient(head, clientfd, q.sin_addr);
        }
        //Handle every client
        //printf("running\n");
        for(i = 0; i <= maxfd; i++) {
            if (FD_ISSET(i, &rset)) {
                for (p = head; p != NULL; p = p->next) {
                    if (p->fd == i) {//
                        int result = handleclient(p, head);
                        if (result == -1) {
                            int tmp_fd = p->fd;
                            head = removeclient(head, p->fd);
                            FD_CLR(tmp_fd, &allset);
                            close(tmp_fd);
                        }
                        break;
                    }
                }
            }
        }
    }
    return 0;
}