Click here to Skip to main content
15,879,239 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.3K   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.Collections;
using System.Diagnostics;
using SocketServerLib.SocketHandler;

namespace SocketServerLib.Server
{
    /// <summary>
    /// This class contains a list of client in a Socket Server.
    /// </summary>
    class ClientInfoList : IDisposable
    {
        /// <summary>
        /// Hashtable to store the clients
        /// </summary>
        private Hashtable clientList = Hashtable.Synchronized(new Hashtable());

        #region Methods to add/remove a client

        /// <summary>
        /// Add a client to the list.
        /// </summary>
        /// <param name="client">The client to add</param>
        public void AddClient(ClientInfo client)
        {
            lock (this)
            {
                if (!clientList.ContainsKey(client))
                {
                    clientList.Add(client.TcpSocketClientHandler, client);
                    Trace.WriteLine(string.Format("{0} added to the server", client));
                }
            }
        }

        /// <summary>
        /// Remove a client from the list.
        /// </summary>
        /// <param name="client">The client to remove</param>
        public void RemoveClient(ClientInfo client)
        {
            if (client == null)
            {
                return;
            }
            lock (this)
            {
                if (clientList.ContainsKey(client.TcpSocketClientHandler))
                {
                    clientList.Remove(client.TcpSocketClientHandler);
                    Trace.WriteLine(string.Format("{0} removed from the server", client));
                }
            }
        }

        #endregion

        #region Properties

        /// <summary>
        /// Get the number of clients.
        /// </summary>
        public virtual int Count
        {
            get
            {
                lock (this)
                {
                    return this.clientList.Count;
                }
            }
        }

        /// <summary>
        /// Get from the client list the Client Info connected to the specific socket client handler.
        /// </summary>
        /// <param name="abstractTcpSocketClientHandler">The socket client handler to find</param>
        /// <returns>The client info of the socket client handler</returns>
        public virtual ClientInfo this[AbstractTcpSocketClientHandler abstractTcpSocketClientHandler]
        {
            get
            {
                lock (this)
                {
                    if (!this.clientList.ContainsKey(abstractTcpSocketClientHandler))
                    {
                        return null;
                    }
                    return (ClientInfo)this.clientList[abstractTcpSocketClientHandler];
                }
            }
        }

        /// <summary>
        /// Get from the client list the Client Info of the specific ClientUID.
        /// </summary>
        /// <param name="clientUID">The client UID to find</param>
        /// <returns>The client info of the client UID</returns>
        public virtual ClientInfo this[string clientUID]
        {
            get
            {
                lock (this)
                {
                    ClientInfo ret = null;
                    foreach (ClientInfo client in this.clientList.Values)
                    {
                        if(client.ClientUID.Equals(clientUID))
                        {
                            ret = client;
                            break;
                        }
                    }
                    return ret;
                }
            }
        }

        #endregion

        /// <summary>
        /// Return a clone of the client list.
        /// </summary>
        /// <returns>A clone of the client list as a Client Info array</returns>
        public ClientInfo[] CloneClientList()
        {
            lock (this)
            {
                ClientInfo[] handlerList = new ClientInfo[clientList.Count];
                clientList.Values.CopyTo(handlerList, 0);
                return handlerList;
            }
        }

        #region IDisposable Members

        /// <summary>
        /// Dispose all the contained clients.
        /// </summary>
        public void Dispose()
        {
            lock (this)
            {
                foreach(ClientInfo clientInfo in this.clientList.Values)
                {
                    if (clientInfo.TcpSocketClientHandler != null)
                    {
                        clientInfo.TcpSocketClientHandler.Dispose();
                    }
                }
            }
        }

        #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