What happen if make more connection that are set in listn ?

This is a discussion on What happen if make more connection that are set in listn ? within the Networking/Device Communication forums, part of the General Programming Boards category; Hello ,... what happen if i set a queue amount of available connections (second parameter in listen()) and then create ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    112

    What happen if make more connection that are set in listn ?

    Hello ,...

    what happen if i set a queue amount of available connections (second parameter in listen()) and then create more connection :




    If i run this program in the first time nothing wired happens (all goes as planed).
    but if run this same program again im unable to connect (get "Connection refused" error) .

    the client is telnet


    Code:
    /*
     * This program is free software; you can redistribute it and/or modify
     * it under the terms of the GNU General Public License as published by
     * the Free Software Foundation; either version 2 of the License, or
     * (at your option) any later version.
     * 
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU Library General Public License for more details.
     * 
     * You should have received a copy of the GNU General Public License
     * along with this program; if not, write to the Free Software
     * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
     */
     
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    
    int main()
    {
    	int sockfd , newfd; // The file discriptor
    	int fd[5] ;
    	struct sockaddr_in my_addr ;
            struct sockaddr_in their_addr ;
    	int i = 0;
    	int sin_size;
    	sockfd = socket(PF_INET , SOCK_STREAM , 0);
    	/**
    	 *  0 will make socket to choose the protocol tru the protocol 
    	 */
    	
    	 my_addr.sin_family = AF_INET;
    	 my_addr.sin_port = htons(12345);
    	 my_addr.sin_addr.s_addr = INADDR_ANY;// connect on any ip ?
    	 memset(my_addr.sin_zero, '\0',sizeof my_addr.sin_zero);
             bind(sockfd , (struct sockaddr * )&my_addr , sizeof my_addr);//So bind was done ..
    	 listen(sockfd , 2); // I will listn only for two connection but what if i connect more times ? 
    
    	for ( i = 0 ; i < 5; i++)
    	{
                fd[i] =  accept(sockfd , (struct sockaddr *) &their_addr, &sin_size);
                send(fd[i] , inet_ntoa(their_addr.sin_addr) , strlen ( inet_ntoa(their_addr.sin_addr) ) , 0); //Just send the connecter his ip.
    	}
    	
    		 close(fd[0]);
    		 close(fd[1]);
    		 close(fd[2]);
    		 close(fd[3]);
    		 close(fd[4]);
    
     return 0;	
    	return 0;
    }
    Last edited by jabka; 05-19-2008 at 03:29 PM.
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    There is no substitute for checking the return values from any system call. I suspect that the second time that you run this code you'll get an error on bind(). Try closing sockfd on exit and see if that works.

    I may not understand it right but TCP will hold the socket open just in case there are any stray packets out there that show up later. This timeout is in the area of a few hours. I think your problem has something to do with that and frankly I don't understand it well enough to explain it.

    If you are going to be doing any real network programming then THE book to get is "Unix Network Programming, Volume 1" by Stevens, Fenner and Rudoff. I've recently purchased it, and learned more in the first few days than all the time I spent with other "lesser" books.

    Also, listen() does not limit the number of connections, it limits the number of connection attempts at any given time. Basically (IIRC) it is a queue to hold the connection information between the time that the client tries to connect and the time that your server calls accept(). Once you call accept() and get the fd for the socket, that spot in the queue is freed up to listen for another connection. The connection limit that you have is 5, but only because of the for() loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. VIM: The (un)official thread
    By MK27 in forum Programming Book and Product Reviews
    Replies: 32
    Last Post: 04-20-2011, 06:43 PM
  2. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  3. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 01:56 PM
  4. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2002, 07:40 PM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21