Chris Ritchey
2003-07-03 16:20:03 UTC
I'll post the code at the bottom of the post. Whenever I try to
retreive a udp packet sent via broadcast I get the errorcode
EFAULT(bad address). The same program sends out the packets without
any complaints but it fails when retreiving, I'm lost as to why it
would report this error. It returns the number of bytes correctly but
returns the char* buffer as NULL. Any help would be appriciated and I
wouldn't be supprised if I was just missing something simple with
broacasting as I am still learning some aspects of socket
programiing.Thanks in advance to all whom reply and here is the code:
int Communications::GetBroadcastSocket()
{
int toReturn;
int on = 1; // flag to turn on socket options
memset(&broadcast, 0, sizeof(broadcast));
broadcast.sin_family = AF_INET;
broadcast.sin_port = PORTNUM + 1;
broadcast.sin_addr.s_addr = inet_addr(BCAST_ADDR);
if((toReturn = socket(AF_INET, SOCK_DGRAM, 0)) == -1) return -1;
if( SetNonBlocking(toReturn) < 0 ||
setsockopt(toReturn, SOL_SOCKET, SO_REUSEADDR, &on,
sizeof(on)) < 0 ||
setsockopt(toReturn, SOL_SOCKET, SO_BROADCAST, &on,
sizeof(on)) < 0 ||
bind(toReturn, (sockaddr *) &broadcast, sizeof(broadcast)) <
0)
{
close(toReturn);
return -1;
}
return toReturn;
}
retreive a udp packet sent via broadcast I get the errorcode
EFAULT(bad address). The same program sends out the packets without
any complaints but it fails when retreiving, I'm lost as to why it
would report this error. It returns the number of bytes correctly but
returns the char* buffer as NULL. Any help would be appriciated and I
wouldn't be supprised if I was just missing something simple with
broacasting as I am still learning some aspects of socket
programiing.Thanks in advance to all whom reply and here is the code:
int Communications::GetBroadcastSocket()
{
int toReturn;
int on = 1; // flag to turn on socket options
memset(&broadcast, 0, sizeof(broadcast));
broadcast.sin_family = AF_INET;
broadcast.sin_port = PORTNUM + 1;
broadcast.sin_addr.s_addr = inet_addr(BCAST_ADDR);
if((toReturn = socket(AF_INET, SOCK_DGRAM, 0)) == -1) return -1;
if( SetNonBlocking(toReturn) < 0 ||
setsockopt(toReturn, SOL_SOCKET, SO_REUSEADDR, &on,
sizeof(on)) < 0 ||
setsockopt(toReturn, SOL_SOCKET, SO_BROADCAST, &on,
sizeof(on)) < 0 ||
bind(toReturn, (sockaddr *) &broadcast, sizeof(broadcast)) <
0)
{
close(toReturn);
return -1;
}
return toReturn;
}