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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:40:21+00:00 2026-06-03T16:40:21+00:00

Consider the code: class UPDServer { //start listener public void start() { UdpClient listener

  • 0

Consider the code:

class UPDServer {

    //start listener
    public void start() {
        UdpClient listener = new UdpClient(this._porta);
        IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 10000);

        State s = new State();
        s.listener = listener;
        s.ipEndPoint = ipEndPoint;

        // begin to listen again
        listener.BeginReceive(new AsyncCallback(ReceiveCallback), s);

   }

 //receive data
   public void ReceiveCallback(IAsyncResult ar)
   {
      // we got data
      State s = (State)(ar.AsyncState);

      Byte[] received = e.escuta.EndReceive(ar, ref e.ipEndPoint);
      string text = ""; = Encoding.ASCII.GetString(received);
      // ... do somenthing with text

      // begin to listen again
      s.listener.BeginReceive(new AsyncCallback(ReceiveCallback), e);
   }
}

On the code above there is a time span between the EndReceive and the next BeginReceive that no one is listening, i think if there is a message in this time span it will be lost.

Well, i guess there isw some kind o buffer, but even so if the buffer fill up in the time span where no one is listening for messages, messages will be lost.

I remember someone saying that this could be solved very simple by calling BeginReceive on the same end point several times, so i tried this:

   for( int x = 0; x < 5;x++) {
      escuta.BeginReceive(new AsyncCallback(ReceiveCallback), e);
   }

Oddly calling begin receive on the same socket does no throw errors but every time a message is received all five beginReceive fires and all five got the same message.

Is there any way to improve the code?

  • 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-06-03T16:40:23+00:00Added an answer on June 3, 2026 at 4:40 pm

    Firstly, there is no guarantee that you will get all your data when using UDP, so you have to make your code/protocol fault tolerant regardless.

    Unless your processing is very intensive and you’re expecting huge amounts of data to flood in you probably don’t need to do things differently. Just add some error detection code so you will know if your code is failing to cope.

    However, if you need to keep your receive loop fast, when you receive the data, copy it onto a queue for another thread to process so that you can begin reading again quickly. Or under some circumstances you may be able to write your end-receive so it can handle going re-entrant if several packets come in back to back.

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

Sidebar

Related Questions

Consider this code: class Foo { public void doIt(String... strs) { System.out.println(this is varargs);
Consider this code: class arraytest { public static void main(String args[]) { int[] a
Consider this code: class Jm44 { public static void main(String args[]){ int []arr =
Consider this code: class test { public static void main(String[] args) { test inst_test
Consider this code: class DatabaseCommands { @Privilege(ADMIN) public void delete(Something thingToDelete, User currentUser) {
Consider this code: public class ShortDivision { public static void main(String[] args) { short
Consider the code: class Work { public void DoStuff(string s) { Console.WriteLine(s); // ..
Consider the following code: class A { public: virtual void f() throw ( int
Consider the following C++ code: class A { public: virtual void f()=0; }; int
Consider this code snippet: class MyClass{ private List myList; //... public List getList(){ return

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.