Sign in to follow this  
GamerCon

winsock 1.1 help

Recommended Posts

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[i].connected)
            {
                clients[i].client = accept();
                clients[i].connected = true;
                break;
            }
        }
    }
    ...Other Code
}
[/souce]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
You access the accepted socket (in your psuedo code clients[i].client). The listening socket just sits there... listening.

in super psuedo code:

@socklist;
@tmpsocklist;
$listensock;
$tmpsock;
$newsock;

// do port/queue/address garbage here
bind($listensock); // Tells the OS which card to listen on
listen($listensock); // Actually starts listening for connections

add $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/

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this