Network programming

using sockets


Imagine playing a game with your friend who lives in a different house. You can't just talk to each other, but you still want to exchange messages. This is where socket programming comes into play! Socket programming is a concept that enables communication between computers or processes over a network. Sockets are used as endpoints for communication, acting as interfaces that allow programs to send and receive data over a network. It facilitates data exchange between a server and multiple clients.

Basic Principles

Server-Client Model

Socket programming often relies on the server-client model. The server waits for incoming connections and provides services or resources. The client requests services or resources from the server.

IP Address and Port

Every computer in the network has a unique IP address. A port is a numerical identifier assigned to a specific process on a computer. Together, the IP address and port form a unique address for communication.

Creating sockets

Both the server and the client create sockets to establish a communication connection. The server creates a socket and listens on a specific port. The client also creates a socket and attempts to connect to the server.

Establishing Connection

The server waits for incoming connections (listen/accept), and the client tries to establish a connection with a server (connect) by specifying the server's IP address and port.

Data Transmission

Once the connection is established, data can be exchanged between the server and client. Data is transmitted as bytes, with methods for reading and writing data over the sockets.

Socket classes in {Coder for C#}

Server class

Socket(): Creates a socket.
Bind(string addr, int port): Creates a listener and binds a specific IP address and port to it.
Listen(): Starts the listener to listen for incoming connections.
Accept(): Accepts an incoming connection and returns an object for exchanging data with the client through read/write operations.

Client class

Socket(): Creates a socket.
Connect(string host, int port): Establishes a connection to the server via its "direct dial number" (port).

How to program a simple Web browser


using static CoderLearn.Network.Client;

Console.WriteLine("-----CLIENT (Browser)-----");

var server = "127.0.0.1"; //"www.uni-hamburg.de";

Socket();
var client = Connect(server, 80);

client.Write($"GET / HTTP/1.1\r\nHost: {server}\r\n");

string line;
while((line = client.Read()) != null) 
{
    Console.WriteLine(line);
}

How to program a simple Web server (http)


using static CoderLearn.Network.Server;

Console.WriteLine("-----SERVER-----");

Socket();
Bind("127.0.0.1", 80);
Listen();

while (true)
{
    using var client = await Accept();

    string line;
    while ((line = client.Read()) != null)
    {
        Console.WriteLine(line);
	}
    client.Write("HTTP/1.1 200 ok\r\n\r\nHello client\r\n");
}

 This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License