Since my last plea for help on this list I have continued to work on the
problem. I have made some progress, but it still does not work.
I have written two simple programs, one that sends a UDP broadcast packet, one
that recieves one. The sending one appears to be sending SOMETHING according
to my hub lights, but to what, where, I cannot say. The recieving one never
recieves anything, ever. What’s going on here?
Here is the source:
/udp_broadcast_recv.c/
#include <SDL/SDL.h>
#include <SDL/SDL_net.h>
#include <stdlib.h>
/***
- An ubersimple example of UDP broadcasting with SDL_net.
- Opens a UDP port to recieve UDP broadcast from port 9999, then waits
- for a packet.
- Tyler Montbriand, Feb. 2004
*/
#ifndef INADDR_BROADCAST
#error “You need SDL_net 1.2.5 or newer to send broadcasts!”
#endif
#define UDP_PORT 9999
#define CRYPTIC_MSG “Hold the mayo and pass the cosmic awareness.”
int main(int argc, char *argv[])
{
Uint16 udp_port=9999;
IPaddress bc_recv_ip={INADDR_ANY ,udp_port};
UDPsocket bc_recv=NULL;
UDPpacket *packet=NULL;
if(SDL_Init(SDL_INIT_TIMER)<0)
{
fprintf(stderr,“Couldn’t init SDL: %s\n”,SDL_GetError());
return(1);
}
else
atexit(SDL_Quit);
if(SDLNet_Init()<0)
{
fprintf(stderr,“Couldn’t initialize SDL_Net\n”);
return(1);
}
else
atexit(SDLNet_Quit);
bc_recv=SDLNet_UDP_Open(UDP_PORT);
if(bc_recv==NULL)
{
fprintf(stderr,“Couldn’t open bc_recv\n”);
SDLNet_UDP_Close(bc_recv);
return(1);
}
if(SDLNet_UDP_Bind(bc_recv,-1,&bc_recv_ip)<0)
{
fprintf(stderr,“Couldn’t bind broadcast recieving address\n”);
SDLNet_UDP_Close(bc_recv);
return(1);
}
fprintf(stderr,“Recieving port opened!\n”);
packet=SDLNet_AllocPacket(1024);
if(packet==NULL)
{
fprintf(stderr,“Couldn’t allocate packet!\n”);
SDLNet_UDP_Close(bc_recv);
return(1);
}
{
int tick=SDL_GetTicks();
int recieved=0;
fprintf(stderr,"Waiting for packet.\n");
while(((SDL_GetTicks()-tick)<10000)&&(!recieved))
{
if(SDLNet_UDP_Recv(bc_recv,packet)>0)
recieved=1;
else
SDL_Delay(100);
}
if(recieved)
fprintf(stderr,"UDP speaketh: %s\n",packet->data);
else
fprintf(stderr,"Timeout; no packet.\n");
}
SDLNet_UDP_Close(bc_recv);
SDLNet_FreePacket(packet);
return(0);
}
/udp_broadcast_send.c/
#include <SDL/SDL.h>
#include <SDL/SDL_net.h>
#include <stdlib.h>
#ifndef INADDR_BROADCAST
#error “You need SDL_net 1.2.5 or newer for broadcast support!”
#endif
/**
- An ubersimple example of UDP broadcasting with SDL_net.
- It opens a UDP broadcast socket and sends on port 9999
- Tyler Montbriand, Feb 2004
*/
#define UDP_PORT 9999
int main(int argc, char *argv[])
{
Uint16 udp_port=9999;
IPaddress bc_send_ip={INADDR_BROADCAST,udp_port};
UDPsocket bc_send=NULL;
UDPpacket *packet=NULL;
if(SDL_Init(SDL_INIT_TIMER)<0)
{
fprintf(stderr,“Couldn’t init SDL: %s\n”,SDL_GetError());
return(1);
}
else
atexit(SDL_Quit);
if(SDLNet_Init()<0)
{
fprintf(stderr,“Couldn’t initialize SDL_Net\n”);
return(1);
}
else
atexit(SDLNet_Quit);
bc_send=SDLNet_UDP_Open(UDP_PORT);
if(bc_send==NULL)
{
fprintf(stderr,“Couldn’t open bc_send\n”);
return(1);
}
if(SDLNet_UDP_Bind(bc_send,-1,&bc_send_ip)<0)
{
fprintf(stderr,“Couldn’t bind broadcast sending address\n”);
SDLNet_UDP_Close(bc_send);
return(1);
}
fprintf(stderr,“Broadcast socket opened!\n”);
packet=SDLNet_AllocPacket(1024);
if(packet==NULL)
{
fprintf(stderr,“Couldn’t allocate packet!\n”);
SDLNet_UDP_Close(bc_send);
return(1);
}
strcpy(packet->data,CRYPTIC_MSG);
packet->len=strlen(CRYPTIC_MSG)+1;
fprintf(stderr,“Sending packet of %d bytes\n”,packet->len);
if(SDLNet_UDP_Send(bc_send,0,packet)<0)
{
fprintf(stderr,“Couldn’t send packet!\n”);
SDLNet_UDP_Close(bc_send);
SDLNet_FreePacket(packet);
return(1);
}
SDLNet_UDP_Close(bc_send);
SDLNet_FreePacket(packet);
fprintf(stderr,“Done!\n”);
return(0);
}