Socket communication relies on a well-defined handshake process for establishing and terminating connections.
Connection Handshake (Three-Way Handshake)
- Client sends a connection request.
- Server acknowledges the request.
- Client confirms the connection.
After these steps, data can be exchanged using Send and Receive.
Disconnection Handshake (Four-Way Handshake)
- Client sends a request to terminate the TCP connection.
- Server closes its side of the socket upon receiving the request.
- Server sends a reverse request asking the client to close its socket.
- 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
- Create an
IPEndPointobject using the server's IP address and a chosen port. - Create a
Socketobject. - Bind the socket to the endpoint using
Bind(). - Start listening for incoming connections with
Listen(). - Accept a client connection using
Accept(), which returns a new socket for communication. - 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
- Create an
IPEndPointobject using the server's IP address and port. - Create a
Socketobject. - Connect to the server using
Connect()with the endpoint. - Send data using
Send(). - Receive the server's response using
Receive(). - 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();
}
}
}