Thread: [Help] Best way of using sockets

  1. #1
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193

    [Help] Best way of using sockets

    I'm triying to make a server to hold some information about gaming servers, so the client can connect to the server and retrieve the list and see some more options stored in the server..
    Well, my doubt is, what is the best way of using sockets for multiple connection?
    I was planning on creating a socket for each new connection or something like that.. I need some advice on how to do a multiple stable connection..
    Thanks

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    fork()? What operating system are you using, lautarox? My platform independant response is that you should have a have asynchronous threads for each connection since I assume you want to have real-time interaction.

  3. #3
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    I pretend to build it for linux and windows, i'm working in linux, so if you can give me an example for linux i'll really appreciate that =P

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    My initial statement was hinting at linux, so my ability to predict people's platform is still going strong

    I can spend an hour teaching you this stuff so it may be better for both of us to give you a link to someone who has already compiled the information you are seeking.

    This site seems to be actually just about the best site I have ever seen. I had never seen it prior to googling for an example for you. But it is definitely one I will recommend to other people who are wanting to learn the ropes too.

    Read over his code then ask specific questions as you need help (preferably in either the Linux boards or the Network Programming boards since you are actually asking questions more geared toward those two subject matters).

  5. #5
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Really usefull page, thanks.
    I'm new here, and with the few time i have here, i also find this forum on of the bests.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Me too. Especially when you ask questions while some of us are actually online. Its nice to have an answer to your question within 5 minutes

  7. #7
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    I need a little bit of help undestanding this part of the code.. from the page.. http://www.tenouk.com/Module41.html, i know it's commented but i need an easier explanation about the for loop, how does it really works and about the sets for the select. Also how does it recognise if it is a new connection?
    Thanks

    Code:
    /* add the listener to the master set */
    FD_SET(listener, &master);
    /* keep track of the biggest file descriptor */
    fdmax = listener; /* so far, it's this one*/
    /* loop */
    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 == listener)
         {
             /* handle new connections */
            addrlen = sizeof(clientaddr);
    if((newfd = accept(listener, (struct sockaddr *)&clientaddr, &addrlen)) == -1)
    {
        perror("Server-accept() error lol!");
    }
    else
    {
        printf("Server-accept() is OK...\n");
    FD_SET(newfd, &master); /* add to master set */
    if(newfd > fdmax)
    { /* keep track of the maximum */
        fdmax = newfd;
    }
    printf("&#37;s: New connection from %s on socket %d\n", argv[0], inet_ntoa(clientaddr.sin_addr), newfd);
    }
    }
    else
    {
    /* handle data from a client */
    if((nbytes = recv(i, buf, sizeof(buf), 0)) <= 0)
    {
    /* got error or connection closed by client */
    if(nbytes == 0)
     /* connection closed */
     printf("%s: socket %d hung up\n", argv[0], i);
    else
    perror("recv() error lol!");
    /* close it... */
    close(i);
    /* remove from master set */
    FD_CLR(i, &master);
    }
    else
    {
    /* we got some data from a client*/
    for(j = 0; j <= fdmax; j++)
    {
    /* send to everyone! */
    if(FD_ISSET(j, &master))
    {
           /* except the listener and ourselves */
           if(j != listener && j != i)
           {
                  if(send(j, buf, nbytes, 0) == -1)
                         perror("send() error lol!");
           }
    }
    Last edited by lautarox; 09-04-2008 at 03:06 PM.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Urf. I hate reading code that isn't indented, especially when I don't know what it does.
    Code:
    /* add the listener to the master set */
    FD_SET (listener, &master);
    /* keep track of the biggest file descriptor */
    fdmax = listener;                /* so far, it's this one */
    /* loop */
    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 == listener)
                {
                    /* handle new connections */
                    addrlen = sizeof (clientaddr);
                    if ((newfd =
                        accept (listener, (struct sockaddr *) &clientaddr,
                        &addrlen)) == -1)
                    {
                        perror ("Server-accept() error lol!");
                    }
                    else
                    {
                        printf ("Server-accept() is OK...\n");
                                     /* add to master set */
                        FD_SET (newfd, &master);
                        if (newfd > fdmax)
                        {            /* keep track of the maximum */
                            fdmax = newfd;
                        }
                        printf ("&#37;s: New connection from %s on socket %d\n",
                            argv[0], inet_ntoa (clientaddr.sin_addr), newfd);
                    }
                }
                else
                {
                    /* handle data from a client */
                    if ((nbytes = recv (i, buf, sizeof (buf), 0)) <= 0)
                    {
                        /* got error or connection closed by client */
                        if (nbytes == 0)
                            /* connection closed */
                            printf ("%s: socket %d hung up\n", argv[0], i);
                        else
                            perror ("recv() error lol!");
                        /* close it... */
                        close (i);
                        /* remove from master set */
                        FD_CLR (i, &master);
                    }
                    else
                    {
                        /* we got some data from a client*/
                        for (j = 0; j <= fdmax; j++)
                        {
                            /* send to everyone! */
                            if (FD_ISSET (j, &master))
                            {
                                /* except the listener and ourselves */
                                if (j != listener && j != i)
                                {
                                    if (send (j, buf, nbytes, 0) == -1)
                                        perror ("send() error lol!");
                                }
                            }
    Much better . . .

    I don't know much about socket programming, but I think a set of sockets is like an array of them. Just thought I'd clear that up at the beginning.

    How does it handle new connections? Well . . .
    Code:
                if (i == listener)
                {
                    /* handle new connections */
                    addrlen = sizeof (clientaddr);
                    if ((newfd =
                        accept (listener, (struct sockaddr *) &clientaddr,
                        &addrlen)) == -1)
                    {
                        perror ("Server-accept() error lol!");
                    }
                    else
                    {
                        printf ("Server-accept() is OK...\n");
                                     /* add to master set */
                        FD_SET (newfd, &master);
                        if (newfd > fdmax)
                        {            /* keep track of the maximum */
                            fdmax = newfd;
                        }
                        printf ("%s: New connection from %s on socket %d\n",
                            argv[0], inet_ntoa (clientaddr.sin_addr), newfd);
                    }
                }
    The program has opened a listener socket, and when it receives a connection on that one, it must be a new one. It adds it to the list, and there you go.

    I make it sound so simple.

    [edit] I much prefer this tutorial: http://beej.us/guide/bgnet/output/ht...et.html#listen [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to poll sockets?
    By 39ster in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-22-2008, 01:43 PM
  2. program structure with select sockets
    By n3v in forum Networking/Device Communication
    Replies: 9
    Last Post: 06-03-2006, 06:34 AM
  3. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 07:36 PM
  4. Raw Sockets and SP2...
    By Devil Panther in forum Networking/Device Communication
    Replies: 11
    Last Post: 08-12-2005, 04:52 AM
  5. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 11:49 AM