Jump to content
  • Advertisement
Sign in to follow this  
Decrius

[SDL_net] No wait time for receiving data?

This topic is 3790 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

Hi, I'm trying to build a very very simple chat program. You simply have a server and a client. Either the server and the client must be able to send data and to receive data when the other (server or client) has send data. That last part doesn't really work, as when you call the function to receive data, it will wait till the data received is as long as 'int maxlen' or if the connection is lost. Is there a way that it will only _check_ if data is send, and if there is that it only than will receive the data? And that the other end (server or client) will send data as long as 'int maxlen'? Or something similair, the problem is that the program freezes when its waiting for the data. Thank you, Decrius PS: I'm working with C++, SDL, SDL_net and the TCP protocol on a WinXP.

Share this post


Link to post
Share on other sites
Advertisement
You can use SDLNet_SocketSet to see if a socket (or set of sockets) have data to recieve on them:

// for a TCP socket called 'socket'
SDLNet_SocketSet set = SDLNet_AllocSocketSet(1); // 1 is the number of sockets you will be monitoring
SDLNet_TCP_AddSocket(set,socket);
int num = SDLNet_CheckSockets(set,0); // 0 is the time to wait
SDLNet_FreeSocketSet(set);

if( num == -1 )
{
// handle error somehow
throw std::runtime_error("SDLNet_CheckSockets: " + str(SDLNet_GetError()));
}
else if( num == 0 )
{
// no sockets of interest
}
else
{
// calling SDLNet_TCP_Recv on 'socket' will not block the program

// if you are monitoring multiple sockets, loop through them and use
// http://www.libsdl.org/cgi/docwiki.cgi/SDLNet_5fSocketReady
// SDLNet_SocketReady() to determine whether this socket is active.
}


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!