winsock 1.1 help

Started by
3 comments, last by Telastyn 19 years, 4 months ago
I know how to set up a client and server but im lost when it comes to multiple connections. I've read plenty of winsock tutorials but they either dont cover the subject or arent clear to me. in a main game loop, what function am I supposed to check to determine if a client is trying to connect? I was leaning twords something like..

while(true)
{
    if(listen())
    {
        for(int i=0;i<MAX_CONN;i++)
        {
            if(!clients.connected)
            {
                clients.client = accept();
                clients.connected = true;
                break;
            }
        }
    }
    ...Other Code
}
[/souce]

OS: WinXP HECOMPILER: DevC++ mingw32Programming since 10/3/2004 6:15:36 PMCurrent Project: An un-named MUDCompleted Projects:Tic-Tac-Toe (w32 console)PacMan (w32 console)Tetris (w32 console)Minesweeper (Allegro)BomberMan (Allegro)Chat Client (w32 console)
Advertisement
hmm looks ok to me,
I use something similar, why don't you just try it?
open a server and 2 clients and connect to 127.0.0.1 (loopback)
Doesnt work for me, listen() is always returning 0 and the code in the "if" statements doesnt execute but the clients continualy connect. The strange thing about the clients being able to connect is that, since, the code in the "if" statement doesnt execute, that means the server isn't accept()ing them. I read in one tutorial that listen() creates a socket for listening and another socket when a client connects, but, how would i access the later socket?
OS: WinXP HECOMPILER: DevC++ mingw32Programming since 10/3/2004 6:15:36 PMCurrent Project: An un-named MUDCompleted Projects:Tic-Tac-Toe (w32 console)PacMan (w32 console)Tetris (w32 console)Minesweeper (Allegro)BomberMan (Allegro)Chat Client (w32 console)
When a new client is trying to connect, accept() on the socket you're previously listen()-ing on will return a new socket. If no connection is in queue, then accept() will return -1, if the listen socket is non-blocking, else it will block until a connection comes in. select() on a listen()-ing socket will return the socket as readable if there is a connection pending.

Your suggestion would work reasonably, assuming the socket you accept() on is non-blocking. It would call accept() more than necessary, but that might not be a problem for you.
enum Bool { True, False, FileNotFound };
You access the accepted socket (in your psuedo code clients.client). The listening socket just sits there... listening.

in super psuedo code:
@socklist;@tmpsocklist;$listensock;$tmpsock;$newsock;// do port/queue/address garbage herebind($listensock);            // Tells the OS which card to listen onlisten($listensock);          // Actually starts listening for connectionsadd $listensock to @socklist;while (1){     @tmpsocklist=@socklist;             select(@tmpsocketlist);                     // This is the really important part!     //  Select does magical things to the socket list     //  basically picking which ones are active     //  and deleting the rest from the list     //  @tmpsocklist is now a list of the active sockets     //  if $listensock is active, this means you now have     //  a new client!     //     //  note the copy of the list before select!     //  since select modifies the list, you want to keep     //  the original list intact.     for $tmpsock = each @tmpsocklist{          if ($tmpsock == $listensock){               // new client!               $newsock = accept($listensock);               // the new client gets assigned a new socket!               add $newsock to @socklist;               // note: add the socket to the socket list so next               //  time through, you'll check for info!          }else{               // then you've got an already connected client               //  sending you info.               $x = recv($tmpsock,$buffer);               if ($x<1){                   // something bad happened!                   //                   // -1 is an error.                   // 0 means the client just closed the connection.                   remove $tmpsock from @socklist;               }else{                   // then $buffer now has some stuff.                   do_stuff($buffer);               }          }     }     // and here, you do the rest of the game loop!}


This is a little bit more of BSD sockets than winsock, so it will be far more portable should you ever leave the windows world [as servers tend to do...]

This is the best online socket programming guide, and will go better into details, along with code examples:

http://www.ecst.csuchico.edu/~beej/guide/net/

This topic is closed to new replies.

Advertisement