UDP Broadcast issues.
From: jkannen (jkannen_at_discussions.microsoft.com)
Date: 10/19/04
- Next message: Brad Williamson [MSFT]: "Re: Where To find LSP???"
- Previous message: James Jenkins: "Re: network Interface Descriptions & Names"
- Next in thread: Arkady Frenkel: "Re: UDP Broadcast issues."
- Reply: Arkady Frenkel: "Re: UDP Broadcast issues."
- Reply: Alun Jones [MSFT]: "Re: UDP Broadcast issues."
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 19 Oct 2004 09:19:01 -0700
I am trying to write a UDP Server and Client. The UDP Server should
broadcast its messages on MY_PORT and the Client should listen to those
messages, but I don't ever seem to receive the broadcast messages on the
client-end of things. Any ideas whats wrong with this code?
Also, should I be able to "hear" broadcast messages if I'm running the
server and the client on the same machine, through the same NIC? (Right now
I'm testing from one machine - but I'm wondering if thats part of the issue,
although I've tested this a bit with other peoples machines, but then maybe
my code wasn't in the right configuration...)
To set up the UDP Server to send out broadcast messages, I am doing the
following:
// Create our listening socket, we want internet protocol
// datagram sockets and IP
sListen = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
my_data_struct incoming;
int iDataSize = sizeof(incoming);
int iRetVal = recv(m_Socket, (char *) &incoming, iDataSize, 0);
... But this last call never returns - I don't receive any of the data, even
though I can see that the server is sending packets out...
// check for errors
if (sListen == SOCKET_ERROR)
{
TRACE("socket() failed: %d\n", WSAGetLastError());
return FALSE;
}
BOOL optval=TRUE;
int optlen=sizeof(optval);
if (setsockopt(sListen, SOL_SOCKET, SO_BROADCAST, (char *) &optval, optlen)
== SOCKET_ERROR)
{
TRACE("setsockopt() failed: %d\n", WSAGetLastError());
return FALSE;
}
struct sockaddr_in remote;
my_data_struct data;
// Select the local interface and bind to it
remote.sin_addr.s_addr = htonl(INADDR_BROADCAST); // take anyone who wants
to connect
remote.sin_family = AF_INET; // accept internet protocols
remote.sin_port = htons(MY_PORT); // listen on this port
memset(&data, 0, sizeof(data));
data.time = time(NULL);
int retVal;
retVal = sendto(sListen, (char *) &data, sizeof(data), 0, (sockaddr *)
&remote, sizeof(remote));
---------------------------------------------------------------------------------
And the client is set up as so:
// Create the socket, and attempt to connect to the server
// Create our listening socket, we want internet protocol
// datagram based sockets and IP
m_Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (m_Socket == INVALID_SOCKET)
{
SetLastError(WSAGetLastError());
m_Socket = NULL;
return FALSE;
}
struct sockaddr_in local;
// Clear the memory
memset(&local, 0, sizeof(local));
local.sin_family = AF_INET;
local.sin_addr.s_addr = htons(INADDR_ANY);
local.sin_port = htons(iPort);
/* Bind to the broadcast port */
if (bind(m_Socket, (struct sockaddr *) &local, sizeof(local)) < 0)
{
// TODO: give the user more information...
TRACE("bind() failed: %d\n", WSAGetLastError());
closesocket(m_Socket);
m_Socket = NULL;
return FALSE;
}
- Next message: Brad Williamson [MSFT]: "Re: Where To find LSP???"
- Previous message: James Jenkins: "Re: network Interface Descriptions & Names"
- Next in thread: Arkady Frenkel: "Re: UDP Broadcast issues."
- Reply: Arkady Frenkel: "Re: UDP Broadcast issues."
- Reply: Alun Jones [MSFT]: "Re: UDP Broadcast issues."
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|