Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6923631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:33:35+00:00 2026-05-27T10:33:35+00:00

I am using System.Net.Sockets Socket class to receive UDP datagrams. I wanted to know

  • 0

I am using System.Net.Sockets Socket class to receive UDP datagrams.
I wanted to know the exact length of the datagram received in order to check the validity of the datagram.

Socket.Receive(Byte()) method documentation says:

If you are using a connectionless Socket, Receive will read the first queued datagram from the destination address you specify in the Connect method. If the datagram you receive is larger than the size of the buffer parameter, buffer gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.

Socket.Available property gives the total of bytes available to be read, This is sums up the size of all queued datagrams.

Is there a way I can find out the size of the next datagram?

public static void Receive()
{
    Byte[] buf, discardbuf;
    const int paklen = 32;                  //correct packet length
    int l;

    buf = new Byte[paklen];
    discardbuf = new Byte[4096];

    while (rcv_enable)
    {
        l = soc.Available;
        if (l == 0) continue;
        if (l == paklen) soc.Receive(buf);  //receive the correct packet
        else soc.Receive(discardbuf);       //discard otherwise
        Thread.Sleep(200);
        Console.WriteLine("Received {0}", l);
    };
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:33:35+00:00Added an answer on May 27, 2026 at 10:33 am

    I’m going to assume that you’ve developed your own protocol and are expecting datagrams of a pre-known size and you want to guard yourself against rogue packets.

    Since performance seems to be an issue and you want to avoid exceptions I’d have a look at the overload of Receive which returns the raw socket error instead of throwing exceptions. The MSDN documentation does (incorrectly?) state that this method too will throw an exception but I don’t think that is the case. It’s definitely worth a try.

    SocketError error;
    byte[] buffer = new byte[512]; // Custom protocol max/fixed message size
    int c = this.Receive(buffer, 0, buffer.Length, SocketFlags.None, out error);
    
    if (error != SocketError.Success)
    {
        if(error == SocketError.MessageSize)
        {
            // The message was to large to fit in our buffer
        }
    }
    

    Use a buffer of a pre-known sane size and use the overload to check the SocketError error code in order to determine whether or not the read succeeded or if you sholud drop the package.

    If, however your own protocol can send datagrams of unknown sizes up to the limit of the maximum datagram size you have no choice other than allocating a buffer large enough to fit the largest packet (65k) (you could use buffer pooling to avoid memory issues depending on your code).

    Also check out the SocketFlags enum, it contains some members which may be of interest to you such as the Partial and Peek members.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using C#.Net and the Socket class from the System.Net.Sockets namespace. I'm using the
I'm trying to mock out the System.net.Sockets.Socket class in C# - I tried using
I'm using System.Net.Sockets.TcpClient class but whenever I send custom packet over the network I'm
using System; using System.IO; using System.Net; using System.Text.RegularExpressions; namespace Working { class Program4 {
I have an asynchronous socket listener class like this: using System; using System.Collections.Generic; using
I'm working on a web client. Here is the code: using System.Net; using System.Net.Sockets;
Hi I'm using System.Net.Mail to send some HTML formatted emails. What is the correct
My domain model is using System.Net.Uri to represent URLs, and System.Drawing.Color to represent colors.
How can I send an email using SYstem.Net.Mail to an email address that is
I get security exception when using System.Net.WebClient to do HTTP requests, which is due

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.