Skip to content

A very simple tcp socket library that handles message framing and has optional dos protection.

Notifications You must be signed in to change notification settings

lanicon/ProtocolSocket

 
 

Repository files navigation

ProtocolSocket

A very simple tcp networking library that handles message framing and has optional dos protection.

Simple example on how to use:

            //Declare socket options to use for both server and client
            //The server will use these options for the connected clients by default
            ProtocolSocketOptions socketOptions = new ProtocolSocketOptions();
            //Set dos protection to look if theres more than 10 packets every 100 milliseconds
            DOSProtection dosProtection = new DOSProtection(10,100);
            SetDOSProtection(dosProtection);
            
            //Declare what endpoint to listen on
            EndPoint listenEndPoint = new IPAddress(IPAddress.Any, 9090);
            
            //Declare maximum size of connection queue (Max allowed connection to wait to get connected)
            int maxConnectQueue = 10;
            
            //Declare server options
            ProtocolServerOptions serverOptions = new ProtocolServerOptions(socketOptions, 
            listenEndPoint, maxConnectQueue);
            
            //Declare server socket
            ProtocolServer server = new ProtocolServer(serverOptions);

            //if dos was detected fire this event
            server.DOSDetected += (s) => {
                Console.Title = "A dos was detected " + s.RemoteEndPoint + " warning.";
                //Could call s.Close();
            };

            //S = sender, e = exception (can be null), connected = if client is connected
            server.ClientStateChanged += (s, e, connected) => {
                Console.WriteLine("Client " + s.RemoteEndPoint + " changed state connected? " + 
                connected + " message " + e?.Message);
            };
            
            server.ConnectionError(s, e) => {
                 Console.WriteLine("Client " + s.RemoteEndPoint + " disconnected");
            };
            
            server.ConnectionEstablished += (sender) => {
                Console.WriteLine("Client " + s.RemoteEndPoint + " connected");
            };
            
            server.PacketReceived += (s, e) => {
                Console.WriteLine("Received " + e.Length + " from " + s.RemoteEndPoint);
            };

            //Start server
            server.Listen();

            ProtocolSocket.ProtocolSocket sock = new ProtocolSocket.ProtocolSocket(socketOptions);
            sock.Connect("127.0.0.1", 9090);
            while (true) {
                sock.SendPacket(new byte[1024]);
                Thread.Sleep(5);   
            }

About

A very simple tcp socket library that handles message framing and has optional dos protection.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%