Thread: Listen at several ports at the same time

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    2

    Listen at several ports at the same time

    Hi, I'm wondering if it is possible to code a program that listens to more than one port at the same time, and be capable of manage several clients at the same time (I pretty sure this is possible, otherwise there wouldn't be online games and things like that) and then make those clients that are connected to the program through different ports to communicate each other. Well, I'm asking many things here, my main interest is to know if it is possible to open (and listen) at several ports at the same time. Please post a code as an example.

    Btw, I'm using mainly linux, but windows examples are fine too.

    Thanks a lot.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Have you read "beej's guide to network programming"?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    you don't need to listen to several ports to be able to handle multiple clients...
    multiple clients can be assigned to one single port...
    many methods for doing this, the easiest way to create one thread for every client...
    there are also an asynchronous method...

    go for the web...find out...check codeproject.com if you are using windows...

  4. #4
    Registered User
    Join Date
    Feb 2009
    Location
    India, Gujarat
    Posts
    22
    hi,

    yes u can do it, u have to create multiple socket and using select() or poll() u can listen on multiple port, when u found the event on particular socket fd then u create one thread and perform ur job with client connection.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Atlantico View Post
    Hi, I'm wondering if it is possible to code a program that listens to more than one port at the same time, and be capable of manage several clients at the same time (I pretty sure this is possible, otherwise there wouldn't be online games and things like that)
    Of course, silly -- it's called a server! You should decide if you really need to listen at multiple ports tho, since a large number of clients can be connected to a single server on a single port (which would be the normal and easy way).

    Technically, a server does not have to maintain multiple connections -- it is a server by virtue of the fact that it can accept() a connection request. Servers can make calls AND answer them; a client can only make them. But a port number is not like a single phone line; it's a single number to which multiple calls can be addressed simultaneously without problem (presuming the server is set up to answer them all). What happens is that each client gets it's own socket, which is part of the server process. This
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    
    
    int bindsock (int port) {
    	struct sockaddr_in Me;
    	int sock;
    	if ((sock=socket(PF_INET, SOCK_STREAM, 0))<3) perror("socket");
    	Me.sin_family=AF_INET;
    	Me.sin_port=htons(port);
    	Me.sin_addr.s_addr=INADDR_ANY;
    /*	inet_aton(addrstr,(struct in_addr*)&Me.sin_addr.s_addr)  <-- assign address */
    	memset(&(Me.sin_zero), '\0', 8);
            if (bind(sock,(struct sockaddr*)&Me,sizeof(struct sockaddr))==-1) perror("bind");
    	if (listen(sock,2)!=0) perror("listen");
    	return sock;
    }
    
    
    int main(int argc, char *argv[]) {       /* argv[1] should be a port number */
    	struct sockaddr_in info;
    	int SFD=bindsock(atoi(argv[1])), CFD;  
    	socklen_t len=sizeof(struct sockaddr_in); /* beware this detail */
    
    	printf("using port %d...\n",atoi(argv[1]));
    	CFD=accept(SFD,(struct sockaddr*)&info,&len);
    	printf("accepted connection from %s \n",inet_ntoa(info.sin_addr));
    	return 0;
    }
    is the basic server process in C. It's written for linux but if you included the correct headers it should work anywhere. All it does is monitor a port until a client attempts to connect(), which it accepts and then ends.

    If you wanted to accept multiple connections, you could do it in a couple of different ways. Notice that the server uses one socket to monitor the port and then assigns a different socket to the accepted connection. All you need to do is create new sockets for each client.

    You also need a method to deal with all the connections and monitor the port at (more or less) the same time. That will depend on the specifics of what you're doing. It is possible to do it without forking or threading, in a continuous loop, if you set the all the sockets in non-blocking mode
    Code:
    fcntl(sock,F_SETFL,O_NONBLOCK);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    2
    Thanks for all of your answers. Do you have an specific example of how to do it. Because I have been looking for an example code using the information that you gave me and I just can't found anything. I'd really appreciate an example code.

    Thanks in advance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  3. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM