Click here to Skip to main content
15,891,184 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.5K   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.Net.Sockets;
using System.Diagnostics;
using System.Net;

namespace SocketServerLib.SocketHandler
{
    /// <summary>
    /// This class represents a Socket (not SSL). Incapsulates the system Socket class in order to allow different implementations.
    /// </summary>
    public class TcpSocket : IDisposable
    {
        /// <summary>
        /// The underlayer 
        /// </summary>
        protected Socket socket = null;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="socket">The system socket</param>
        public TcpSocket(Socket socket) 
        {
            this.socket = socket;
            Trace.WriteLine(string.Format("Create TcpSocket for EndPoint {0}", this.socket.RemoteEndPoint));
        }

        #region Properties

        /// <summary>
        /// Get or Set the system socket Keep Alive flag
        /// </summary>
        public bool KeepAlive
        {
            get
            {
                return (bool)this.socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive);
            }
            set
            {
                this.socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, value);
            }
        }

        /// <summary>
        /// Get or Set the system socket send time out
        /// </summary>
        public int SendTimeout
        {
            get
            {
                return (int)this.socket.SendTimeout;
            }
            set
            {
                this.socket.SendTimeout = value;
            }
        }

        /// <summary>
        /// Get the remote end point.
        /// </summary>
        public EndPoint IPEndPoint
        {
            get
            {
                return socket.RemoteEndPoint;
            }
        }

        /// <summary>
        /// Get if the sockect is connected.
        /// </summary>
        public bool Connected
        {
            get
            {
                return this.socket.Connected;
            }
        }

        #endregion

        /// <summary>
        /// Close the socket.
        /// </summary>
        public virtual void Close()
        {
            try
            {
                if (socket != null)
                {
                    if (socket.Connected)
                    {
                        socket.Shutdown(SocketShutdown.Both);
                    }
                    socket.Close();
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(string.Format("Exception in Close:{0}", ex));
            }
        }

        #region Methods for Receive/Send data

        /// <summary>
        /// Start the asynchronous receiving data. 
        /// </summary>
        /// <param name="state">The socket state object to receive data</param>
        /// <param name="callback">The callback for the asynchronous receiving</param>
        internal virtual void BeginReceive(SocketStateObject state, AsyncCallback callback)
        {
            this.socket.BeginReceive(state.buffer, 0, SocketStateObject.BufferSize, 0, callback, state);
        }

        /// <summary>
        /// Stop the asynchronous receiving.
        /// </summary>
        /// <param name="result">The socket state object to receive data</param>
        /// <returns>The number of bytes received</returns>
        internal virtual int EndReceive(IAsyncResult result)
        {
            int r = 0;
            try
            {
                if (this.socket.Connected)
                {
                    r = this.socket.EndReceive(result);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(string.Format("Exception in EndReceive:{0}", ex));
            }
            return r;
        }

        /// <summary>
        /// Send a buffer. This method is synchronus.
        /// </summary>
        /// <param name="buffer">The buffer to send</param>
        /// <returns>The number of bytes sent</returns>
        internal virtual int Send(byte[] buffer)
        {
            return this.socket.Send(buffer);
        }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// Close and dispose the sockect.
        /// </summary>
        public virtual void Dispose()
        {
            try
            {
                this.Close();
                socket = null;
            }
            catch (Exception ex)
            {
                Trace.WriteLine(string.Format("Exception in Dispose:{0}", ex));
            }
        }

        #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