Building a Basic Synchronous TCP Socket Server and Client in C#

Socket communication relies on a well-defined handshake process for establishing and terminating connections.

Connection Handshake (Three-Way Handshake)

  1. Client sends a connection request.
  2. Server acknowledges the request.
  3. Client confirms the connection.

After these steps, data can be exchanged using Send and Receive.

Disconnection Handshake (Four-Way Handshake)

  1. Client sends a request to terminate the TCP connection.
  2. Server closes its side of the socket upon receiving the request.
  3. Server sends a reverse request asking the client to close its socket.
  4. Client acknowledges and closes its socket.

The example below demonstrates a synchronous socket application. The client sends a single message to the server, and the server replies with one message. This illustrates the fundamental workflow of socket programming.

Server Implementation

  1. Create an IPEndPoint object using the server's IP address and a chosen port.
  2. Create a Socket object.
  3. Bind the socket to the endpoint using Bind().
  4. Start listening for incoming connections with Listen().
  5. Accept a client connection using Accept(), which returns a new socket for communication.
  6. Close the socket after communicatoin.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            int port = 2000;
            string host = "127.0.0.1";

            // Create IP endpoint
            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint endpoint = new IPEndPoint(ip, port);

            // Create a TCP socket
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // Bind and listen
            listener.Bind(endpoint);
            listener.Listen(0);
            Console.WriteLine("Server is listening for connections...");

            // Accept a client
            Socket handler = listener.Accept();
            Console.WriteLine("Client connected.");

            Console.WriteLine("Press ENTER to receive message from client.");
            Console.ReadLine();

            // Receive data
            byte[] buffer = new byte[1024];
            int received = handler.Receive(buffer, buffer.Length, 0);
            string message = Encoding.ASCII.GetString(buffer, 0, received);
            Console.WriteLine("Received from client: {0}", message);

            Console.WriteLine("Press ENTER to send acknowledgment.");
            Console.ReadLine();

            // Send response
            string response = "OK! Message received successfully.";
            byte[] responseBytes = Encoding.ASCII.GetBytes(response);
            handler.Send(responseBytes, responseBytes.Length, 0);

            Console.WriteLine("Response sent. Press ENTER to close.");
            Console.ReadLine();

            // Clean up
            handler.Close();
            listener.Close();
        }
    }
}

Client Implementation

  1. Create an IPEndPoint object using the server's IP address and port.
  2. Create a Socket object.
  3. Connect to the server using Connect() with the endpoint.
  4. Send data using Send().
  5. Receive the server's response using Receive().
  6. Cloce the socket.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            int port = 2000;
            string host = "127.0.0.1";

            // Create IP endpoint
            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint endpoint = new IPEndPoint(ip, port);

            // Create a TCP socket
            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            Console.WriteLine("Client connecting...");
            client.Connect(endpoint);
            Console.WriteLine("Connected to server.");

            Console.WriteLine("Press ENTER to send message.");
            Console.ReadLine();

            // Send message
            string message = "Hello! This is the client.";
            byte[] messageBytes = Encoding.ASCII.GetBytes(message);
            client.Send(messageBytes, messageBytes.Length, 0);

            // Receive response
            byte[] buffer = new byte[1024];
            int received = client.Receive(buffer, buffer.Length, 0);
            string response = Encoding.ASCII.GetString(buffer, 0, received);
            Console.WriteLine("Server response: {0}", response);

            Console.WriteLine("Press ENTER to close.");
            Console.ReadLine();

            // Clean up
            client.Close();
        }
    }
}

Tags: C# Socket Programming tcp networking synchronous

Posted on Fri, 06 Nov 2026 16:44:55 +0000 by psyion