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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:29:22+00:00 2026-06-10T20:29:22+00:00

So with my software, I send a discover broadcast on the network and every

  • 0

So with my software, I send a discover broadcast on the network and every “client” that receives that broadcast will connect over TCP to me. With what I have, it seems to work “OK” but I feel like there has to be a better way. What I am seeing is that some TCP connections into my software are refused (I think) because I am currently working on accepting another socket. So with my current version, I can accept a socket around 80% of the time. Sometimes more, but usually around the 80% range. The rest are refused by my software and I dont know why. To me thats unacceptable but I am suck as to improve this number.

Here is a class I use to accept TCP clients and notify my other class about a new socket that has connected:

public class AsynchronousSocketListener
{
    // Thread signal.
    public ManualResetEvent allDone = new ManualResetEvent(false);
    public event EventHandler<ErtdRawDataArgs> ClientConnected;

    private string bindingIp;
    public string AddressBind 
    {
        get { return this.bindingIp; } 
        private set { this.bindingIp = value; } 
    }

    private int port;
    public int Port
    {
        get { return this.port; }
        private set { this.port = value; }
    }

    private Socket listener;

    public AsynchronousSocketListener(string bindingIp, int port) 
    {
        this.bindingIp = bindingIp;
        this.port = port;
    }

    protected void OnClientConnected(string data, IPEndPoint clientEP)
    {
        if (this.ClientConnected == null)
            return;

        Task.Factory.StartNew(() =>
        {
            //build args
            ErtdRawDataArgs args = new ErtdRawDataArgs(Encoding.Default.GetBytes(data));
            args.Source = string.Format("{0}:{1}", clientEP.Address.ToString(), clientEP.Port);
            this.ClientConnected(this, args);
        });
    }

    public void Close()
    {
        if (this.listener == null || !this.listener.Connected)
            return;

        this.listener.Shutdown(SocketShutdown.Both);
        this.listener.Close();
    }

    public void StartListening() 
    {
        Task.Factory.StartNew(() =>
        {
            // Data buffer for incoming data.
            byte[] bytes = new Byte[1024];

            // Establish the local endpoint for the socket.
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse(this.bindingIp), this.port);

            // Create a TCP/IP socket.
            listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // Bind the socket to the local endpoint and listen for incoming connections.
            try
            {
                listener.Bind(localEndPoint);
                int maxConnections = (int)SocketOptionName.MaxConnections;
                listener.Listen(maxConnections);

                while (true)
                {
                    // Set the event to nonsignaled state.
                    allDone.Reset();

                    // Start an asynchronous socket to listen for connections.
                    listener.BeginAccept(
                        new AsyncCallback(AcceptCallback),
                        listener);

                    // Wait until a connection is made before continuing.
                    allDone.WaitOne();
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        });
    }

    public void AcceptCallback(IAsyncResult ar) 
    {
        // Signal the main thread to continue.
        allDone.Set();

        // Get the socket that handles the client request.
        Socket listener = (Socket) ar.AsyncState;
        Socket handler = listener.EndAccept(ar);

        // Create the state object.
        StateObject state = new StateObject();
        state.workSocket = handler;
        handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,
            new AsyncCallback(ReadCallback), state);
    }

    public void ReadCallback(IAsyncResult ar) 
    {
        String content = String.Empty;

        // Retrieve the state object and the handler socket
        // from the asynchronous state object.
        StateObject state = (StateObject) ar.AsyncState;
        Socket handler = state.workSocket;

        // Read data from the client socket. 
        int bytesRead = handler.EndReceive(ar);

        if (bytesRead > 0) {
            // There  might be more data, so store the data received so far.
            state.sb.Append(Encoding.ASCII.GetString(
                state.buffer,0,bytesRead));

            // Check for end-of-file tag. If it is not there, read 
            // more data.
            content = state.sb.ToString();
            OnClientConnected(content, handler.RemoteEndPoint as IPEndPoint);

            //close socket
            handler.Shutdown(SocketShutdown.Both);
            handler.Close();
        }
    }
}

Is there any way to improve this code or is there something completely different all together to improve my results of accepting TCP connections at about the same time?

  • 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-10T20:29:24+00:00Added an answer on June 10, 2026 at 8:29 pm

    Well, you have a period of time between when you accept a connection and when the connection is “made” before you can accept another connection. Using things like wait handles is probably fairly slow.

    After re-reading your code, conceptually the point at which you can accept another connection is when you’ve called EndAccept. It appears that you’re setting the event before you call EndAccept which means BeginAccept can be called before the EndAccept and that another connection can be accepted before the previous had EndAccept called. I don’t know if that’s an issue–as far as I can tell, that’s legal. But, you can simplify your code to avoid the events and simply chain the next BeginAccept during the current accept and ensure EndAccept is called before the next BeginAccept

    What I do is chain the next accept from within the current accept. For example:

    listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    listener.Bind(new IPEndPoint(IPAddress.Any, 62000));
    listener.Listen(1000);
    listener.BeginAccept(OnAccept, listener);
    
    private void OnAccept(IAsyncResult ar)
    {
        Socket listener = (Socket)ar.AsyncState;
        Socket socket = listener.EndAccept(ar);
        listener.BeginAccept(OnAccept, listener);
        socket.BeginReceive(new byte[10], 0, 10, 0, (arReceive) => socket.EndReceive(arReceive), null);
    }
    

    of course, i’m using BeginAcceptSocket here; but the concept would be the same with BeginAccept…

    This frees you from having to start a new Task, frees from creating wait handles which are about 50-times slower that lock because they are cross-process–which frees you from having a “huge” pause between accepts.

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

Sidebar

Related Questions

We have some standalone devices that will send XML messages to arbitrary processing software
I have a client software (on Android) that listens to incoming messages. The messages
I am trying to design a software in c++ that will send request bytes
I have two threads that need to send TCP messages using the same already
I have two threads that need to send TCP messages using the same already
I'm looking for a mobile software that will send/receive text messages from mobile phone
I am using a computer with Processing software to send the Arduino (over USB
Can software be written (for some specific programming language, platform, etc.) that will inform
I am creating a grid application which requires me to send software packets that
I have Java webserver (no standard software ... self written) . Everything seems to

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.