UDP Broadcast issues.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: jkannen (jkannen_at_discussions.microsoft.com)
Date: 10/19/04


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;
}



Relevant Pages

  • Confused about network broadcasts
    ... What I'm doing is, on the client, to create a broadcast socket, then ... send out a string which the server can recognise. ...
    (comp.sys.acorn.programmer)
  • Re: Confused about network broadcasts
    ... What I'm doing is, on the client, to create a broadcast socket, then ... send out a string which the server can recognise. ...
    (comp.sys.acorn.programmer)
  • Re: UDP Broadcast issues.
    ... Take broadcast example from Platform SDK or MSDN, you client (as you ... > I am trying to write a UDP Server and Client. ...
    (microsoft.public.win32.programmer.networks)
  • Re: Best method for an NT service and python to interact?
    ... unsuspecting patrons the following literary masterpiece: ... the client does it and the NT service can stay very ... Setting up a simple UDP server is a great idea and I ...
    (comp.lang.python)
  • Re: A question about UDP packet receive
    ... I created a UDP server by using $socket = ... the client send me a reqest, then server return a "hello string". ... I suggest that you use EITHER stream_socket_recvfrom OR fread. ... It doesn't sound like you really want the buffering. ...
    (comp.lang.php)