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 7488625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:00:08+00:00 2026-05-29T15:00:08+00:00

Whenever my server application receives a packet which is too big for the buffer,

  • 0

Whenever my server application receives a packet which is too big for the buffer, it crashes when calling Socket.EndReceiveFrom. This is what my code looks like:

static EndPoint remote = new IPEndPoint(IPAddress.Any, 0);
static Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

static void Main(string[] args)
{
    socket.Bind(new IPEndPoint(IPAddress.Any, 1234));
    Receive();
    Console.WriteLine("Receiving ...");

    for (; ; ) ;
}

static void Receive()
{
    byte[] buffer = new byte[64];
    socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remote, ReceiveFromCallback, buffer);
}

static void ReceiveFromCallback(IAsyncResult result)
{
    EndPoint theRemote = new IPEndPoint(IPAddress.Any, 0);
    byte[] buffer = (byte[])result.AsyncState;

    // the following for loop is irrelevant for this question - it simply outputs the received data as hex numbers
    for (int x = 0; x < 8; x++)
    {
        Console.Write("  ");
        for (int y = 0; y < 8; y++)
        {
            string hex = Convert.ToString(buffer[(x * 8) + y], 16);
            Console.Write((hex.Length == 1 ? "0" : "") + hex + " ");
        }
        Console.WriteLine();
    }

    // the following line of code crashes the application if the received message is larger than 64 bytes
    socket.EndReceiveFrom(result, ref theRemote);
}

If the received packets is larger than 64 bytes, my application throws a SocketException saying the following:

A message which was sent over the datagram socket was too large for
the internal data buffer or another network limit, or the buffer used
for receiving the datagram was too small.

Notice that this is not the original message text. Since I’m working with the German version of Visual Studio, I had to translate it back.

ReceiveFromCallback’s “buffer” variable only contains the first 64 bytes of the message if it’s larger than that. So checking whether “buffer” contains more than 64 bytes is not an option.

So my questions are:

Do I need to call EndReceiveFrom(); why should it be called? How can I check whether the received message is too big for the buffer?

  • 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-29T15:00:09+00:00Added an answer on May 29, 2026 at 3:00 pm

    From MSDN:

    Within the callback method, call the AsyncState method of the IAsyncResult to obtain the state object passed to the BeginReceiveFrom method. Extract the receiving Socket from this state object. After obtaining the Socket, you can call the EndReceiveFrom method to successfully complete the read operation and return the number of bytes read.

    Therefore, you should call EndReceiveFrom in the callback (like you do). Simply catch the exception and your application wont “crash”.

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

Sidebar

Related Questions

I want to create an application which will have a client and server components.
i got this error whenever i trying execute my project which is used database
I just deployed an ASP.NET MVC 3 application to our staging server. Whenever an
At work, we have several applications with databases in a centralized SQL server. Whenever
I have an error occuring frequently from our community server installation whenever the googlesitemap.ashx
Whenever I restore a backup of my database in SQL Server I am presented
My server time is in GMT, and I do the following whenever someone comes
I have finally managed to get javan-whenever gem working on my site5 server, and
I am using wamp server 2.0 on Windows XP. Whenever I create a table
I have CruiseControl.NET Version 1.4 set up on my development server. Whenever a developer

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.