473,396 Members | 1,797 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,396 software developers and data experts.

Receive replies to a UDP broadcast

Hi all,

I'm currently trying to receive replies to a UDP broadcast that I sent
and I can't get it to work. I know the datagrams are sent as I see
them with WireShark, but my C# code does not see them.
Here is how I intended things to work:

Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
try
{
// Send data to the broadcast address
IPEndPoint broadcastIEP = new IPEndPoint(IPAddress.Broadcast,
port);
socket.Connect(broadcastIEP);
socket.Send(data);

// Wait for someone to respond
for (int i = 0; i < 50; i++)
{
int availableBytes = socket.Available;
if (availableBytes 0)
{
byte[] buffer = new byte[availableBytes];
socket.Receive(buffer, 0, availableBytes,
SocketFlags.None);

return ASCIIEncoding.ASCII.GetString(buffer);
}
System.Threading.Thread.Sleep(100);
}
return "Nobody replied";
}
finally
{
socket.Close();
}
The intent is to send a UDP broadcast and wait for the first machine
that replies. The wait should stop after a while.
I must be doing something wrong here, but I'm quite lost as to what I
should be doing. I tried "ReceiveFrom", but it is a blocking call and
it never seems to return. I see the datagrams coming through (using
WireShark) but ReceiveFrom does not return.

Thanks for your help
Cheers
Olivier
Nov 5 '08 #1
2 6499
There is a whole slew of things that could be the issue here. In the
first place, you should be listening before you send. You'll need to
make another thread to do that or use the BeginReceive method. Second,
don't use the Available property. It's busted. Use the return value on
Receive to know how much data was put into your buffer instead. Third,
I'm pretty sure you'll need to bind to a port (with the Bind method)
to receive any data. In other words, you may need two sockets here.
One to send and one to receive. You may also need to set the option on
the sockets to allow multiple sockets to bind to the same address.
Nov 5 '08 #2
On Wed, 05 Nov 2008 04:36:52 -0800, OBones <ob****@gmail.comwrote:
Hi all,

I'm currently trying to receive replies to a UDP broadcast that I sent
and I can't get it to work. I know the datagrams are sent as I see
them with WireShark, but my C# code does not see them.
The reply from the person posting as "not_a_commie" has a lot of good
suggestions in it. I think that the one most directly applicable to the
immediate issue is that you don't try to receive from the socket until
after you've sent something. With UDP, if you aren't ready to receive
data already when someone else sends data, it's entirely possible that the
network driver will just drop the data altogether and you'll never see it.

You _should_ be able to send and receive broadcast data on the same
socket, but you need to bind the socket to the same IPAddress.Broadcast
address for that to work. Of course, for you to be ready to receive at
the same time you send data on the socket, you'll have to either use the
asynchronous API and call BeginReceive() or have another thread that calls
Receive() before you call Send().

Finally, it's not so much that the Available property is broken, as it is
that it just doesn't really do what most people expect it to do. Network
sockets don't behave exactly the same as other i/o interfaces; in
particular data can come and go without any action on the part of the
client code, depending on the needs of the network driver to manage all of
the network i/o going on at the time. Just because the Available property
indicates there's data to be read, that doesn't mean that by the time you
try to read it, it will still be there.

So, you need a different way of implementing your timeout logic. Since
you're using UDP, I'd say you could probably get away with just setting
the Socket.ReceiveTimeout property (using that with TCP introduces as many
problems as it solves, so you're better off without it in that case...but
UDP should be fine).

I'm not sure that even between our two posts, you have every bit of
information you're going to need to get this work right, but hopefully
it's a nudge in the right direction. :)

Pete
Nov 6 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Steve | last post by:
Please take a look at the simple code segment below and advise me what is wrong. According to the help and examples I've seen it should work unless I misunderstand something. The problem is...
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: Robert Treat | last post by:
We got a few of these on a 7.3 server this afternoon. The server was completed pegged on i/o activity (both physical disks, including the one the db doesn't live on) May 12 15:15:51 gnvdb01...
2
by: Dave | last post by:
Does anyone have an example of how to receive a connectionless(udp) broadcast in c#. I have tried everything i can possibly think of and it won't recieve the message. I know that the message is...
2
by: Daniel Lindberg | last post by:
Hi, im a beginner in C# and i just cant seem to get my udp broadcasting to work. What happens is that it just locks down on the Receive call and never gets any data. I have the code for the server...
3
by: greenxiar | last post by:
My Computer is Win2K3 R2, Code is: using System; using System.Net; using System.Net.Sockets; using System.Threading; namespace TechUDPBroadcast { class Program { static void Main(string...
3
by: siujoe | last post by:
Hi, My app is written in vb.net 2003, I need to receive broadcast messages sent from another application written in VC++6. It doesn't seem i can import system.messaging, I also tried windows...
0
by: Johan | last post by:
Suppose: There are two devices on my network that both broadcast UDP datagrams (and they both use the same portnumber 1234). I want to create two executables that use UdpClient internally. Each...
0
by: cweckel2000 | last post by:
Hi all, I'm new to developing applications using networking protocols and need to figure out how to send/receive data between a server and 8 different devices. I don't have much code as I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.