Problem with System.Net.UdpClient and multicast receive

Posted on 2011-05-04
Last Modified: 2013-11-07
I am developing a VB.NET application that needs to monitor a multicast group. To do this I am using the System.Net.UdpClient class. The multicast group messages are of a fixed size (48 bytes) and each member of the group sends the message about one every second. In my test setup I have a single device sending the group message and my application is monitoring the messages.

What I see using WireShark is exactly what I expect, a multicast message with 48 bytes in the data element of the message.

The application uses the following code to block (in a separate thread) waiting for the messages:
   While Not done
      Dim bytes As Byte() = listener.Receive(groupEP)

      ... process message here
   End While

Open in new window

Typically, the length of "bytes" is the size of the message (48 bytes), however, sometimes many more bytes are returned, even though the time delay between the previous good message and the long one is consistently about 1 second. The data in the buffer appears to be a partial message (first few bytes missing) and then many well formed messages.

As I said, I have examined the network data using WireShark and all the messages are well formed. Also, there are only messages from my single device.

Am I missing something obvious here?

Any pointers as to what may be going wrong would be much appreciated.
Question by:Sid Price
    LVL 11

    Expert Comment

    If there are a lot of messages coming in quickly then the listener will receive bytes currently flushed in the underlying stream which could contain more or less a full message. Because you messages are tiny (48 bytes) it's more than likely reading into the next message's bytes. I only know this for a fact with TcpClient when you are are receiving in a loop so it's a good starting point.

    Author Comment

    by:Sid Price
    Thank you for the quick reply.

    I don't think this is the issue for several reasons.

    First, as I mentioned in my original post a message is sent about once every second.

    Second, I have debug messages in the code that emit a message each time a multicast message is received. These come out at the right period, plus the period between the last good message and the very long one is also about a second. The number of messages I get under the error condition is around 30!

    Third, WireShark confirms the rate of messages on the wire at one every second or so.

    LVL 11

    Expert Comment

    SUPER old article but it's directly related to what I was trying to explain but with code samples and much more detail.

    Author Comment

    by:Sid Price
    Thanks again. I will implement a sockets level receiver and see if I get better results.

    Accepted Solution

    It appears that the problem was entirely of my making in the device generating the multicast messages and that there is in fact nothing wrong with the .NET UdpClient class at all. After finding a subtle bug on the device the .NET based application receiving the messages works very well.


    Author Closing Comment

    by:Sid Price
    The problem was in another element of the system and not in the area that the question covered.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now