Giter VIP home page Giter VIP logo

csharpnetworking's Introduction

C# Networking

This project is a basic implementation of these low level networking protocols

  • TCP (Server and Client)
  • WebSocket (Server and Client)
  • UDP (Server and Client)

Abstract Classes

Each protocol implements abstract class Server or Client which require that the protocol launches the following events when:

Server<T> Events

  • ServerOpened() When the server is started and listening for clients
  • ServerClosed() When the server stops listening for clients
  • ServerError(Exception exception) When there is an exception on the server
  • ClientConnected(T client) When a client connects to the server
  • ClientDisconnected(T client) When a client disconnects from the server
  • ClientReceived(T client, byte[] bytes) When the server receives a bytes from a client
  • ClientSent(T client, byte[] bytes) When the server sends a bytes to a client
  • ClientError(T client, Exception exception) When there is an exception with the client connection

SocketStream Class

The Servers in this project use SocketStream class as T to represent a client connection. It includes the client's Socket and NetworkStream.

Client Events

  • Opened() When the client connects to the server
  • Received(byte[] bytes) When the client receives a bytes from the server
  • Sent(byte[] bytes) When the client sends a bytes to the server
  • Closed() When the client disconnects from the server
  • Error(Exception exception) When there is an exception on the client

WebSocket Server Certificates

A wss:// connection requires a certificate. In order to generate a self-signed [localhost] certificate.pfx file, you can follow these steps on Windows:

  1. Install/Ensure you have OpenSSL installed. If you have chocolatey...
choco install openssl
  1. Generate private key:
openssl genrsa -out certificate.key 2048
  1. Generate certificate signing request:
openssl req -new -key certificate.key -out certificate.csr
  1. Generate self-signed certificate:
openssl x509 -req -days 365 -in certificate.csr -signkey certificate.key -out certificate.crt
  1. Generate PFX file:
openssl pkcs12 -export -out certificate.pfx -inkey certificate.key -in certificate.crt
  1. Place the certificate.pfx file in root of repository directory
  2. Additionally, you have to let Windows know this is a trusted certificate.
    1. Double click on the certificate.pfx file
    2. Store Location: Local Machine
    3. Place all certificates in the following store: Trusted Root Certification Authorities

Caveats

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.

csharpnetworking's People

Contributors

etdofresh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.