Network Types

Introduction to Network Types

The MS networking framework supports several fundamental network types, each designed for specific communication patterns and performance characteristics. Understanding these types is crucial for building efficient and scalable network applications.

This document outlines the primary network types available, their use cases, and key considerations for implementation.

1. TCP Connection

The Transmission Control Protocol (TCP) provides a reliable, ordered, and error-checked stream of bytes between two endpoints. It is connection-oriented, meaning a connection must be established before data can be sent.

Key Characteristics:

Use Cases:

Example Usage:


using MS.Net.Sockets;

// Server side
var listener = new TcpListener(IPAddress.Any, 12345);
listener.Start();
Console.WriteLine("Server started. Listening on port 12345...");

TcpClient client = await listener.AcceptTcpClientAsync();
Console.WriteLine("Client connected!");
NetworkStream stream = client.GetStream();
// ... send/receive data via stream ...
client.Close();
listener.Stop();

// Client side
var client = new TcpClient();
await client.ConnectAsync("server.example.com", 12345);
Console.WriteLine("Connected to server!");
NetworkStream stream = client.GetStream();
// ... send/receive data via stream ...
client.Close();
            

2. UDP Datagram

The User Datagram Protocol (UDP) provides a simpler, connectionless datagram service. It is faster than TCP but does not guarantee delivery, order, or error checking.

Key Characteristics:

Use Cases:

Example Usage:


using MS.Net.Sockets;
using System.Text;

// Server side
var udpClient = new UdpClient(12345);
Console.WriteLine("UDP Server started. Listening on port 12345...");

while (true)
{
    IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
    byte[] receivedBytes = udpClient.Receive(ref remoteEndPoint);
    string message = Encoding.UTF8.GetString(receivedBytes);
    Console.WriteLine($"Received from {remoteEndPoint}: {message}");

    // Optionally send a response
    byte[] responseBytes = Encoding.UTF8.GetBytes("ACK: " + message);
    udpClient.Send(responseBytes, responseBytes.Length, remoteEndPoint);
}
// Note: UDP server typically runs indefinitely or until explicitly stopped.

// Client side
var udpClient = new UdpClient();
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.100"), 12345);
string messageToSend = "Hello UDP!";
byte[] sendBytes = Encoding.UTF8.GetBytes(messageToSend);

udpClient.Send(sendBytes, sendBytes.Length, serverEndPoint);
Console.WriteLine($"Sent: {messageToSend}");

// Optionally receive a response
byte[] receivedBytes = udpClient.Receive(ref serverEndPoint);
string response = Encoding.UTF8.GetString(receivedBytes);
Console.WriteLine($"Received response: {response}");
udpClient.Close();
            

3. WebSocket

WebSockets provide a full-duplex communication channel over a single TCP connection. They are ideal for real-time, bi-directional communication between a client (typically a web browser) and a server.

Key Characteristics:

Use Cases:

Note: WebSocket support is often managed through specific libraries or frameworks built on top of the core networking components.

Choosing the Right Network Type

The selection of the appropriate network type depends heavily on the application's requirements:

Important: Always consider the trade-offs between reliability, speed, and complexity when choosing a network type.