473,418 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

UDP broadcast

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
8 46048
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
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
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
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


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


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


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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Damir Hakimov | last post by:
Hi All! I'm writing a small programm which must send and service UDP broadcast packets. As start of it I looked at /usr/share/doc/python2.4/Examples/Demo/sockets/broadcast.py ...
6
by: pekspro | last post by:
I need some code that gets the address from a server. I read somewhere that you could do this by starting some broadcast server using UDP. The client should send an broadcast message, and when the...
2
by: gregory_may | last post by:
First the research links: IPv6 spec (look for 'jumbo payload'): http://www.cs-ipv6.lancs.ac.uk/ipv6/documents/rfcs/archive/rfc1883.txt IPv6 Sample C# Client/Server...
2
by: Shawn G. | last post by:
I'm going around in circles here and need some help. Scenario: I am using (have used) either the UDPClient class or Sockets class to broadcast some bytes to 255.255.255.255 (udp protocol). Upon...
2
by: Gunnar_Frenzel | last post by:
Hello, this might be an easy question, but I don't have any handy solution at hand. I have an application that is supposed to send UDP broadcast. So far so easy, I did: Socket sockSendBroadcast...
3
Sagittarius
by: Sagittarius | last post by:
Hi there. I have a problem concerning an UDP socket in C++ (Winsock). The next paragraphs is merely to explain the system I am working on. If U want to skip it, I have marked the question in...
9
by: Irmen de Jong | last post by:
Hello Sorry this might be a bit offtopic but I don't really know where else to post this question. If you could point me in the right direction that is much appreciated. I'm running into a...
2
by: Mali Findik | last post by:
Hi @ll, i've got problems with sending an UDP broadcast datagramm over two network interfaces. The code is like this: <Code> UdpClient client = new UdpClient(); IPEndPoint remoteEndPoint =...
1
by: raviskar | last post by:
Hi, I have a problem in receiving UDP broadcast data. We have a simple application which listens for the UDP broadcast data from the external servers. We can receive the UDP broadcast data from...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.