ZiBaT  => Peter Levinsky  => Prog  => Note: Network programming
Network programming
Clients and Servers C#
 
Opdateret :  2016-09-06


Network Programming (Sockets) in C# :

When implementing clients and servers in C# you are using sockets.
A sockets is a programming abstraction to make it easy program and use network connections between two computers in C#. In C# these sockets can represent different approaches to different layers in the protocol stack. You can make network connections:

Most often used sockets – the transport layer sockets

Normally you are using the socket for set up a programming interface for the transport layer i.e. either TCP (the reliable connection) or UDP (the unreliable connection - it is actually a connectionless connection)

The most applications using TCP or UDP sockets are based on the client – server architecture. In these architectures the server is set up to be passive and receive one or more request and then sending response(s). The client on the other hand are the one who take the actions to start the communication. E.g. it is the browser the set up the TCP connection to send the web(Http)-request and wait for the server to respond.

TCP-sockets

Reference: TcpClient | TcpListener | NetworkStream | StreamReader | StreamWriter

In C# a simple TCP client looks like For console applications (not UWP):

-----------------------------------------------------------------------------------------------------

private const int SERVER_PORT = 7;
private const String SERVER_ADDRESS = "localhost";

try
{
  TcpClient client = new TcpClient(SERVER_ADDRESS, SERVER_PORT);

  using (NetworkStream ns = client.GetStream())
  {
     StreamReader reader = new StreamReader(ns);
     StreamWriter writer = new StreamWriter(ns);

     // todo code
  }
}
catch (Exception ex)
{
     // do error handling
}

----------------------------------------------------------------------------------------------------

 

 

A simple TCP server looks like

-----------------------------------------------------------------------------------------------------

private const int SERVER_PORT = 7;
private const int SERVER_IP = IPAddress.Any;

private bool stop = false;

TcpListener listener = new TcpListener(SERVER_IP, SERVER_PORT);
listener.Start();

while (!stop)
  {
     TcpClient client = listener.AcceptTcpClient();
     using (NetworkStream ns = client.GetStream())
     {
        StreamReader reader = new StreamReader(ns);
        StreamWriter writer = new StreamWriter(ns);

        // Todo handle the client
     }
}
listener.Stop();

----------------------------------------------------------------------------------------------------

UDP-sockets

Reference: UdpClient | IPEndPoint

In C# a simple UDP client looks like For console applications (not UWP):

-------------------------------------------------------------------------------------------------

private const int SERVER_PORT = 7;
private readonly IPAddress SERVER_IP = IPAddress.Any;

IPEndPoint serverEndPoint = new IPEndPoint(SERVER_IP, SERVER_PORT);
using (UdpClient client = new UdpClient())
{
// sending message
byte[] datagram = Encoding.ASCII.GetBytes("some text here" + "\r\n"); // any byte-stream
client.Send(datagram, datagram.Length, serverEndPoint);

// receiving reasponse
byte[] datagramReceived = client.Receive(ref serverEndPoint);
string response = Encoding.ASCII.GetString(datagramReceived, 0, datagramReceived.Length);
}

-------------------------------------------------------------------------------------------------

 

A simple UDP server looks like

-------------------------------------------------------------------------------------------------

private const int MY_SERVER_PORT = 7;
private readonly IPAddress MY_SERVER_IP = IPAddress.Any;

IPEndPoint serverEndPoint = new IPEndPoint(MY_SERVER_IP, MY_SERVER_PORT);
using (UdpClient listener = new UdpClient(serverEndPoint))
{

while(true) // run forever
{

   IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Loopback, 0);// the client IP-EndPoint

   // receiving response
   byte[] datagramReceived = listener.Receive(ref serverEndPoint);
       // Do something with byte-array

   // sending message
       // Make a byte array = 'datagram'
   client.Send(datagram, datagram.Length, remoteEndPoint);
}


}

-------------------------------------------------------------------------------------------------

 

In rarer cases you can use raw socket

With raw sockets you can communicate directly onto the network layer (IP), i.e. sending and receiving IP-packets on connection less unreliable communication.

Raw-sockets (IP)
In C# a simple IP client looks like:

Comming soon . . . . . . . .

A simple IP server looks like

Comming soon . . . . . . . .

In case of working with http

With http sockets, you can use an even higher abstraction than the TCP-sockets. You can work with HTTP-sockets that encapsulate the http format in some request and response object on a connection.

Http-sockets
In C# a simple HTTP client looks like:

Comming soon . . . . . . . .

A simple HTTP server looks like

Comming soon . . . . . . . .