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

  • Home
  • SEARCH
  • 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 6673697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:39:29+00:00 2026-05-26T03:39:29+00:00

I want to write a simple socket server, however I’d like it to be

  • 0

I want to write a simple socket server, however I’d like it to be vertically scalable, for example, not creating a thread per connection or very long running tasks, which may consume all threads.

The server receives a request containing a query and streams an arbitrarily large result.

I would like the idiomatic way to do this using techniques and libraries that are available in C# 4, with an emphasis on simple code, rather than raw performance.

Reopening
A socket server is a useful part of a scalable system. If you want to scale horizontally, there are different techniques. You should probably won’t be able to answer this question if you have never created a socket server.

  • 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-26T03:39:29+00:00Added an answer on May 26, 2026 at 3:39 am

    I’ve been working on something similar for a week or two now so hopefully I’ll be able to help you out a bit.

    If your focus is on simple code, I’d recommend using the TcpClient and TcpListener classes. They both make sockets much easier to work with. While they have existed since .NET Framework 1.1 they have been updated and are still your best bet.

    In terms of how to utilize the .NET Framework 4.0 in writing simplistic code, Tasks are the first thing that come to mind. They make writing asynchronous code much less painful and it will become much easier to migrate your code once C# 5 comes out (new async and await keywords). Here is an example of how Tasks can simplify your code:

    Instead of using tcpListener.BeginAcceptTcpClient(AsyncCallback callback, object state); and providing a callback method which would call EndAcceptTcpClient(); and optionally cast your state object, C# 4 allows you to utilize closures, lambdas, and Tasks to make this process much more readable and scalable. Here is an example:

    private void AcceptClient(TcpListener tcpListener)
    {
        Task<TcpClient> acceptTcpClientTask = Task.Factory.FromAsync<TcpClient>(tcpListener.BeginAcceptTcpClient, tcpListener.EndAcceptTcpClient, tcpListener);
    
        // This allows us to accept another connection without a loop.
        // Because we are within the ThreadPool, this does not cause a stack overflow.
        acceptTcpClientTask.ContinueWith(task => { OnAcceptConnection(task.Result); AcceptClient(tcpListener); }, TaskContinuationOptions.OnlyOnRanToCompletion);
    }
    
    private void OnAcceptConnection(TcpClient tcpClient)
    {
        string authority = tcpClient.Client.RemoteEndPoint.ToString(); // Format is: IP:PORT
    
        // Start a new Task to handle client-server communication
    }
    

    FromAsync is very useful as Microsoft has provided many overloads that can simplify common asynchronous operations. Here’s another example:

    private void Read(State state)
    {
        // The int return value is the amount of bytes read accessible through the Task's Result property.
        Task<int> readTask = Task<int>.Factory.FromAsync(state.NetworkStream.BeginRead, state.NetworkStream.EndRead, state.Data, state.BytesRead, state.Data.Length - state.BytesRead, state, TaskCreationOptions.AttachedToParent);
    
        readTask.ContinueWith(ReadPacket, TaskContinuationOptions.OnlyOnRanToCompletion);
        readTask.ContinueWith(ReadPacketError, TaskContinuationOptions.OnlyOnFaulted);
    }
    

    State is just a user-defined class that usually just contains the TcpClient instance, the data (byte array), and perhaps the bytes read as well.

    As you can see, ContinueWith can be used to replace a lot of cumbersome try-catches that until now were a necessary evil.

    At the beginning of your post you mentioned not wanting to create a thread per connection or create very long running tasks and I thought I would address that at this point. Personally, I don’t see the problem with creating a thread for each connection.

    What you must be careful with, however, is using Tasks (an abstraction over the ThreadPool) for long-running operations. The ThreadPool is useful because the overhead of creating a new Thread is not negligible and for short tasks such as reading or writing data and handling a client connection, Tasks are preferred.

    You must remember that the ThreadPool is a shared resource with a specialized function (avoiding the overhead of spending more time creating a thread than actually using it). Because it is shared, if you used a thread, another resource cannot and this can quickly lead to thread-pool starvation and deadlock scenarioes.

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

Sidebar

Related Questions

I want to write a simple server socket in Ruby, which, when a client
I have a simple programs for socket client and server its not working over
I am new to raw socket. I want write a server and a client
I am trying to write a simple IO::Socket connection in perl. However, I am
I want to realize a simple client-server connection using Nodejs. But I've encountered with
i'm trying to write simple C openssl client and server. Here is client's code:
I want to write a simple chat-client in Ruby for the terminal. The Problem
I want to write a simple Outlook 2007 AddIn that allows me to manually
I want to write a server that can accept multiple clients in python (twisted).
I have two simple programmes, server and client, both at localhost. What I want

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.