SDL_net UDP Broadcast - Howto?

Greetings everyone!

I’m wanting to tinker with some network programming and so I was naturally
checking out SDL_net. I keep coming across people saying that SDL_net supports
UDP broadcasting, but I have not been able to find an example of this.

I wrote a quick server and client. The client is supposed to resolve
255.255.255.255, but this test failed. I then tried using 192.168.0.255 which
192.168.0 is my work’s subnet. Here is the code I wrote:

SERVER:
#include “SDL.h”
#include “SDL_net.h”

int main(int argc, char *argv[]) {
SDL_Surface *screen = NULL;
IPaddress ipaddress;
UDPsocket sock;
UDPpacket *in;
Uint8 *keystate = NULL;
SDL_Init(SDL_INIT_VIDEO);
SDLNet_Init();
screen = SDL_SetVideoMode(640,480,0,SDL_SWSURFACE|SDL_DOUBLEBUF);
sock = SDLNet_UDP_Open(1234);
in = SDLNet_AllocPacket(65535);
keystate = SDL_GetKeyState(NULL);
printf(“SERVER STARTED\n”); fflush(stdout);
while (!keystate[SDLK_ESCAPE]) {
if (SDLNet_UDP_Recv(sock, in)) {
printf(“MSG: [%s]\n”,in->data); fflush(stdout);
}
SDL_Delay(10);
SDL_PumpEvents();
keystate = SDL_GetKeyState(NULL);
}
printf(“SERVER STOPPED\n”); fflush(stdout);
SDLNet_FreePacket(in);
SDLNet_UDP_Close(sock);
SDLNet_Quit();
SDL_Quit();
return 0;
}

CLIENT:
#include “SDL.h”
#include “SDL_net.h”

int main(int argc, char *argv[]) {
IPaddress ipaddress;
UDPsocket sock;
UDPpacket *out;
SDL_Init(SDL_INIT_VIDEO);
SDLNet_Init();
SDLNet_ResolveHost(&ipaddress,“192.168.0.255”,1234);
sock = SDLNet_UDP_Open(0);
out = SDLNet_AllocPacket(65535);
SDLNet_UDP_Bind(sock, 0, &ipaddress);
printf(“CLIENT STARTED\n”); fflush(stdout);
sprintf(out->data,“HELLO! CAN ANYBODY HEAR ME?”);
SDLNet_UDP_Send(sock,0,out);
printf(“CLIENT STOPPED\n”); fflush(stdout);
SDLNet_FreePacket(out);
SDLNet_UDP_Close(sock);
SDLNet_Quit();
SDL_Quit();
return 0;
}

I think the issue is that you are using SDLNet_UDP_Bind. You can
instead set the ip address of the packet manually, which works for me.

SDLNet_ResolveHost(&ipaddress,“255.255.255.255”,1234);
// …
out->address = ipaddress;

http://www.libsdl.org/cgi/docwiki.cgi/UDPpacket

Also, using 65535 as an incoming packet size seems a little paranoid =)On Tue, Jun 2, 2009 at 5:39 PM, Micah Brening <micah.brening at gmail.com> wrote:

Greetings everyone!

I’m wanting to tinker with some network programming and so I was naturally
checking out SDL_net. ?I keep coming across people saying that SDL_net supports
UDP broadcasting, but I have not been able to find an example of this.

I wrote a quick server and client. ?The client is supposed to resolve
255.255.255.255, but this test failed. ?I then tried using 192.168.0.255 which
192.168.0 is my work’s subnet. ?Here is the code I wrote:
<<>>

This is the example I’ve used:
http://wiki.gamedev.net/index.php/SDL:Tutorial:Using_SDL_net

HTH
Phuoc Can HUA

/_/_/_/_/_/_
www.huaonline.com
My Homepage is my Castle— On Wed, 3/6/09, Micah Brening <micah.brening at gmail.com> wrote:

From: Micah Brening <micah.brening at gmail.com>
Subject: [SDL] SDL_net UDP Broadcast - Howto?
To: sdl at libsdl.org
Received: Wednesday, 3 June, 2009, 2:39 AM
Greetings everyone!

I’m wanting to tinker with some network programming and so
I was naturally
checking out SDL_net.? I keep coming across people
saying that SDL_net supports
UDP broadcasting, but I have not been able to find an
example of this.

I wrote a quick server and client.? The client is
supposed to resolve
255.255.255.255, but this test failed.? I then tried
using 192.168.0.255 which
192.168.0 is my work’s subnet.? Here is the code I
wrote:

SERVER:
#include “SDL.h”
#include “SDL_net.h”

int main(int argc, char *argv[]) {
??? SDL_Surface *screen = NULL;
??? IPaddress ipaddress;
??? UDPsocket sock;
??? UDPpacket *in;
??? Uint8 *keystate = NULL;
??? SDL_Init(SDL_INIT_VIDEO);
??? SDLNet_Init();
??? screen =
SDL_SetVideoMode(640,480,0,SDL_SWSURFACE|SDL_DOUBLEBUF);
??? sock = SDLNet_UDP_Open(1234);
??? in = SDLNet_AllocPacket(65535);
??? keystate = SDL_GetKeyState(NULL);
??? printf(“SERVER STARTED\n”);
fflush(stdout);
??? while (!keystate[SDLK_ESCAPE]) {
??? ??? if
(SDLNet_UDP_Recv(sock, in)) {
??? ??? ???
printf(“MSG: [%s]\n”,in->data); fflush(stdout);
??? ??? }
??? ??? SDL_Delay(10);
??? ??? SDL_PumpEvents();
??? ??? keystate =
SDL_GetKeyState(NULL);
??? }
??? printf(“SERVER STOPPED\n”);
fflush(stdout);
??? SDLNet_FreePacket(in);
??? SDLNet_UDP_Close(sock);
??? SDLNet_Quit();
??? SDL_Quit();
??? return 0;
}

CLIENT:
#include “SDL.h”
#include “SDL_net.h”

int main(int argc, char *argv[]) {
??? IPaddress ipaddress;
??? UDPsocket sock;
??? UDPpacket *out;
??? SDL_Init(SDL_INIT_VIDEO);
??? SDLNet_Init();
???
SDLNet_ResolveHost(&ipaddress,“192.168.0.255”,1234);
??? sock = SDLNet_UDP_Open(0);
??? out = SDLNet_AllocPacket(65535);
??? SDLNet_UDP_Bind(sock, 0,
&ipaddress);
??? printf(“CLIENT STARTED\n”);
fflush(stdout);
??? sprintf(out->data,“HELLO! CAN ANYBODY
HEAR ME?”);
??? SDLNet_UDP_Send(sock,0,out);
??? printf(“CLIENT STOPPED\n”);
fflush(stdout);
??? SDLNet_FreePacket(out);
??? SDLNet_UDP_Close(sock);
??? SDLNet_Quit();
??? SDL_Quit();
??? return 0;
}


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

  Need a Holiday? Win a $10,000 Holiday of your choice. Enter now.http://us.lrd.yahoo.com/_ylc=X3oDMTJxN2x2ZmNpBF9zAzIwMjM2MTY2MTMEdG1fZG1lY2gDVGV4dCBMaW5rBHRtX2xuawNVMTEwMzk3NwR0bV9uZXQDWWFob28hBHRtX3BvcwN0YWdsaW5lBHRtX3BwdHkDYXVueg--/SIG=14600t3ni/**http%3A//au.rd.yahoo.com/mail/tagline/creativeholidays/*http%3A//au.docs.yahoo.com/homepageset/%3Fp1=other%26p2=au%26p3=mailtagline

Thanks for the reply.

I adjusted the code to do what you said here, bypassing the bind and just
assigning the address to the packet directly, but I had the same results.
The server never registered the message. I just tested the exact same code
using the server’s IP, trying to do it the way it is normally. It failed as
well. So it might not be the broadcast that is failing. It might be my
implimentation of the client/server.

I agree that 65535 seems paranoid. I copied that from the UDP examples in
the documentation. I figured if it worked for them, it should work in this.

So does anyone else see any reason why this might fail? Is it because I
kill the client right away? Should I pause to make sure the UDP packet gets
sent?

“Brian” <brian.ripoff at gmail.com> wrote in message
news:a65cfd0f0906021132j1189b0e9w2f35da8182c2b690 at mail.gmail.com
I think the issue is that you are using SDLNet_UDP_Bind. You can
instead set the ip address of the packet manually, which works for me.

SDLNet_ResolveHost(&ipaddress,“255.255.255.255”,1234);
// …
out->address = ipaddress;

http://www.libsdl.org/cgi/docwiki.cgi/UDPpacket

Also, using 65535 as an incoming packet size seems a little paranoid =)On Tue, Jun 2, 2009 at 5:39 PM, Micah Brening <@Micah_Brening> wrote:

Greetings everyone!

I’m wanting to tinker with some network programming and so I was naturally
checking out SDL_net. I keep coming across people saying that SDL_net
supports
UDP broadcasting, but I have not been able to find an example of this.

I wrote a quick server and client. The client is supposed to resolve
255.255.255.255, but this test failed. I then tried using 192.168.0.255
which
192.168.0 is my work’s subnet. Here is the code I wrote:
<<>>

What I’ve done is using the example (see my previous post). After it’s working, I simply built up my code from there. The only thing I’ve changed is IP=255.255.255.255 (there’s a constant for that).

/_/_/_/_/_/_
www.huaonline.com
My Homepage is my Castle— On Wed, 3/6/09, Micah Brening <micah.brening at gmail.com> wrote:

From: Micah Brening <micah.brening at gmail.com>
Subject: Re: [SDL] SDL_net UDP Broadcast - Howto?
To: sdl at libsdl.org
Received: Wednesday, 3 June, 2009, 11:51 PM
Thanks for the reply.

I adjusted the code to do what you said here, bypassing the
bind and just assigning the address to the packet directly,
but I had the same results. The server never registered the
message.? I just tested the exact same code using the
server’s IP, trying to do it the way it is normally.?
It failed as well.? So it might not be the broadcast
that is failing.? It might be my implimentation of the
client/server.

I agree that 65535 seems paranoid.? I copied that from
the UDP examples in the documentation.? I figured if it
worked for them, it should work in this.

So does anyone else see any reason why this might
fail?? Is it because I kill the client right
away?? Should I pause to make sure the UDP packet gets
sent?

“Brian” <brian.ripoff at gmail.com>
wrote in message news:a65cfd0f0906021132j1189b0e9w2f35da8182c2b690 at mail.gmail.com
I think the issue is that you are using SDLNet_UDP_Bind.
You can
instead set the ip address of the packet manually, which
works for me.

SDLNet_ResolveHost(&ipaddress,“255.255.255.255”,1234);
// …
out->address = ipaddress;

http://www.libsdl.org/cgi/docwiki.cgi/UDPpacket

Also, using 65535 as an incoming packet size seems a little
paranoid =)

On Tue, Jun 2, 2009 at 5:39 PM, Micah Brening <micah.brening at gmail.com> wrote:

Greetings everyone!

I’m wanting to tinker with some network programming
and so I was naturally
checking out SDL_net. I keep coming across people
saying that SDL_net supports
UDP broadcasting, but I have not been able to find an
example of this.

I wrote a quick server and client. The client is
supposed to resolve
255.255.255.255, but this test failed. I then tried
using 192.168.0.255 which
192.168.0 is my work’s subnet. Here is the code I
wrote:
<<>>


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

  Need a Holiday? Win a $10,000 Holiday of your choice. Enter now.http://us.lrd.yahoo.com/_ylc=X3oDMTJxN2x2ZmNpBF9zAzIwMjM2MTY2MTMEdG1fZG1lY2gDVGV4dCBMaW5rBHRtX2xuawNVMTEwMzk3NwR0bV9uZXQDWWFob28hBHRtX3BvcwN0YWdsaW5lBHRtX3BwdHkDYXVueg--/SIG=14600t3ni/**http%3A//au.rd.yahoo.com/mail/tagline/creativeholidays/*http%3A//au.docs.yahoo.com/homepageset/%3Fp1=other%26p2=au%26p3=mailtagline