Jump to content
  • Advertisement
Sign in to follow this  
stianstr

SDL UDP - Binding a channel

This topic is 3315 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'm making a small game and I am trying to use SDL_Net with UDP. (I have been searching the web, but most of the information is for SDL_Net with TCP.) At the moment the server loop is just the following:
  if (SDLNet_UDP_Recv(sd, p))
  {
     sprintf(szTmp, "%s", (char *)p->data);        
     ErrorLog("Received package: %s", szTmp);
  }

The message is received fine from the client, but what I want to do is to bind a socket for each client and put it in an array. Any tips on how I can make a loop that takes a new connection, add it to an array, and then receive/send information from/to clients? I have trouble understanding where the SDLNet_UDP_Bind fits in, I assume I can't call it for every message I receive? So how do I know if it is a new connection, or one that is already binded? Any guidance will help a lot... Thanks...

Share this post


Link to post
Share on other sites
Advertisement
Unlike TCP, with UDP you can use a single socket to service any number of clients. What you want to do is associate some data with an IP address/port pair.

If you are expecting a large number of clients, you can use something like std::map<> for this, for a small number a linear array search will probably be fine.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Unlike TCP, with UDP you can use a single socket to service any number of clients. What you want to do is associate some data with an IP address/port pair.

If you are expecting a large number of clients, you can use something like std::map<> for this, for a small number a linear array search will probably be fine.


If I understand it correct I will receive data from all the clients in the UDPpacket through the one UDPsocket that I have created.

I only plan to have 8 players, so a fixed size array should do fine.

If I want to send data to one specific client what can I do?

Share this post


Link to post
Share on other sites
You could remember each client's address, and send packets to them explicitly by filling the IPAddress field in the UDPpacket and setting the channel field in SDLNet_UDP_Send to -1. You can probably collect addresses from clients when they first connect by reading that same field from the incoming packet.

Alternatively it looks like you can use the SDL_Net channels to associate the incoming addresses with channel numbers, then you can just use those channel numbers in future instead of sending to explicit addresses. This is most useful when you have subsets of clients that you may want to broadcast packets to.

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!