Thread: Problem using sockets for multiple clients

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    13

    Question Problem using sockets for multiple clients

    Hi,
    I'm a bit of a newbie and in getting this far I have exhausted all the online socket tutorials I could find.

    Here's my problem:
    I have a server and a client. They connect happily. Once. To make them connect again I have to set them both to use a different port. How do I free up resources from the port the server is listening to? As far as my conceptual understanding goes, I want the server to accept the connection and shift it to a different port, so it can resume listening on the original port (while the client is connected to another) for the next potential connection.

    I believe my problem lies somewhere in the following server connection code, but I'm clueless as to where. "man socket" suggests I need the socket() command to work differently somehow, but doesn't make much sense to me.

    Note, #define is used to define PORTNUMBER and MAXPENDING.
    Code:
    	int socketfd, clientlength;
    	struct sockaddr_in serv_addr, cli_addr;
    
    	socketfd = socket( PF_INET, SOCK_STREAM, 0);
    	if (socketfd < 0)
    	{
    		printf("\n\tERROR:  Unable to open socket");
    		exit(0);
    	}
    	bzero((char *) &serv_addr, sizeof(serv_addr));
    	serv_addr.sin_family = PF_INET;
    	serv_addr.sin_addr.s_addr = INADDR_ANY;
    	serv_addr.sin_port = htons(PORTNUMBER);			
    	if (bind(socketfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
    	{
    		printf("\n\tERROR:  Unable to bind socket\n");
    		exit(0);
    	}
    	listen(socketfd,MAXPENDING);
    	clientlength = sizeof(cli_addr);
    	socketfd = accept(socketfd, (struct sockaddr *) &cli_addr, &clientlength);
    	if (socketfd < 0)
    	{
    		printf("\n\tERROR:  Unable to accept connection\n");
    		exit(0);
    	}
    	printf("\n\tConnected.\n");
    Any advice would be greatly appreciated.
    Thanks.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    accept() should be called in a loop and the socket it returns is not the same as the one you pass to it so you can keep accepting connections on that socket. The say I have always done it is to create a new thread to handle send/recv on the returned socket.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    13

    Cool

    Hey thanks a lot. Your reply confused me for a while ( i just learnt the meaning of a 'fork bomb'...twice actually.), but now I'm getting it sorted. Well, mostly anyway. I'm able to connect multiple clients, BUT for some reason it's not behaving how I expected. I think I can fix that by myself though. Hopefully.

    Thanks so much for the advice. It seems obvious now

    Cheers man

  4. #4
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    look at the setsockopt function. Does wonders or releasing the port ASAP. Also to all Newbies I'd recommend getting this book: [green] Effective TCP/IP Programming: 44 tips to improve your Network Programs[/green] by Jon C Snader. REALLY HELPS
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple clients to "internet" server
    By Zarniwoop in forum C Programming
    Replies: 2
    Last Post: 10-11-2008, 11:04 PM
  2. Discerning sockets using asynchronous
    By jmd15 in forum Networking/Device Communication
    Replies: 4
    Last Post: 02-16-2006, 02:03 PM
  3. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 07:36 PM
  4. BSD Sockets: Finding a clients IP
    By Moddy in forum C++ Programming
    Replies: 2
    Last Post: 12-16-2005, 02:06 PM
  5. Sign-up Thread: Problem Solving #1
    By ygfperson in forum Contests Board
    Replies: 15
    Last Post: 01-26-2003, 02:55 AM