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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:24:55+00:00 2026-05-15T18:24:55+00:00

I have a class that uses ‘System.Net.Sockets.Socket’ directly for network comunication, and currently I

  • 0

I have a class that uses ‘System.Net.Sockets.Socket’ directly for network comunication, and currently I use the following two interfaces to break dependency on the Socket class :

public interface ISocketListener
    {
        void Listen(int backlog);

        ISocket Accept();

        void Bind(EndPoint endpoint);               
    }

public interface ISocket
    {
        void Connect (EndPoint ep);

        Stream CommunicationStream { get; }

        void Close();

        void Close(int timeout);

        void Shutdown();
    }

In the production implementation I just redirect all the method calls to the private Socket object. In testing environment I use a MemoryStream as the comunication ‘pipe’ between the sockets.
Since I have little experience in the subject, some questions appeared in my head while writing tests : Are there any ‘formal’ good practices in testing this kind of software? When doing integration testing, how do I test the performance of this server in multiple connections/high latency situations (more specifically, how to simulate these situations)? Why there are asynchronous versions of Socket.Accept/Socket.Receive? Can I replace the asynchronous methods for handling their synchronous versions in separate threads?

  • 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-15T18:24:56+00:00Added an answer on May 15, 2026 at 6:24 pm

    Are there any ‘formal’ good practices
    in testing this kind of software?

    I can speak to this but I’m sure there is some good information here on stack or out in the wild.

    When doing integration testing, how
    do I test the performance of this
    server in multiple connections/high
    latency situations (more specifically,
    how to simulate these situations)?

    For a simple start write some test and logging routines, running them on a separate box, that hammered the server with a verity of requests…including nonsensical ones. Running log4net on both would also be helpful. Introducing some lag could be done, as already mentioned, via a sort of proxy…a box that sits between the client and server. The client points to the proxy and the proxy modifies the data…delay, change packet order, etc….sends to to the server…reverse and repeat. As a slight aside…I would avoid Thread.Sleep(…). Better to use something like this :

    lock (timelock) { Monitor.Wait(timelock, TimeoutInMilliseconds); }
    

    …asynchronous versions of
    Socket.Accept/Socket.Receive? Can I
    replace the asynchronous methods for
    handling their synchronous versions in
    separate threads?

    You have many options, asynchronous methods, plain old threading, the threadpool, the TPL, etc. The best choice is application specific.

    Here is an Asynchronous Server Socket Example from the MSDN entry on sockets.

    As luck would have it….the O’Reilly book: C# 4.0 in a Nutshell has an excellent set of TCP Server examples that cover both asynchronous/threaded methods in blocking/non-blocking manifestations. Here is one of the examples….

      public class Server
      {
        public void Serve(IPAddress address, int port)
        {
    
          TcpListener listener = new TcpListener(address, port);
          listener.Start();
          while (true)
          {
            TcpClient c = listener.AcceptTcpClient();
            Task.Factory.StartNew(Accept, c);
          }
        }
    
        void Accept(object clientObject)
        {
          using (TcpClient client = (TcpClient)clientObject)
          {
            using (NetworkStream n = client.GetStream())
            {
              byte[] data = new byte[5000];
    
              int bytesRead = 0; int chunkSize = 1;
    
              while (bytesRead < data.Length && chunkSize > 0)
              {
                bytesRead +=
                  chunkSize = n.Read /// Read is blocking
                    (data, bytesRead, data.Length - bytesRead);
              }
    
              Array.Reverse(data);
              n.Write                /// Write is blocking
                (data, 0, data.Length);
            }
          }
        }
      }
    

    Hope this is helpful.

    P.S. Get a copy of C# 4.0 in a Nutshell…tis good.

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

Sidebar

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.