By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

UDP broadcast

P: n/a
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!
Nov 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Frank Esser" <Mi*****@nurfuerspam.de> wrote in message
news:ef****************@TK2MSFTNGP14.phx.gbl...
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!

Nov 17 '05 #2

P: n/a
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> schrieb
im Newsbeitrag news:Oj**************@tk2msftngp13.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Frank Esser" <Mi*****@nurfuerspam.de> wrote in message
news:ef****************@TK2MSFTNGP14.phx.gbl...
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!


Nov 17 '05 #3

P: n/a
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 Esser" <Mi*****@nurfuerspam.de> schrieb im Newsbeitrag
news:ef****************@TK2MSFTNGP14.phx.gbl...
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!

Nov 17 '05 #4

P: n/a
Frank Esser wrote:
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
Nov 17 '05 #5

P: n/a


Frank Esser wrote:
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.GetByt es("Test"));

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

Eyal.

Nov 17 '05 #6

P: n/a


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


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

Eyal.

Nov 17 '05 #7

P: n/a


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


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

Eyal.

Nov 17 '05 #8

P: n/a


Frank Esser wrote:
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.GetByt es("Test"));

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

Eyal.

Nov 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.