Sign in to follow this  
YasinKhalil

UDP Multiplayer with SDL_net

Recommended Posts

YasinKhalil    224
Hello, this is my first time posting in these forums. I have been developing a game for several months now. I wanted to implement SDL_net into my code because my entire game engine uses SDL and many of the other addons (SDL_ttf/mixer/image..etc.) I have been coding the Multiplayer aspect of my game(2d side scroller). My experience with c++ is between 5-6 years so i have an understanding of the language. I'm using UDP for my game. The problem I'm having with SDL_net is that there are like no tutorials or examples on the net, which is very frustrating for someone trying to learn how to use it ( I have spent countless hours and days working on this without any help until now, i have used the faqs on this forum as well as the search bar where i searched through countless posts (dating back up to 4 years ago) about SDL_net where i saw a few people with my same problem but it looked like no one wanted to help them...) I am not asking to be spoon fed because i know thats not the right way and im a hard headed a@#hole who likes to learn by countless hours of trial and error. however, i just need a few hints on how to proceed. Ok, so now i will explain what I'm trying to freaken do lol:

tl;dr -> my client sends a packet with a "connecting" string to server, server reads packet and determines if there are available slots then server sends a string back to the client with "FULL" if
there are no slots left or a number if there are available slots to use as the clients ID. now my problem lies with sending packets from the server to the client.
for some reason i cant figure out how to send data to the client from the server.

On the server side i have tried to use these functions

[code]
IPaddress *address;
int chan;
address = SDLNet_UDP_GetPeerAddress(udpSocket, -1);

followed by the chan = SDLNet_UDP_Bind(udpSocket, -1, IPaddress *address);

I'm not quite sure what I'm supposed to be doing. am i supposed to SDLNet_UDP_GetPeerAddress first then use the
bind function afterwards and then SDLNet_UDP_Send. do i need to resolvehost at any point on the server side ?

[/code]

There is no documentation on the correct way of doing this. i mean there is the sdlnet wiki but its doesn't help me much.
maybe someone can send some pseudo code in [b]SDLNet SYNTAX[/b] with the correct set up for a server using [b]UDP [/b] or an example?
Also, I'm not looking to be flamed, i have looked into the faq and imo the answers are more generalized and not geared towards what I'm specifically looking for.

Share this post


Link to post
Share on other sites
smasherprog    568
You can skip using SDL if you want. Writing multi platform networking code is very simple, there will literally be maybe 15 lines of code difference between a windows and a Linux based networking library. So, you can either go straight to using the standard Berkley sockets, of which there are MANY tutorials on. Or, you can check the FAQ out for the existing network libraries. I would suggest using ENet as it is very simple to setup and use, or you can try out my library. The link is in my signature.

Share this post


Link to post
Share on other sites
smasherprog    568
Hidden
You can skip using SDL if you want. Writing multi platform networking code is very simple, there will literally be maybe 15 lines of code difference between a windows and a Linux based networking library. So, you can either go straight to using the standard Berkley sockets, of which there are MANY tutorials on. Or, you can check the FAQ out for the existing network libraries. I would suggest using ENet as it is very simple to setup and use, or you can try out my library. The link is in my signature.

Share this post


Link to post
YasinKhalil    224
[quote name='smasherprog' timestamp='1312863092' post='4846522']
You can skip using SDL if you want. Writing multi platform networking code is very simple, there will literally be maybe 15 lines of code difference between a windows and a Linux based networking library. So, you can either go straight to using the standard Berkley sockets, of which there are MANY tutorials on. Or, you can check the FAQ out for the existing network libraries. I would suggest using ENet as it is very simple to setup and use, or you can try out my library. The link is in my signature.
[/quote]

I appreciate your reply but honestly, i have spent too much time and effort to just give up on this and move to some other library/wrapper/whatever. call it idiotic or hardheaded but i
have just put too much effort to give up. However, i will take a look at your Library read through it etc. thanks.

Share this post


Link to post
Share on other sites
rip-off    10976
You don't bind UDP sockets, and getting the peer address is meaningless, because server UDP sockets generally talk to multiple clients.

What the server does is allocate a SDLNet_UDP_Packet*, set the "address" member to the same value as in the packet it SDLNet_UDP_Recv()ed* from the client (you'll likely be storing this address in a data structure somewhere), write whatever data it wants and then uses SDLNet_UDP_Send()*.

I suggest you create a new project, just to get familiar with SDL_Net. The first program might be a simple echo server (The server sends the clients message back at them) and a simple client that reads input from the command line. Then you might make something more sophisticated like a chat program. If you try to integrate networking with your main program before you are comfortable with what you are doing then you'll probably end up confused and frustrated.

*[size=1]Or whatever they're called, it has been a while since I've used SDL_net[/size]

Share this post


Link to post
Share on other sites
YasinKhalil    224
HAHAHA!!! i did it! :3 i made an echo server ^_^. I actually took what you said quite literally and simply did this !
Basicaly i just did exactly that "p" is my recv() udp packet and send is well the send packet.

[code]

send->address = p->address;
send->channel = -1;
char *dude = (char*)p->data;
send->data = (Uint8*)dude;
send->len = strlen((char *)send->data) + 1;
send->status = NULL;
SDLNet_UDP_Send(sd, -1, send);

[/code]

i read your reply and then went into the documentation for UDPpacket and found out that the address holds both the destination and the source ip address and port. then i just set it all equal to each other and send it on through!
High-five? lol

Share this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this