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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:49:47+00:00 2026-05-24T17:49:47+00:00

I am doing some basic Socket messaging. I have a routine that works well

  • 0

I am doing some basic Socket messaging. I have a routine that works well but there is a problem under load.

I’m using UDP to do a connectionless SendTo to basically do a ping-like operation to see if any of my listeners are out there on the LAN. Ideally I would just use the broadcast address, but Wireless routers don’t seem to relay my broadcast. My work around is to iterate through all IPs on the Subnet and send my data gram to each IP. The other PCs are listening and if they get the message they will reply and that is how I get Peers to find each other. Here is the code that is in the loop which sends the data gram to each IP in the subnet.

            string msgStr = "some message here...";
            byte[] sendbuf = Encoding.ASCII.GetBytes(msgStr);

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socket.Blocking = true;
            socket.SendTo(sendbuf, remoteEndPt);
            //socket.Close();

This works, but when the Subnet range is large, say 255.255.0.0 (meaning ~60,000 IPs to iterate through) I will eventually get a SocketException with error code “10022”, meaning “Invalid Argument”. This tends to happen after ~10,000 or so successful sends then I start to see this error. Also, the router I use at work handles it and is presumably a high powered router, but the cheap-o one in my lab is the one that produces the error.

If I put in a wait time after catching the SocketException and before resuming the loop it will typically recover but eventually I’ll get the error again.

I think what is happening is that the buffer on the router gets full and I cannot send anymore data. The higher quality one at work can handle it but the cheap-o one gets bogged down. Does that sound plausible?

A couple questions:

1) When using SendTo in a connectionless manner, do I need to call Close() on my Socket?

I’ve haven’t seen any benefit in calling Close(), but when I do call Close() it severely slows down my iteration (I have it commented out above because it does slow things down a lot). Does this make sense?

2) Is there a way for me to tell I should wait before trying to send more data? It doesn’t seem right to just catch the Exception which I still don’t know what the cause of it is.

Thanks, J.

  • 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-24T17:49:49+00:00Added an answer on May 24, 2026 at 5:49 pm

    I am not sure that is the router only but I suspect that you are also running into some limit in the OS…

    Any reason you are creating the Socket every time you send ?
    Just reuse it…

    Anyways according to http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx it is a good idea to call Shutdown() and then Close() on the Socket… perhaps not with every send but every 255 IPs or so…

    Checkout UdpClient – that could make implementation easier / more robust

    EDIT – as per comment:

    IF you want a Socket reuse “cache”… this for example would make sure that a specific Socket is only used every 256 checks…

    // build/fill your Socket-Queue for example in the con
    class SocketExample
    {
        Queue<Socket> a = new Queue<Socket>();
        SocketExample ()
        {
            int ii = 0, C = 256;
            for (ii = 0; ii < C; C++)
            {
                a.Enqueue (new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp));
            }
        }
    
        // in your function you just dequeue a Socket and use it, 
        // after you are finished you enqueue it
        void CheckNetIP (some parameters...)
        {
            Socket S = a.Dequeue();
            // do whatever you want to do...
            // IF there is no exception
            a.Enqueue(S); 
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have my iDevices doing some basic messaging via Bonjour and I am ready
I'm now doing some work with decoding MP3 files, but just have some basic
While doing some small regex task I came upon this problem. I have a
I am doing some basic form validation. I have the following JavaScript function: function
So i have this very basic ipad view controller and i was doing some
I'm doing some basic sql on a few tables I have, using a union(rightly
I have setup some basic code that triggers on focus of various input fields:
I'm doing some work for a department that has a pretty basic table structure
Doing some jquery animation. I have certain divs set up with an attribute of
While doing some refactoring I've found that I'm quite often using a pair or

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.