This project is a basic implementation of these low level networking protocols
- TCP (Server and Client)
- WebSocket (Server and Client)
- UDP (Server and Client)
Each protocol implements abstract class Server or Client which require that the protocol launches the following events when:
ServerOpened()
When the server is started and listening for clientsServerClosed()
When the server stops listening for clientsServerError(Exception exception)
When there is an exception on the serverClientConnected(T client)
When a client connects to the serverClientDisconnected(T client)
When a client disconnects from the serverClientReceived(T client, byte[] bytes)
When the server receives a bytes from a clientClientSent(T client, byte[] bytes)
When the server sends a bytes to a clientClientError(T client, Exception exception)
When there is an exception with the client connection
The Servers in this project use SocketStream
class as T to represent a client connection. It includes the client's Socket and NetworkStream.
Opened()
When the client connects to the serverReceived(byte[] bytes)
When the client receives a bytes from the serverSent(byte[] bytes)
When the client sends a bytes to the serverClosed()
When the client disconnects from the serverError(Exception exception)
When there is an exception on the client
A wss://
connection requires a certificate. In order to generate a self-signed [localhost] certificate.pfx
file, you can follow these steps on Windows:
- Install/Ensure you have
OpenSSL
installed. If you have chocolatey...
choco install openssl
- Generate private key:
openssl genrsa -out certificate.key 2048
- Generate certificate signing request:
openssl req -new -key certificate.key -out certificate.csr
- Generate self-signed certificate:
openssl x509 -req -days 365 -in certificate.csr -signkey certificate.key -out certificate.crt
- Generate PFX file:
openssl pkcs12 -export -out certificate.pfx -inkey certificate.key -in certificate.crt
- Place the
certificate.pfx
file in root of repository directory - Additionally, you have to let Windows know this is a trusted certificate.
- Double click on the
certificate.pfx
file - Store Location:
Local Machine
Place all certificates in the following store
:Trusted Root Certification Authorities
- Double click on the
As shown in the examples, this implementation uses buffers and streams. If the data is larger than the buffer, it will be split into multiple packets. This is not handled in the example, but it is something to be aware of when implementing this in your production environment.
Udp throws an exception when trying to send packets bigger than the buffer size or when sending to a non-existent endpoint. This is not handled in the example, but it is something to be aware of when implementing this in your production environment.