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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:34:36+00:00 2026-06-01T07:34:36+00:00

Part of my n00b server: TcpClient client = tcpListener.AcceptTcpClient(); Thread clientThread = new Thread(new

  • 0

Part of my n00b server:

TcpClient client = tcpListener.AcceptTcpClient();
Thread clientThread = new Thread(new ParameterizedThreadStart(handleClientRegistration));
clientThread.Start(client);
...
//and in the clientThread we wait for data
bytesRead = clientStream.Read(message, 0, 4096);

Now I’d like to write unit tests for this snippet. I want to fake a client connection, pass arbitrary data and check how the server handles it.

How should I do it in C#?

edit:
What I’d like to mock is the connection itself – I want to avoid network connections.

  • 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-01T07:34:38+00:00Added an answer on June 1, 2026 at 7:34 am

    It is not a unit test if it is listening to a socket.
    Wrap the client and with a thin layer and work with a mock instead. This way you can fake all the received data you want.

    E.g. Mocking the socket (simplified example):

    Create an interface ISocket with all the methods you need:

    public interface ISocket
    {
        ISocket Accept( int port );
        byte[] Receive( int numberOfBytes );
        bool Send( byte[] data );
        void Disconnect();
        bool Connect( string address, int port );
    }
    

    Now create a concrete class TcpSocket implementing ISocket:

    public class TcpSocket : ISocket
    {
        private Socket socket;
        public TcpSocket()
        {
            socket = new Socket( AddressFamily.InterNetwork,
                                 SocketType.Stream, ProtocolType.Tcp );
        }
    
        // implement all methods of ISocket by delegating to the internal socket
    }
    

    Wherever you would normally use a System.Net.Sockets.Socket, pass a ISocket instead. When you need to new up a Socket use TcpSocket. If you create sockets deep down in the guts of your system you might want to create a factory instead of newing up a TcpSocket directly. Under test you can then pass a different implementation of ISocket (the Mock) (possibly created through the factory). You could implement your own mock by creating a second implementation of ISocket called MockSocket which returns test data in Receive or you could use on of the countless mock frameworks to do that for you.

    public class MockSocket : ISocket
    {
         private byte[] testData;
         public void SetTestData(byte[] data)
         {
             testData = data;
         }
    
         public byte[] Receive(int numberOfBytes)
         {
             return testData;
         }
    
         // you need to implement all members of ISocket ofcourse...
    }
    

    This might seen like a lot of effort, but in all but toy systems the boundary api should be hidden behind a thin layer not only for testing but also to stay flexible. If, for instance, you want to use a more powerful socket library instead of System.Net.Socket you can make TcpSocket use that without the need to change every use of sockets in your own code. This is also the reason why programming to an interface instead of programming to a concrete implementation is usually a good idea: you can easily switch implementations (but that doesn’t mean you should create interfaces to ALL your classes). If this all confuses you read more about mocking (like here: http://en.wikipedia.org/wiki/Mock_object)

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

Sidebar

Related Questions

Part of a new product I have been assigned to work on involves server-side
Part of the application I'm working on for my client involves sending emails for
Part of a C# application I'm writing requires collecting data from a service provider's
Part of what's so powerful about Clojure is that all the core data-types implement
Part of my code: byte[] bytes = new byte[8000]; final DataInputStream in = new
Part of my spreadsheet's script runs the following piece of code: temp.getRange(1, 1).setValue(=QUERY(data!A1:H125, \Select
Part of my everyday work is maintaining and extending legacy VB6 applications. A common
Part of the install for an app I am responsible for, compiles some C
Part of our java application needs to run javascript that is written by non-developers.
Part of the series of controls I am working on obviously involves me lumping

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.