Click here to Skip to main content
15,889,833 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 161.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 SocketServerLib.SocketHandler;

namespace SocketServerLib.Server
{
    /// <summary>
    /// This is the class for a Client Info. Extend this class to add new properties.
    /// </summary>
    public class ClientInfo : IDisposable
    {
        /// <summary>
        /// The socket client handler connected as client
        /// </summary>
        protected AbstractTcpSocketClientHandler abstractTcpSocketClientHandler = null;
        /// <summary>
        /// The string for the client identification. For default is abstractTcpSocketClientHandler.ToString()
        /// </summary>
        protected string clientUID = null;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="abstractTcpSocketClientHandler">The socket client handler</param>
        public ClientInfo(AbstractTcpSocketClientHandler abstractTcpSocketClientHandler)
        {
            this.abstractTcpSocketClientHandler = abstractTcpSocketClientHandler;
            this.clientUID = this.abstractTcpSocketClientHandler.ToString();
        }

        #region Properties

        /// <summary>
        /// Get the underlayer socket client handler.
        /// </summary>
        public AbstractTcpSocketClientHandler TcpSocketClientHandler
        {
            get
            {
                return this.abstractTcpSocketClientHandler;
            }
        }

        /// <summary>
        /// Get the cient UID.
        /// </summary>
        public string ClientUID
        {
            get
            {
                return this.clientUID;
            }
        }

        #endregion

        /// <summary>
        /// Print on string the information.
        /// </summary>
        /// <returns>The client information</returns>
        public override string ToString()
        {
            if (this.abstractTcpSocketClientHandler == null)
            {
                return "Client Info: null";
            }
            return string.Format("Client Info: {0}", this.clientUID);
        }

        #region IDisposable Members

        /// <summary>
        /// Dispose the client.
        /// </summary>
        public void Dispose()
        {
            if (this.abstractTcpSocketClientHandler == null)
            {
                return;
            }
            this.abstractTcpSocketClientHandler.Dispose();
            this.abstractTcpSocketClientHandler = null;
        }

        #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