Jump to content
  • Advertisement
Sign in to follow this  
GamerCon

winsock 1.1 help

This topic is 4932 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

Share this post


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

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.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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!