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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:15:02+00:00 2026-06-03T03:15:02+00:00

I wrote a TCP server to use the BeginAccept/EndAccept pattern. With this, I coded

  • 0

I wrote a TCP server to use the BeginAccept/EndAccept pattern. With this, I coded up a simple UnitTest using a TcpClient, and measured each portion. All tests are localhost, so I am surprised to see that TCP connection is consistently taking 1 second. I have set the Socket.NoDelay = true although I believe this only affects Send/Receive. I am not receiving the first packet of data. Any help or ideas on speed this up are appreciated.

Note: I can not change the client side to keep the connection open, and I need to be able to handle a lot of requests per second if possible.

Server Side:

public void Start()
{
    System.Net.IPHostEntry localhost = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
    System.Net.IPEndPoint endpoint;
    int port = Properties.Settings.Default.ListenPort;

    //
    // Setup the connection to listen on the first IP, and specified port
    //
    endpoint = new IPEndPoint(IPAddress.Any, port);
    listenSocket = new Socket(endpoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
    listenSocket.Bind(endpoint);
    listenSocket.NoDelay = true; // do not wait 200 milliseconds for new data to be buffered on the connection
    listenSocket.Listen(int.MaxValue);
    Console.WriteLine("Listening on {0}:{1}", localhost.AddressList[0], port);

    //
    // Post the accept. The accept method will continuously post a new accept as soon as a connection is made
    //
    while (true)
    {
        accepted.Reset();
        Connection connection = connections.Pop();
        listenSocket.BeginAccept(AcceptCallback, connection);
        accepted.WaitOne();
    }
}

private static void AcceptCallback(IAsyncResult ar)
{
    accepted.Set();

    Connection connection = ar.AsyncState as Connection;
    Socket remoteSocket = null;

    try
    {
        remoteSocket = listenSocket.EndAccept(ar);
        remoteSocket.NoDelay = true;                
        connection.RemoteSocket = remoteSocket;

        //
        // Start the Receive cycle
        //
        Receive(connection);             
    }
    catch (SocketException)
    {
        Disconnect(connection);
    }           
}

Simple Test Client:

[TestMethod()]
public void ClientTest()
{
    TestContext.BeginTimer("Connection");
    TcpClient client = new TcpClient("localhost", 10300);
    NetworkStream stream = client.GetStream();
    TestContext.EndTimer("Connection");
    ...

Using a LoadTest I loaded 25 users, and the Transaction “Connection” always takes above 1 second.

  • 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-03T03:15:03+00:00Added an answer on June 3, 2026 at 3:15 am

    Not sure why, but simply changing this:

    TestContext.BeginTimer("Connection");          
    TcpClient client = new TcpClient("localhost", 10300);            
    TestContext.EndTimer("Connection");
    

    To this:

    TestContext.BeginTimer("Connection");
    TcpClient client = new TcpClient();
    client.Connect("localhost", 10300);       
    TestContext.EndTimer("Connection");
    

    Drops the time from 1 second to .13 seconds. Will have to investigate as to why, but hopefully this will help somebody out in the future.

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

Sidebar

Related Questions

I am attempting to write a simple TCP server using Aleph. Everything works fine,
I wrote a simple server and client apps, where I can switch between TCP,
I wrote two simple programs server and a client using sockets in C++ (Linux).
I've wrote a simple client and a server. They both can configured to use
I'm working on a Ruby TCP client/server app using GServer and TCPSocket. I've run
All, Working on a custom TCP/IP server for a text-based multiplayer game, using Ruby.
im making file transfer (Server-Client) TCP i've already looked for same questions like this
I have a server in linux using the Berkeley_sockets and I create a TCP
I would like to use libev for a streaming server I am writing. This
When someone connects to my TCP Server, I log the time they connected using

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.