Click here to Skip to main content
15,881,757 members
Articles / Programming Languages / C#

An Asynchronous Socket Server and Client

Rate me:
Please Sign up or sign in to vote.
4.89/5 (265 votes)
29 Apr 2009CPOL16 min read 4M   25.6K   1.1K  
An asynchronous socket server and client with encryption and compression.
using System;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Security.Cryptography;
using System.Text;

using EchoSocketService;
using EchoCryptService;

using ALAZ.SystemEx.NetEx.SocketsEx;

namespace Main
{

    class MainClass
    {

        [STAThread]
        static void Main(string[] args)
        {

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            //----- Socket Server!
            OnEventDelegate FEvent = new OnEventDelegate(Event);

            SocketServer echoServer = new SocketServer(CallbackThreadType.ctWorkerThread, new EchoSocketService.EchoSocketService(FEvent));

            echoServer.Delimiter = new byte[] { 0xFF, 0x00, 0xFE, 0x01, 0xFD, 0x02 };
            echoServer.DelimiterType = DelimiterType.dtMessageTailExcludeOnReceive;
            
            echoServer.SocketBufferSize = 1024;
            echoServer.MessageBufferSize = 2048;
            
            echoServer.IdleCheckInterval = 60000;
            echoServer.IdleTimeOutValue = 120000;

            //----- Socket Listener!
            SocketListener listener = echoServer.AddListener("Commom Port - 8090", new IPEndPoint(IPAddress.Any, 8090));

            listener.AcceptThreads = 3;
            listener.BackLog = 100;

            listener.EncryptType = EncryptType.etNone;
            listener.CompressionType = CompressionType.ctNone;
            listener.CryptoService = new EchoCryptService.EchoCryptService();
            
            echoServer.Start();
 
            Console.WriteLine("Started!");
            Console.WriteLine("----------------------");

            int iot = 0;
            int wt = 0;

            ThreadPool.GetAvailableThreads(out wt, out iot);
            Console.WriteLine("Threads Work - " + wt.ToString());
            Console.WriteLine("Threads I/O  - " + iot.ToString());

            string s;
            
            do
            {

                s = Console.ReadLine();

                if (s.Equals("g"))
                {

                    ThreadPool.GetAvailableThreads(out wt, out iot);
                    Console.WriteLine("Threads Work " + iot.ToString());
                    Console.WriteLine("Threads I/O  " + wt.ToString());

                }

            } while (s.Equals("g"));

            try
            {
                echoServer.Stop();
                echoServer.Dispose();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            echoServer = null;

            Console.WriteLine("Stopped!");
            Console.WriteLine("----------------------");
            Console.ReadLine();

        }

        static void echoServer_OnException(Exception ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Service Exception! - " + ex.Message);
            Console.WriteLine("------------------------------------------------");
            Console.ResetColor();
        }

        static void Event(string eventMessage)
        {
            if (eventMessage.Contains("Exception"))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(eventMessage);
                Console.ResetColor();
            }
            else
            {
                Console.WriteLine(eventMessage);
            }

        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Console.WriteLine(e.ExceptionObject.ToString());
            Console.ReadLine();
        }

    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Brazil Brazil
- Living in São Paulo, Brazil
- Developing since 1994 with
* Clipper (Summer '87 and 5.02)
* FoxPro (DOS, 2.6), Visual Foxpro (6, 7, 8, 9)
* Delphi (1, 2, 5, 7, 2007)
* C# (2.0, 4.0)

Comments and Discussions