Click here to Skip to main content
15,886,788 members
Articles / Programming Languages / XML

A Full Library for a Socket Client/Server System

Rate me:
Please Sign up or sign in to vote.
4.81/5 (42 votes)
14 Jan 2015CPOL5 min read 160.9K   11.3K   149  
My article shows a library that everyone can use to create their socket communication. Also, it explains how the library is developed.
using System;
using System.Threading;
using System.Diagnostics;

namespace SocketServerLib.Threads
{
    /// <summary>
    /// This abstract class represents a thread. You have to implement the method ThreadLoop to define what the thread continuoisly do.
    /// To start the thread call Init and then StartUp. To stop the thread call Shutdown.
    /// </summary>
    public abstract class AbstractThread : IDisposable
    {
        /// <summary>
        /// The internal thread.
        /// </summary>
        private Thread th = null;
        /// <summary>
        /// Flag to stop the thread.
        /// </summary>
        protected bool shutdown = false;

        /// <summary>
        /// Default constructor.
        /// </summary>
        public AbstractThread()
        {
        }

        /// <summary>
        /// Implement this method to define what the thread do in its infinite loop.
        /// </summary>
        protected abstract void ThreadLoop();

        #region Public methods to start and stop the thread

        /// <summary>
        /// Init the thread.
        /// </summary>
        public virtual void Init()
        {
            th = new Thread(this.Run);
            th.Name = this.GetType().FullName;
        }

        /// <summary>
        /// Start the thread.
        /// </summary>
        public virtual void StartUp()
        {
            th.Start();
        }

        /// <summary>
        /// Stop the thread.
        /// </summary>
        public virtual void Shutdown()
        {
            lock (this)
            {
                if (shutdown)
                {
                    return;
                }
                shutdown = true;
                if (th != null)
                {
                    if (!th.Join(5000))
                    {
                        th.Interrupt();
                    }
                    if (!th.Join(5000))
                    {
                        th.Abort();
                    }
                    th = null;
                }
            }
        }

        /// <summary>
        /// This is the internal thread loop. The shutdown flag is checked and if false the abstract ThreadLoop method is called.
        /// It's a infinite loop until the shutdown.
        /// </summary>
        protected virtual void Run()
        {
            while (!shutdown)
            {
                try
                {
                    ThreadLoop();
                }
                catch (ThreadInterruptedException thInt)
                {
                    Trace.WriteLine(string.Format("Get exception {0} to exit from thread", thInt.GetType().FullName));
                }
                catch (ThreadAbortException thAbort)
                {
                    Trace.WriteLine(string.Format("Get exception {0} to exit from thread", thAbort.GetType().FullName));
                    shutdown = true;
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(string.Format("Exception {0}", ex));
                }
            }
            Trace.WriteLine("Exit from Run. Thread is stopped.");
        }

        #endregion

        #region IDisposable Members
        
        /// <summary>
        /// Shutdown and dispose the thread.
        /// </summary>
        public virtual void Dispose()
        {
            Shutdown();
        }

        #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
Team Leader Mediatech Solutions
Italy Italy
I’m an IT Project Manager for an Italian Betting Company and over the last 2 years I acquired experience in Betting area.
I have developed code in different object oriented languages (C#, C++, Java) for more than 10 years using a set of technology such as .Net, J2EE, multithreading, etc…

Comments and Discussions