Click here to Skip to main content
15,896,063 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.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;

using EchoSocketService;

namespace EchoFormTemplate
{
    
    public partial class frmEchoForm : Form
    {

        public OnEventDelegate FEvent;
        private int FConnectionCount;
        
        public frmEchoForm()
        {
            InitializeComponent();
        }

        public void Event(string eventMessage)
        {
            Event(eventMessage, false);
        }

        public void Event(string eventMessage, bool ex)
        {

            if (lstMessages.InvokeRequired)
            {
                lstMessages.Invoke(new OnEventDelegate(delegate(string s) { Event(s); }), eventMessage);
            }
            else
            {

                if (eventMessage.Contains("Connected"))
                {
                    Interlocked.Increment(ref FConnectionCount);
                }

                if (eventMessage.Contains("Disconnected"))
                {
                    Interlocked.Decrement(ref FConnectionCount);
                }

                this.Text = FConnectionCount.ToString() + " Connections";

                string[] s = eventMessage.Split('\n');

                for (int i = s.Length - 1; i >= 0; i--)
                {
                    lstMessages.BeginUpdate();
                    lstMessages.Items.Insert(0, s[i]);

                    if (lstMessages.Items.Count > 100)
                        lstMessages.Items.RemoveAt(100);

                    lstMessages.EndUpdate();
                }

            }

        }

        private void frmServer_Load(object sender, EventArgs e)
        {
            FEvent = new OnEventDelegate(Event);
            FConnectionCount = 0;
        }

        public void OnException(Exception ex)
        {
            Event("Service Exception! - " + ex.Message, true);
            Event("------------------------------------------------", true);
        }

        private void lstMessages_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}

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