On Sun, 20 Dec 1998, Glynn Clements wrote:

> Nuno Carvalho wrote:
> > 
> >  How can I limit a certain number of connections to a port !?
   ....
> >        int listen(int s, int backlog);
> > 
> >  What should I do for that !?
> 
> If you wish to limit the number of connections, you have to stop
> accept()ing new connections once you have reached the limit.

 Shouldn't listen do that !? Should I take care about number connections,
rigth !?

 Could you help me on limit connections !?
 On attachment it's a source code of the program I would like to do!
 I don't know how to make connection_number variable work fine(count the
number of incoming connections as also limit the number of connetions).

 Thanks for the help.

 Best regards,
  Nuno Carvalho

������������������������������
   Nuno Emanuel F. Carvalho
 Dep. Informatics Engineering
    University of Coimbra

  PGP key available at finger
������������������������������
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h> 
#include <sys/socket.h>
#include <signal.h>

#define MYPORT 4000   /* the port users will be connecting to */
#define BACKLOG 4     /* how many pending connections queue will hold */

int sockfd ;

void termina()
{
   while(wait(NULL)!=-1);
   close(sockfd);
   exit(0);
}   

main()
{
   int new_fd;  /* listen on sock_fd, new connection on new_fd */
   struct sockaddr_in my_addr;    /* my address information */
   struct sockaddr_in their_addr; /* connector's address information */
   int sin_size;
   char buffer[50];
   
   int pid, leu;
   int connection_number=0;


    sockfd = socket(AF_INET, SOCK_STREAM, 0) ;
    
    my_addr.sin_family = AF_INET;         /* host byte order */
    my_addr.sin_port = htons(MYPORT);     /* short, network byte order */
    my_addr.sin_addr.s_addr = INADDR_ANY; /* automatically fill with my IP */
    bzero(&(my_addr.sin_zero), 8);        /* zero the rest of the struct */

    if( (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)))<0) 
    {
      perror("[BIND]") ;
      exit(0);
    }    
    
    listen(sockfd, BACKLOG) ;

    sin_size = sizeof(struct sockaddr_in);
    signal(SIGINT, termina);    

   while(1)
   {
     if( connection_number < BACKLOG )
     {
   	    new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size) ;
        connection_number++ ;        
        if((pid=fork())<0)
        {
          close(new_fd);        
          termina();
          perror("[LISTEN]") ;
          exit(0);
        }else{
          if(pid==0)  /* CHILD process */
          {   
            if( (leu=recv(new_fd,buffer,50,0))==-1)
            {
              perror("[RECV]") ;
              exit(0) ;
            }
            close(new_fd);
            printf("\nConnection closed!\n");            
            exit(0);
          }else{      /* processo PAI */
            printf("server: got connection from %s on port %i at connection #%d\n",inet_ntoa(their_addr.sin_addr),
                  ntohs(their_addr.sin_port), connection_number);    
            close(new_fd);
            connection_number-- ;
          }
        }
     }else
     {
   	    new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size) ;
        send(new_fd, "\nConnections exceeded ....Please try later!\n", sizeof("\nConnections exceeded ....Please try later!\n"),0);
        close(new_fd);
     }
  }         /* Loop */
}

Reply via email to