Server/Client TCP/IP Sample

Noah Aas 1,110 Reputation points
2025-10-23T17:10:14.88+00:00

The goal:
I create two C# desktop WPF applications. Two applications each.

  • One server
  • One client

The exchange should take place via TCP/IP. Messages must be transmitted in both directions. Question Answer. When an application is closed and reopened, a connection should follow automatically.
Do you have any good ideas or examples with TcpClient and TcpListener library?
I see the following challenge.
The user interface and the automatic connection. Ideally, I should be able to see in the user interface what has been sent, what has been received, and whether the connection is active.

enter image description here

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

2 answers

Sort by: Most helpful
  1. Starry Night 600 Reputation points
    2025-10-24T03:17:59.5933333+00:00

    TcpClient and TcpListener are built on top of the System.Net.Sockets.Socket class and take care of the details of transferring data for ease of use.

    1.Create a TcpClient

    The TcpClient class provides TCP services at a higher level of abstraction than the Socket class. TcpClient is used to create a client connection to a remote host. Knowing how to get an IPEndPoint, let's assume you have an IPAddress to pair with your desired port number. The following example demonstrates setting up a TcpClient to connect to a time server on TCP port 13:

    var ipEndPoint = new IPEndPoint(ipAddress, 13);
    using TcpClient client = new();
    await client.ConnectAsync(ipEndPoint);
    await using NetworkStream stream = client.GetStream();
    var buffer = new byte[1_024];
    int received = await stream.ReadAsync(buffer);
    var message = Encoding.UTF8.GetString(buffer, 0, received);
    Console.WriteLine($"Message received: \"{message}\"");
    // Sample output:
    //     Message received: "πŸ“… 8/22/2022 9:07:17 AM πŸ•›"
    

    2.Create a TcpListener

    The TcpListener type is used to monitor a TCP port for incoming requests and then create either a Socket or a TcpClient that manages the connection to the client. The Start method enables listening, and the Stop method disables listening on the port. The AcceptTcpClientAsync method accepts incoming connection requests and creates a TcpClient to handle the request, and the AcceptSocketAsync method accepts incoming connection requests and creates a Socket to handle the request.

    The following example demonstrates creating a network time server using a TcpListener to monitor TCP port 13. When an incoming connection request is accepted, the time server responds with the current date and time from the host server.

    var ipEndPoint = new IPEndPoint(IPAddress.Any, 13);
    TcpListener listener = new(ipEndPoint);
    try
    {    
        listener.Start();
        using TcpClient handler = await listener.AcceptTcpClientAsync();
        await using NetworkStream stream = handler.GetStream();
        var message = $"πŸ“… {DateTime.Now} πŸ•›";
        var dateTimeBytes = Encoding.UTF8.GetBytes(message);
        await stream.WriteAsync(dateTimeBytes);
        Console.WriteLine($"Sent message: \"{message}\"");
        // Sample output:
        //     Sent message: "πŸ“… 8/22/2022 9:07:17 AM πŸ•›"
    }
    finally
    {
        listener.Stop();
    }
    

    For more information, you can check document:

    1. TCP overview
    2. Simple TCP Server and Client with TcpListener and TcpClient .
    3. Developing a Simple Chat Tool using C# TcpClient and TcpListener

  2. Bruce (SqlWork.com) 81,191 Reputation points Volunteer Moderator
    2025-10-24T16:25:16.9766667+00:00

    Because it client / server, the client always connects to the server.

    the client needs a loop. When it tries to connect it may fail. The client should delay and try again. When connected, it may lose the connection, so it should go back to the connect loop.

    the server just waits for a connection. If the connection is lost, it goes back to waiting.

    both the client and need to keep track of the connection status. If live they can send a message, if not then wait until the connection is restored. Detecting lost connection will typically happen on a message send. App often implements a ping/keep alive message they send to more quickly detect a lost connection .


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.