UDP broadcast

  • Thread starter Thread starter Frank Esser
  • Start date Start date
F

Frank Esser

Hello!

I have got machines that answer a certain UDP broadcast request with certain
information about them.

Some years ago I wrote a VB6 application that just sent out this UDP
broadcast request and received all machine information to display all
available machines in a grid.

Now I want to write the same application in C# but so far with no success.

There is a udp client available in the .NET framework but how can I use it
for my purpose?

I think it must be a matter of not more than 10 lines of code but I did not
find a hint so far how to do it... Can anybody help?

Thanks!
 
Frank,

You want to set the EnableBroadcast property on the UdpClient instance
set to true. Once you do that, you want to send your request to the IP
address of 255.255.255.255. From the documentation for the EnableBroadcast
property on the UdpClient class:
Broadcasting is limited to a specific subnet. You can broadcast to your
local subnet by sending a packet to 255.255.255.255; or, you can use the
directed broadcast address, which is the network portion of an Internet
Protocol (IP) address with all bits set in the host portion. For example, if
your IP address is 192.168.1.40 (a Class C address, with the network portion
as the first three octets, and the host portion is the last octet), your
directed broadcast address is 192.168.1.255.

Hope this helps.
 
Thanks, Nicholas!

But in fact I do not know how to send and receive. Should I bind or connect
or just send to the braodcast adress and if I receive an answer from any
client is it necessary to accept this connection in any way and how can I
then receive the next answers from other clients and so on.

It would be helpful for me just to have a few snippets of code...


Nicholas Paldino said:
Frank,

You want to set the EnableBroadcast property on the UdpClient instance
set to true. Once you do that, you want to send your request to the IP
address of 255.255.255.255. From the documentation for the
EnableBroadcast property on the UdpClient class:
Broadcasting is limited to a specific subnet. You can broadcast to your
local subnet by sending a packet to 255.255.255.255; or, you can use the
directed broadcast address, which is the network portion of an Internet
Protocol (IP) address with all bits set in the host portion. For example,
if your IP address is 192.168.1.40 (a Class C address, with the network
portion as the first three octets, and the host portion is the last
octet), your directed broadcast address is 192.168.1.255.

Hope this helps.




--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)





Frank Esser said:
Hello!

I have got machines that answer a certain UDP broadcast request with
certain information about them.

Some years ago I wrote a VB6 application that just sent out this UDP
broadcast request and received all machine information to display all
available machines in a grid.

Now I want to write the same application in C# but so far with no
success.

There is a udp client available in the .NET framework but how can I use
it for my purpose?

I think it must be a matter of not more than 10 lines of code but I did
not find a hint so far how to do it... Can anybody help?

Thanks!
 
Ok, I got it nearly. My problem: The udpclient is in blocking mode when
receiving data.
Is there a way of getting available bytes prior to call receive method?

Here is my code:

UdpClient udpClient = new UdpClient();


// broadcasts identification request message to the network

Byte[] sendBytes = Encoding.ASCII.GetBytes("0 1");

// set address to broadcast address and port to 8001

IPEndPoint RemoteIpEndPoint = new
IPEndPoint(IPAddress.Parse("255.255.255.255"), 8001);

// broadcast data

udpClient.Send(sendBytes, sendBytes.Length, RemoteIpEndPoint);

//IPEndPoint object will allow us to read datagrams sent from any source.

RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);


// Blocks until a message returns on this socket from a remote host.

string returnData = String.Empty;

do

{

Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);

returnData = Encoding.ASCII.GetString(receiveBytes);

MessageBox.Show(returnData);

} while (returnData != String.Empty);

// close udp client

udpClient.Close();
 
Frank said:
Hello!

I have got machines that answer a certain UDP broadcast request with certain
information about them.

Some years ago I wrote a VB6 application that just sent out this UDP
broadcast request and received all machine information to display all
available machines in a grid.

Now I want to write the same application in C# but so far with no success.

There is a udp client available in the .NET framework but how can I use it
for my purpose?

I think it must be a matter of not more than 10 lines of code but I did not
find a hint so far how to do it... Can anybody help?

Thanks!

Hi Frank, I am currently working on a small C#
project that involves automatic server discovery.
The method that we used was to have the servers
listening on a multicast address, and then once
they recieved the udp request they would reply to
the client.

UdpClient oListener = new
UdpClient(iPort,AddressFamily.InterNetwork);
oListener.JoinMulticastGroup(oGroupAddress);
IPEndPoint oGroupEP = new
IPEndPoint(oGroupAddress, iPort);
byte[] byBytes = oListener.Receive(ref oGroupEP);

Just posting this on the off chance that it might
give you another option to explore, whether it
helps or not is another thing :)

~Nick
 
Frank said:
Ok, I got it nearly. My problem: The udpclient is in blocking mode when
receiving data.
Is there a way of getting available bytes prior to call receive method?

Here is my code:

UdpClient udpClient = new UdpClient();


// broadcasts identification request message to the network

Byte[] sendBytes = Encoding.ASCII.GetBytes("0 1");

// set address to broadcast address and port to 8001

IPEndPoint RemoteIpEndPoint = new
IPEndPoint(IPAddress.Parse("255.255.255.255"), 8001);

// broadcast data

udpClient.Send(sendBytes, sendBytes.Length, RemoteIpEndPoint);

//IPEndPoint object will allow us to read datagrams sent from any source.

RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);


// Blocks until a message returns on this socket from a remote host.

string returnData = String.Empty;

do

{

Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);

returnData = Encoding.ASCII.GetString(receiveBytes);

MessageBox.Show(returnData);

} while (returnData != String.Empty);

// close udp client

udpClient.Close();

use Socket, its better.

using System.Net;
using System.Net.Sockets;

[STAThread]
static void Main(string[] args)
{
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
socket.Connect(new IPEndPoint(IPAddress.Broadcast, 8000));
socket.Send(System.Text.ASCIIEncoding.ASCII.GetBytes("Test"));

int availableBytes = socket.Available;
if (availableBytes > 0)
{
byte[] buffer = new byte[availableBytes];
socket.Receive(buffer, 0, availableBytes, SocketFlags.None);
}
}

Eyal.
 
Eyal said:
socket.Connect(new IPEndPoint(IPAddress.Broadcast, 8000));

oops, the ports should be 8001:
socket.Connect(new IPEndPoint(IPAddress.Broadcast, 8001));

Eyal.
 
Eyal said:
socket.Connect(new IPEndPoint(IPAddress.Broadcast, 8000));

oops, the port should be 8001:
socket.Connect(new IPEndPoint(IPAddress.Broadcast, 8001));

Eyal.
 
Frank said:
Ok, I got it nearly. My problem: The udpclient is in blocking mode when
receiving data.
Is there a way of getting available bytes prior to call receive method?

Here is my code:

UdpClient udpClient = new UdpClient();


// broadcasts identification request message to the network

Byte[] sendBytes = Encoding.ASCII.GetBytes("0 1");

// set address to broadcast address and port to 8001

IPEndPoint RemoteIpEndPoint = new
IPEndPoint(IPAddress.Parse("255.255.255.255"), 8001);

// broadcast data

udpClient.Send(sendBytes, sendBytes.Length, RemoteIpEndPoint);

//IPEndPoint object will allow us to read datagrams sent from any source.

RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);


// Blocks until a message returns on this socket from a remote host.

string returnData = String.Empty;

do

{

Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);

returnData = Encoding.ASCII.GetString(receiveBytes);

MessageBox.Show(returnData);

} while (returnData != String.Empty);

// close udp client

udpClient.Close();

Hi,
Use Socket, its better:

using System.Net;
using System.Net.Sockets;

[STAThread]
static void Main(string[] args)
{
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
socket.Bind(new IPEndPoint(IPAddress.Any, 8002/*the port on which we
receive responds*/));
socket.Connect(new IPEndPoint(IPAddress.Broadcast, 8001));
socket.Send(System.Text.ASCIIEncoding.ASCII.GetBytes("Test"));

int availableBytes = socket.Available;
if (availableBytes > 0)
{
byte[] buffer = new byte[availableBytes];
socket.Receive(buffer, 0, availableBytes, SocketFlags.None);
}
}

Eyal.
 
Back
Top