Click here to Skip to main content
15,896,063 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.Threading;
using System.Text;
using System.Security.Cryptography;
using System.Net.Sockets;
using System.Diagnostics;

using ALAZ.SystemEx.NetEx.SocketsEx;
using ALAZ.SystemEx.ThreadingEx;

namespace EchoSocketService
{
 
    #region Delegates

    public delegate void OnEventDelegate(string eventMessage);

    #endregion

    public class EchoSocketService : BaseSocketService
    {

        #region Fields

        private OnEventDelegate FOnEventDelegate;

        #endregion

        #region Constructor

        public EchoSocketService()
        {
            FOnEventDelegate = null;
        }

        public EchoSocketService(OnEventDelegate eventDelegate)
        {
            FOnEventDelegate = eventDelegate;
        }

        #endregion

        #region Methods

        #region Event
        
        private void Event(string eventMessage)
        {

            if (FOnEventDelegate != null)
            {
                FOnEventDelegate(eventMessage);
            }

        }
        
        #endregion

        #region SleepRandom
        
        public void SleepRandom(HostType hostType)
        {
            Random r = new Random(DateTime.Now.Millisecond);
            
            if (hostType == HostType.htServer)
            {
                ThreadEx.SleepEx( r.Next(1000, 2000) );
            }
            else
            {
                ThreadEx.SleepEx( r.Next(10000, 15000));
            }
            
        }
        
        #endregion

        #region GetMessage
        
        public byte[] GetMessage(int handle)
        {

            Random r = new Random(handle + DateTime.Now.Millisecond);
            
            byte[] message = new byte[r.Next(246, 1000)];

            for (int i = 0; i < message.Length; i++)
            {
                message[i] = (byte)r.Next(32, 122);
            }

            return message;

        }
        
        #endregion

        #region OnConnected
        
        public override void OnConnected(ConnectionEventArgs e)
        {

            StringBuilder s = new StringBuilder();

            s.Append("------------------------------------------------" + "\r\n");
            s.Append("Connected - " + e.Connection.ConnectionId + "\r\n");
            s.Append(e.Connection.Host.HostType.ToString() + "\r\n");
            s.Append(e.Connection.Creator.Name + "\r\n");
            s.Append(e.Connection.Creator.EncryptType.ToString() + "\r\n");
            s.Append(e.Connection.Creator.CompressionType.ToString() + "\r\n");
            s.Append("------------------------------------------------" + "\r\n");

            Event(s.ToString());

            s.Length = 0;

            Thread.Sleep(123);

            if (e.Connection.Host.HostType == HostType.htServer)
            {
                e.Connection.BeginReceive();
            }
            else
            {
                byte[] b = GetMessage(e.Connection.SocketHandle.ToInt32());
                e.Connection.BeginSend(b);
            }

            
        }
        
        #endregion

        #region OnSent
        
        public override void OnSent(MessageEventArgs e)
        {

            if (!e.SentByServer)
            {
                StringBuilder s = new StringBuilder();

                s.Append("------------------------------------------------" + "\r\n");
                s.Append("Sent - " + e.Connection.ConnectionId + "\r\n");
                s.Append("Sent Bytes - " + e.Connection.WriteBytes.ToString() + "\r\n");
                s.Append("------------------------------------------------" + "\r\n");

                Event(s.ToString().Trim());

                s.Length = 0;

            }


            if (e.Connection.Host.HostType == HostType.htServer)
            {
    
                if (!e.SentByServer)
                {
                    e.Connection.BeginReceive();
                }

            }
            else
            {
                e.Connection.BeginReceive();
            }

        }
        
        #endregion

        #region OnReceived
        
        public override void OnReceived(MessageEventArgs e)
        {

            StringBuilder s = new StringBuilder();

            s.Append("------------------------------------------------" + "\r\n");
            s.Append("Received - " + e.Connection.ConnectionId + "\r\n");
            s.Append("Received Bytes - " + e.Connection.ReadBytes.ToString() + "\r\n");
            s.Append("------------------------------------------------" + "\r\n");

            Event(s.ToString());
            s.Length = 0;

            SleepRandom(e.Connection.Host.HostType);

            if (e.Connection.Host.HostType == HostType.htServer)
            {
                e.Connection.BeginSend(e.Buffer);
            }
            else
            {
                byte[] b = GetMessage(e.Connection.SocketHandle.ToInt32());
                e.Connection.BeginSend(b);
            }

        }
        
        #endregion

        #region OnDisconnected
        
        public override void OnDisconnected(ConnectionEventArgs e)
        {

            StringBuilder s = new StringBuilder();

            s.Append("------------------------------------------------" + "\r\n");
            s.Append("Disconnected - " + e.Connection.ConnectionId + "\r\n");
            s.Append("------------------------------------------------" + "\r\n");

            Event(s.ToString());
            s.Length = 0;

            if (e.Connection.Host.HostType == HostType.htServer)
            {
                //------
            }
            else
            {
                e.Connection.AsClientConnection().BeginReconnect();
            }

        }
        
        #endregion

        #region OnException
        
        public override void OnException(ExceptionEventArgs e)
        {

            StringBuilder s = new StringBuilder();

            s.Append("------------------------------------------------" + "\r\n");
            s.Append("Exception - " + e.Exception.GetType().ToString() + "\r\n");
            s.Append("Exception Message - " + e.Exception.Message + "\r\n");

            if (e.Exception is ReconnectAttemptException)
            {
                s.Append("Attempted   - " + ((ReconnectAttemptException)e.Exception).Attempt.ToString() + "\r\n");
                s.Append("Max Reached - " + ((ReconnectAttemptException)e.Exception).MaxReached.ToString() + "\r\n");
                s.Append("------------------------------------------------" + "\r\n");
                s.Append("Inner Error - " + ((SocketException)e.Exception.InnerException).ErrorCode.ToString() + "\r\n");
                s.Append("------------------------------------------------" + "\r\n");
                s.Append("Creator - " + ((ReconnectAttemptException)e.Exception).Creator.Name + "\r\n");
                s.Append("Creator - " + ((ReconnectAttemptException)e.Exception).Creator.EncryptType.ToString() + "\r\n");
                s.Append("Creator - " + ((ReconnectAttemptException)e.Exception).Creator.CompressionType.ToString() + "\r\n");

            }

            if (e.Exception is SocketException)
            {
                s.Append("Socket Error - " + ((SocketException)e.Exception).ErrorCode.ToString() + "\r\n");
            }

            s.Append("------------------------------------------------" + "\r\n");

            Event(s.ToString());
            s.Length = 0;

            if (e.Connection != null)
            {
                e.Connection.BeginDisconnect();
            }
          
        }
        
        #endregion

        #endregion

    }

}

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