Click here to Skip to main content
15,891,943 members
Articles / Programming Languages / C#

An Asynchronous Socket Server and Client

Rate me:
Please Sign up or sign in to vote.
4.89/5 (265 votes)
29 Apr 2009CPOL16 min read 4M   25.6K   1.1K  
An asynchronous socket server and client with encryption and compression.
using System;
using System.Text;
using System.Net.Sockets;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

using ALAZ.SystemEx;
using ALAZ.SystemEx.NetEx.SocketsEx;
using ALAZ.SystemEx.ThreadingEx;

namespace ChatSocketService
{

    public class ChatSocketService : BaseSocketService
    {

        #region Fields

        private ReaderWriterLock FUsersSync;
        private Dictionary<long, ISocketConnection> FUsers;

        #endregion

        #region Constructor

        public ChatSocketService()
        {
            FUsersSync = new ReaderWriterLock();
            FUsers = new Dictionary<long, ISocketConnection>(100);
        }

        #endregion

        #region Methods

        #region Utils

        #region SerializeMessage

        public static byte[] SerializeMessage(ChatMessage msg)
        {
        
            using(MemoryStream m = new MemoryStream())
            {
            
                BinaryFormatter bin = new BinaryFormatter();
                bin.Serialize(m, msg);
            
                return m.ToArray();
                
            }
        
        }
        
        #endregion

        #region DeserializeMessage

        public static ChatMessage DeserializeMessage(byte[] buffer)
        {

            using(MemoryStream m = new MemoryStream())
            {

                m.Write(buffer, 0, buffer.Length);
                m.Position = 0;

                BinaryFormatter bin = new BinaryFormatter();

                return (ChatMessage) bin.Deserialize(m);
                
            }

        }

        #endregion

        #endregion
        
        #region OnConnected

        public override void OnConnected(ConnectionEventArgs e)
        {

            StringBuilder s = new StringBuilder();

            s.Append("\r\n------------------------------------------------\r\n");
            s.Append("New Client\r\n");
            s.Append(" Connection Id " + e.Connection.ConnectionId + "\r\n");
            s.Append(" Ip Address " + e.Connection.RemoteEndPoint.Address + "\r\n");
            s.Append(" Tcp Port " + e.Connection.RemoteEndPoint.Port + "\r\n");
            
            Console.WriteLine(s.ToString());
            s.Length = 0;
            
            e.Connection.UserData = new ConnectionData(ConnectionState.csConnected);
            e.Connection.BeginReceive();
            
        }

        #endregion

        #region OnSent

        public override void OnSent(MessageEventArgs e)
        {

            //if (!e.SentByServer)
            //{
            //    e.Connection.BeginReceive();
            //}

        }

        #endregion

        #region OnReceived

        public override void OnReceived(MessageEventArgs e)
        {

            ChatMessage msg = DeserializeMessage(e.Buffer);

            switch (msg.MessageType)
            {

                case MessageType.mtLogin:

                    ((ConnectionData)e.Connection.UserData).ConnectionState = ConnectionState.csAuthenticated;
                    ((ConnectionData)e.Connection.UserData).UserName = msg.UserInfo[0].UserName;

                    msg.UserInfo[0].UserId = e.Connection.ConnectionId;
                    e.Connection.BeginSend(SerializeMessage(msg));
                    
                    msg.MessageType = MessageType.mtAuthenticated;
                    e.Connection.AsServerConnection().BeginSendToAll(SerializeMessage(msg), false);
                    
                    ISocketConnection[] cnns = e.Connection.AsServerConnection().GetConnections();
                    
                    if ( (cnns != null) && (cnns.Length > 0) )
                    {
                        
                        bool send = false;
                        
                        msg.MessageType = MessageType.mtHello;
                        msg.UserInfo = new UserInfo[cnns.Length];
                        
                        for (int i = 0; i < cnns.Length; i++)
        			    {
                            
                            if (cnns[i] != e.Connection)
                            {
                                msg.UserInfo[i].UserName = ((ConnectionData)cnns[i].UserData).UserName;
                                msg.UserInfo[i].UserId = cnns[i].ConnectionId;
                                send = true;
                            }
                            
		        	    }

                        if (send)
                        {
                            e.Connection.AsServerConnection().BeginSend(SerializeMessage(msg));
                        }
                        
                    }                    
                    
                    break;

                case MessageType.mtMessage:

                    e.Connection.AsServerConnection().BeginSendToAll(e.Buffer, false);

                    break;
                    
                    
                case MessageType.mtLogout:

                    e.Connection.AsServerConnection().BeginSendToAll(SerializeMessage(msg), false);
                    break;    
                
            }

            e.Connection.BeginReceive();

        }

        #endregion

        #region OnDisconnected

        public override void OnDisconnected(ConnectionEventArgs e)
        {

            StringBuilder s = new StringBuilder();

            s.Append("------------------------------------------------" + "\r\n");
            s.Append("Client Disconnected\r\n");
            s.Append(" Connection Id " + e.Connection.ConnectionId + "\r\n");
            
            e.Connection.UserData = null;
            
            Console.WriteLine(s.ToString());
            
            s.Length = 0;


        }

        #endregion

        #region OnException

        public override void OnException(ExceptionEventArgs e)
        {
            e.Connection.BeginDisconnect();
        }

        #endregion

        #endregion

    }
    
    [Serializable]
    public struct UserInfo
    {

        #region Fields

        private string FUserName;
        private long FUserId;

        #endregion
        
	    #region Properties
	    
	    public string UserName
	    {
            get { return FUserName; }
            set { FUserName = value; }
	    }

        public long UserId
        {
            get { return FUserId; }
            set { FUserId = value; }
        }

	    #endregion   
	    
        #region Methods

        public override string ToString()
        {
            return FUserName;
        }

        #endregion

    }

    [Serializable]
    public class ChatMessage
    {

        #region Fields

        private MessageType FMessageType;
        private UserInfo[] FUsers;
        private string FMessage;

        #endregion
        
        #region Constructor

        public ChatMessage() { }
		 
	    #endregion
	 
	    #region Properties

        public MessageType MessageType
        {
            get { return FMessageType; }
            set { FMessageType = value; }
        }

        public UserInfo[] UserInfo
	    {
            get { return FUsers; }
            set { FUsers = value; }
	    }

        public string Message
        {
            get { return FMessage; }
            set { FMessage = value; }
        }

	    #endregion   

    }

    public class ConnectionData
    {
        
        #region Fields

        private ConnectionState FConnectionState;
        private string FUserName;

        #endregion

        #region Constructor

        public ConnectionData(ConnectionState state)
        {
            FConnectionState = state;
            FUserName = String.Empty;
        }

        #endregion

        #region Properties

        public ConnectionState ConnectionState
        {
            get { return FConnectionState; }
            set { FConnectionState = value; }
        }

        public string UserName
        {
            get { return FUserName; }
            set { FUserName = value; }
        }

        #endregion
        
    }

    public enum MessageType
    {
        mtLogin,
        mtAuthenticated,
        mtHello,
        mtLogout,
        mtMessage
    }

    public enum ConnectionState
    {
        csConnected,
        csAuthenticated,
        csDisconnected
    }


}

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
Software Developer (Senior)
Brazil Brazil
- Living in São Paulo, Brazil
- Developing since 1994 with
* Clipper (Summer '87 and 5.02)
* FoxPro (DOS, 2.6), Visual Foxpro (6, 7, 8, 9)
* Delphi (1, 2, 5, 7, 2007)
* C# (2.0, 4.0)

Comments and Discussions