Click here to Skip to main content
15,886,199 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.

namespace SocketServerLib.Message
{
    /// <summary>
    /// This abstract class represent a message header. You have to extend and implement this class to define your message header.
    /// </summary>
    public abstract class AbstractMessageHeader
    {
        /// <summary>
        /// Flag for a complete header read from a buffer.
        /// </summary>
        protected bool complete = true;

        /// <summary>
        /// Constructor for an empty message header.
        /// </summary>
        public AbstractMessageHeader()
        {
        }

        /// <summary>
        /// Get the ClientUID.
        /// </summary>
        public abstract string ClientUID { get; }
        /// <summary>
        /// Get the Message UID.
        /// </summary>
        public abstract string MessageUID { get; }
        /// <summary>
        /// Get the message length.
        /// </summary>
        public abstract int MessageLength { get; }
        /// <summary>
        /// Get the header length.
        /// </summary>
        public abstract int HeaderLength { get; }

        /// <summary>
        /// Write the header in a buffer.
        /// </summary>
        /// <param name="destBuffer">The buffer</param>
        /// <param name="offset">The offset in the buffer</param>
        /// <returns>The next position in the buffer after the header</returns>
        public abstract int Write(byte[] destBuffer, int offset);
        /// <summary>
        /// Read the header from a buffer.
        /// </summary>
        /// <param name="destBuffer">The buffer</param>
        /// <param name="offset">The offset in the buffer</param>
        /// <returns>The next position in the buffer after the header. Return 0 in case the hedaer is not complete</returns>
        public abstract int Read(byte[] sourceBuffer, int offset);

        /// <summary>
        /// Chekk if the header is complete.
        /// </summary>
        /// <returns>True if the header is complete, otherwise false</returns>
        public bool IsComplete()
        {
            return complete;
        }
    }
}

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