Starting with SDL_net[solved]

Recommended Posts

gsGomer    122
So I am reading through the Beej's guide to Network programming, and its very informative. I am simultaneously trying to create a network application using the SDL_net library. So far everything is working great, but I have run into one stumbling block. I'm sure this is an easy fix, but I wanted to get some experienced advice on the matter. So far I have an application that can be a server or a client, just reads a command line argument "server" to differentiate. Then I call the resolve host function which sets the ipaddress and port to listen (or connect) on. Then I use this address information to create a tcp socket. Now for a server I know I have to call the TCP accept function to accept an incoming client. My goal however is for the server to sit and listen on a port until a client connects and then do something. Here is my code so far:
#include "SDL.h"
#include "SDL_net.h"

bool isServer = false;
unsigned int port = 19800;
char* host = NULL;
TCPsocket tcpsock, insock;

bool Init()
{
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
return(false);
if(SDLNet_Init() == -1)
return(false);

if(isServer)
{
if(SDLNet_ResolveHost(&ipaddress, NULL, port) == -1)
return false;
return(false);
if(!tcpsock)
return(false);
}
else
{
if(SDLNet_ResolveHost(&ipaddress, "neprog", port) == -1)
return false;
if(!tcpsock)
return(false);
}

return(true);
}

void cleanup()
{
if(tcpsock)
SDLNet_TCP_Close(tcpsock);
SDLNet_Quit();
SDL_Quit();
}

int main(int argc, char** argv)
{
if(argc > 1)
{
if(strcmp("server", argv[1]) == 0)
isServer = true;
}
if(!Init())
return 1;

if(isServer)
{
insock = SDLNet_TCP_Accept(tcpsock);
if(!insock)
return 1;
else
{
}
}

cleanup();

return 0;

}


Problem is insock always comes back NULL, which means failure to accept. How do I make my server sit and listen (rather then execute the accept function at the same time a client attempts to connect? Should I place the accept function in some sort of loop? Would appreciate any kind of help on this matter! :) [Edited by - gsGomer on March 22, 2008 12:44:33 AM]

Share on other sites
rip-off    10976
IIRC, SDL_net makes accept a non-blocking call. You can run it in a loop to make it simulate a blocking call:
TCPsocket accept(TCPsocket server){     TCPsocket *socket;     while( (socket = SDLNet_TCP_Accept(server)) == NULL )     {         SDL_Delay(/* some value*/);     }     return socket;}

If you want to handle more than one client at once, having accept as a non-blocking call is rather handy.

Share on other sites
gsGomer    122
So that makes sense and indeed works. However I'm confused by your semantics, mostly because I'm very new to network programming....like this is my first network program.

You say this simulates a "blocking call", and the accept function is for a "non-blocking call." I imagine that this means running accept through a loop only allows the connection of one client at a time.

So if this method is limited, how would I go about using the accept method for "non-blocking calls?" I guess what I mean is how would you use the method in a useful network server that will communicate with more then one client?

Thanks again for the help!

Ok, thanks for the "nudge" in the right direction.
I managed to pull up some examples of SDL_net application and found that my answer is in fact context sensitive.

I suppose that if I was writing this code in a game I would attempt an accept call perhaps 1-3 times a frame (I guess that really depends on the game I'm writing) and implement the rest of the code if I get a connection. I also found socket sets to be very useful for this type of situation.
[/Edit]

[Edited by - gsGomer on March 22, 2008 12:27:16 AM]

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