473,805 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(AddressF amily.InterNetw ork,
SocketType.Dgra m, ProtocolType.Ud p);
try
{
// Send data to the broadcast address
IPEndPoint broadcastIEP = new IPEndPoint(IPAd dress.Broadcast ,
port);
socket.Connect( broadcastIEP);
socket.Send(dat a);

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

return ASCIIEncoding.A SCII.GetString( buffer);
}
System.Threadin g.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 "ReceiveFro m", 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 6551
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.c omwrote:
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_comm ie" 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.Broad cast
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.ReceiveT imeout 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
7850
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 that UdpClient.Receive method always throws following exception, even though I verified that message was successfuly received by devices and responses were sent back:
2
2416
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 doing so, a device (of which I will know nothing about) will receive my broadcast and send out a broadcast of its own. Using Ethereal (LAN packet sniffer), I can see my broadcast, and I can see the device broadcast. I cannot however receive...
2
1976
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 postgres: LOG: PGSTATBUFF: Warning - receive buffer full May 12 15:15:52 gnvdb01 postgres: LOG: PGSTATBUFF: Warning - receive buffer full May 12 15:15:52 gnvdb01 postgres: LOG: PGSTATBUFF: Warning - receive buffer full
2
2593
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 coming in and that its not being blocked because there is another app on my machine that is getting the message. Please help.
2
5789
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 who answers the broadcast, so i know that it sends a reply, but i cant figure out why my C# client cant receive it.. Any input on whats wrong with my code will be appreciated //My code...
3
2310
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 args) {
3
3419
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 API RegisterWindowMessage but no luck.
0
1654
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 executable should only receive the datagrams from one device. Is this possible? Lets say the devices are 10.0.0.1 and 10.0.0.2. I have already tried something like this: UdpClient udpClient = new UdpClient(1234);
0
1845
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 still in the planning stages, but I'm confused about the best way to send / receive data. On program startup I plan on sending a broadcast ping and then listening for the 8 device's responses. What is the best way to receive the data? I've done some...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10609
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10360
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10366
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10105
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9185
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7646
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.