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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:06:10+00:00 2026-05-21T18:06:10+00:00

this is my first question and I’m not English native speaker so sorry for

  • 0

this is my first question and I’m not English native speaker so sorry for my (maybe) inaccurate English.

I’m implementing real-time network engine and using Socket.xxxAsync() method.

I made a UDP socket at server-side like this.

m_udpSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
m_udpSock.Bind(new IPEndPoint(IPAddress.Any, udpPort));
SocketAsyncEventArgs udpRecvArg = new SocketAsyncEventArgs();
udpRecvLoopStart(m_udpSock, udpRecvArg);

(udpPort is known to client.)

    private void udpRecvLoopStart(Socket udpSocket, SocketAsyncEventArgs udpRecvArg)
    {            
        udpRecvArg.AcceptSocket = udpSocket;
        byte[] udpRecvBuffer = new byte[NetworkEngineConst.MsgSizeMax];
        udpRecvArg.SetBuffer(udpRecvBuffer, 0, udpRecvBuffer.Length);
        udpRecvArg.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
        udpRecvArg.Completed += new EventHandler<SocketAsyncEventArgs>(udpRecvArg_Completed);

        udpRecv(udpRecvArg);
    }

    private void udpRecv(SocketAsyncEventArgs udpRecvArg)
    { 
        bool synchronous = false;
        try
        {                
            synchronous = !udpRecvArg.AcceptSocket.ReceiveFromAsync(udpRecvArg);
        }
        catch (Exception e)
        {
            OnIOError("recvUdp()\n" + e.ToString());
            return;
        }
        if (synchronous)
            udpRecvArg_Completed(this, udpRecvArg);
    }

Completed event handler is this:

    void udpRecvArg_Completed(object sender, SocketAsyncEventArgs udpRecvArg)
    {
        EndPoint udpEp = udpRecvArg.RemoteEndPoint;            
        string msg = Encoding.UTF8.GetString(udpRecvArg.Buffer, 14, udpRecvArg.BytesTransferred - 14);
        Debug.WriteLine(udpEp + " " + msg);
        udpRecv(udpRecvArg);            
    }

( first 14 bytes are message prefix,CRC and sequence number)

And two(or more) clients send UDP packet to server.
( sending rate is hundreds per second )

For example, client1(192.168.0.1:50113) sends “11111111111111111111111111111”,
client2(192.168.0.1:59368) sends “2”.

But sometimes(not always) endpoint is wrong. log is like below:

192.168.0.1:50113 11111111111111111111111111111

192.168.0.1:50113 11111111111111111111111111111

192.168.0.1:50113 11111111111111111111111111111

192.168.0.1:59368 2

192.168.0.1:59368 2

192.168.0.1:50113 11111111111111111111111111111

192.168.0.1:59368 11111111111111111111111111111 <- wrong part

192.168.0.1:59368 2

192.168.0.1:50113 11111111111111111111111111111

192.168.0.1:50113 11111111111111111111111111111

192.168.0.1:50113 11111111111111111111111111111

192.168.0.1:59368 2

192.168.0.1:50113 11111111111111111111111111111

192.168.0.1:59368 2

192.168.0.1:59368 2

I doubt packet error, but there was no error in packet(I confirmed with Wireshark)

Is this a bug ?

(Add)

I tried

        m_udpSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        m_udpSock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
        m_udpSock.Bind(new IPEndPoint(IPAddress.Any, udpPort));

and replaced ReceiveFromAsync() with ReceiveMessageFromAsync().

but SocketAsyncEventArgs.ReceiveMessageFromPacketInfo.Address is null

(Add)
ReceiveMessageFromAsync() problem is a bug.

It is fixed in the .NET framework 4.0

here

thank you.

  • 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-21T18:06:11+00:00Added an answer on May 21, 2026 at 6:06 pm

    Maybe try using Socket.ReceiveMessageFromAsync and then get the packet data using ReceiveMessageFromPacketInfo .

    FYI this is an alternative way to do it (see the Edit in the answer)

    HTH

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

Sidebar

Related Questions

this is my first question here :) I know that I should not check
First: This is not a repeat of this question . Same script, but different
This is my first question so please be patient :) Background: I'm implementing an
This is my first question after leeching over here for some time.. So spare
this is my first question here and if its format is not what is
This is my first question on the site and I hope it's not too
this is my first question.. so, here we go. i have a site, 100%
This is my first question :). Im writing a little twitter app in PHP
This is my first question. Thanks to everyone who contributes to this site, it's
This is my first question on stackoverflow. I just wonder why my getJSON code

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.