🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

SDLNet server can only accept one client at a time.

Started by
2 comments, last by hplus0603 9 years, 1 month ago
So I'm rewriting a server with SDL_net and I'm working on the main loop.
If I just do
if((tempSocket = SDLNet_TCP_Accept(sd)) > 0)
then I can accept sockets.
But if I try to actually check the socket set as I have it below it will let me accept one client.
Then once I have a client connected it won't say the socket set has activity when a client tries to connect until the first one disconnects.
while(cont)
    {
        int numReady = SDLNet_CheckSockets(serverSet,0);
        if(numReady < 0)
            log("Error checking server socket set.");
        else if(numReady > 0)
        {
            if(SDLNet_SocketReady(sd))
            {
                tempSocket = SDLNet_TCP_Accept(sd);
                if(tempSocket)
                {
                    log("Connection accepted.");
                    int freeClient = findFreeClient();
                    if(freeClient != -1)
                    {
                        if(clients[freeClient].createClient(tempSocket))
                        {
                            log("Accepted client with ID " + istr(freeClient));
                        }
                        else
                            log("Could not create new client.");
                        tempSocket = NULL;
                    }
                    else
                        log("Had to reject client. Too many connections.");
                }
                else
                    log("Error accepting socket.");
            }
            else
                log("Checked socket not ready.");
        }
 
        SDL_Delay(10);
    }
Should I just check for accept sockets like above even though it throws an error with SDLNet_GetError() or is there a way around this. If it's relevant the sockets connecting are from the same IP. (localhost)
Advertisement

I'm not 100% sure, but I think you need to add the TCP socket to a socket set first, and then call SDLNet_CheckSockets on the set, which returns the number of ready sockets in the set, at which point you would then call SDLNet_SocketReady on the server socket, after CheckSockets indicates that something is happening on the socket. This seems redundant to me, but SDLNet introduces a few of its own ideas to the whole convention of sockets as is. Keep us updated :)

I'm not 100% sure, but I think you need to add the TCP socket to a socket set first, and then call SDLNet_CheckSockets on the set, which returns the number of ready sockets in the set, at which point you would then call SDLNet_SocketReady on the server socket, after CheckSockets indicates that something is happening on the socket. This seems redundant to me, but SDLNet introduces a few of its own ideas to the whole convention of sockets as is. Keep us updated smile.png

sd was added to serverSet before the loop and in clients[freeClient].createClient(tempSocket) tempSocket is added to its own set with only itself in it.

I might try making it all one big socket set though.
Where are you adding the tempSocket to the serverSet?
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement