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.
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);
}
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