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:
- Reliability: Guarantees delivery of data in the correct order.
- Flow Control: Prevents a fast sender from overwhelming a slow receiver.
- Congestion Control: Manages network traffic to avoid overloading the network.
- Connection-Oriented: Requires a handshake to establish a connection before data transfer.
Use Cases:
- Web browsing (HTTP/HTTPS)
- Email (SMTP, POP3, IMAP)
- File transfer (FTP)
- Remote login (SSH)
- Any application requiring guaranteed delivery and order.
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:
- Connectionless: No connection setup required.
- Unreliable: Datagrams may be lost, duplicated, or arrive out of order.
- Fast: Lower overhead compared to TCP.
- Best-Effort: No retransmissions or guaranteed delivery.
Use Cases:
- Online gaming
- Video and audio streaming
- DNS (Domain Name System)
- VoIP (Voice over IP)
- Applications where occasional data loss is acceptable for lower latency.
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:
- Full-Duplex: Both client and server can send messages independently at any time.
- Persistent Connection: Establishes a long-lived connection.
- Low Latency: More efficient for frequent, small messages than polling.
- Protocol: Runs over HTTP for the initial handshake, then upgrades to the WebSocket protocol.
Use Cases:
- Real-time chat applications
- Live score updates
- Collaborative editing
- Push notifications
Choosing the Right Network Type
The selection of the appropriate network type depends heavily on the application's requirements:
- For reliable, ordered data transfer: Use TCP.
- For speed and low overhead, where data loss is tolerable: Use UDP.
- For real-time, bi-directional communication, especially with web clients: Use WebSockets.