C中的服务器套接字程序,用于多个端口

时间:2014-03-12 20:05:49

标签: c sockets

我正在尝试在多个端口上侦听多个客户端,但我无法执行多个端口。我提供的一个端口作为输入工作就像一个魅力,但没有得到如何提供多个端口。请帮忙。下面是我的server.c程序

int main (int argc, char **argv)
{

  int listenfd, connfd, n, portnum, fdmax, i, j, nbytes;
  /* master file descriptor list */
  fd_set master;
  /* temp file descriptor list for select() */
  fd_set read_fds;
  pid_t childpid;
  socklen_t clilen;
  char buf[MAXLINE];
  struct sockaddr_in cliaddr, servaddr;
  FD_ZERO(&master);
  FD_ZERO(&read_fds);


  //Create a socket for the soclet
  //If sockfd<0 there was an error in the creation of the socket
  if ((listenfd = socket (AF_INET, SOCK_STREAM, 0)) <0) {
  perror("Problem in creating the socket");
  exit(2);
  }


  //preparation of the socket address
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  scanf("%d", &portnum);
  servaddr.sin_port = htons(portnum);
  memset(&(servaddr.sin_zero), '\0', 8);

  //bind the socket
  bind (listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));

  //listen to the socket by creating a connection queue, then wait for clients
  listen (listenfd, LISTENQ);

  printf("%s\n","Server running...waiting for connections.");

 /* add the listener to the master set */
 FD_SET(listenfd, &master);
 /* keep track of the biggest file descriptor */
 fdmax = listenfd; /* so far, it's this one*/

 for ( ; ; ) {

 /* copy it */
 read_fds = master;

 if(select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1)
 {
    perror("Server-select() error lol!");
    exit(1);
 }
 printf("Server-select() is OK...\n");

 /*run through the existing connections looking for data to be read*/
 for(i = 0; i <= fdmax; i++)
 {
    if(FD_ISSET(i, &read_fds))
    { /* we got one... */
       if(i == listenfd)
       {
           /* handle new connections */
           clilen = sizeof(cliaddr);

           //accept a connection
           connfd = accept (listenfd, (struct sockaddr *) &cliaddr, &clilen);

           printf("%s\n","Received request...");

           if ( (childpid = fork ()) == 0 ) {//if it’s 0, it’s child process

           printf ("%s\n","Child created for dealing with client requests");

           //close listening socket
           close (listenfd);

           while ( (n = recv(connfd, buf, MAXLINE,0)) > 0)  {
           printf("%s","String received from and resent to the client:");
           puts(buf);
           send(connfd, buf, n, 0);
        }
        if (n < 0)
            printf("%s\n", "Read error");
        exit(0);
    }

    for(j = 0; j <= fdmax; j++)
    {
           /* send to everyone! */
           if(FD_ISSET(j, &master))
           {
                    /* except the listener and ourselves */
                    if(j != listenfd && j != i)
                    {
                         if(send(j, buf, nbytes, 0) == -1)
                         perror("send() error lol!");
                    }
           }
    }
    //close socket of the server
    close(connfd);
}
}
}
}
}

0 个答案:

没有答案