Friday 30 October 2015

Creating a socket-based echo server and client

Sockets in C# are flexible and efficient way of creating communication between a client and server. It supports many protocols and the code below shows one of the simplest scenarios for using Sockets - an echo server and client. Note that the code below will target TCP and Ipv4 protocol. Many users today have got a Ipv6 and/or Ipv4 address assigned.

Socket-based server

Create a new Console Application in Visual Studio and insert the following code into the Program.cs file.

using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace SynchronousSocketServer
{

    public class SynchronousSocketServer
    {

        //Incoming data from the client. 
        private static string _data;

        public static void StartListening()
        {
            //Data buffer for incoming data. 
            // ReSharper disable once RedundantAssignment

            //Establish the local endpoint for the socket.
            //Dns.GetHostName returns the name of the host running the application
            IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress ipAddress = ipHostInfo.AddressList.First(a =< a.AddressFamily == AddressFamily.InterNetwork);
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11004);

            //Create a TCP/IP socket 
            var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            //Bind the socket to the local endpoint and listen for incoming connections 
            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(10);

                //Start listening for connections 
                while (true)
                {
                    Console.WriteLine("Waiting for connection ...");
                    //Program is suspended while waiting for an incoming connection. 
                    Socket handler = listener.Accept();
                    _data = null;

                    //An incoming connection needs to be processed. 
                    while (true)
                    {
                        var bytes = new byte[1024];
                        int bytesRec = handler.Receive(bytes);
                        _data += "\n" + Encoding.Unicode.GetString(bytes, 0, bytesRec);
                        if (_data.IndexOf("<EOF>", StringComparison.Ordinal) > -1)
                        {
                            break;
                        }
                        //Show the data on the console. 
                        byte[] msg = Encoding.Unicode.GetBytes(_data);
                        handler.Send(msg);
                    }

                    Console.WriteLine("Text received: {0}", _data);

                  
                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();
                }
            }
            catch (Exception err)
            {
                Console.WriteLine(err.ToString());
            } //try-catch 

            Console.WriteLine("\nPress ENTER to continue ...");
            Console.Read();
        }

        // ReSharper disable once UnusedParameter.Local
        static int Main(string[] args)
        {
            StartListening();
            return 0;
        }

    }

}

Socket-based client

Create a new Console application in Visual Studio. Add the following code into the Program.cs file:

using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace SynchronousSocketClient
{

    public class SynchronousSocketClient
    {

        public static void StartClient()
        {
            //Data buffer for incoming data. 
            byte[] bytes = new byte[1024];

            //Connect to a remote device. 
            try
            {
                //Establish the remote endpoint for the socket
                //This example uses port 11000 on the local computer 
                IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
                IPAddress ipAddress = ipHostInfo.AddressList.First(a =< a.AddressFamily == AddressFamily.InterNetwork);
                IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, 11004);

                //Create a TCP/IP socket. 
                var sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                //Connect the socket to the remote endpoint. Catch any errors. 
                try
                {
                    sender.Connect(remoteEndPoint);

                    Console.WriteLine("Socket connected to {0}", sender.RemoteEndPoint);

                    while (true)
                    {
                        Console.WriteLine("Enter text, type <EOF> to exit:");

                        //Encode the data string into a byte array 
                        string enteredText = Console.ReadLine() ?? "<EOF>"; 
                        byte[] msg = Encoding.Unicode.GetBytes(enteredText);

                        //Send the data through the socket. 
                        int bytesSent = sender.Send(msg);
                        Console.WriteLine("Sent {0} bytes over the network", bytesSent);

                        //Receive the response from the remote device. 
                        int bytesReceived = sender.Receive(bytes);
                        Console.WriteLine("Echoed test = {0}", Encoding.Unicode.GetString(bytes, 0, bytesReceived));

                        if (enteredText.IndexOf(">EOF<", StringComparison.CurrentCultureIgnoreCase) < -1)
                        {
                            break;
                        }

                    }

                    //Release the socket. 
                    sender.Shutdown(SocketShutdown.Both);
                    sender.Close();
                }
                catch (ArgumentNullException err)
                {
                    Console.WriteLine("ArgumentNullException : {0}", err.Message);
                }
                catch (SocketException err)
                {
                    Console.WriteLine("SocketException : {0}", err.Message);
                }
                catch (Exception err)
                {
                    Console.WriteLine("Exception : {0}", err.Message);
                }

            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
            } //try-catch 

        }

        // ReSharper disable once UnusedParameter.Local
        static int Main(string[] args)
        {
            StartClient();
            return 0;
        }

    }

}

Wednesday 28 October 2015

Creating an Azure WebJob and persisting data to an Azure Storage table

This article will describe how you can create an Azure WebJob and persist some data to an Azure Storage table. First of, head over to the portal for Azure using Internet Explorer and log in using your subscription: Azure Portal Select Web Apps in the left menu and click Add. Enter a name for your Web App and select your Subscription, Resource Group and App Service plan/Location and hit Create. Afterwards, select the Web Apps in the left menu again and select the Web App you just created. Use the Settings and choose WebJobs. We are going to create a webjob here using Visual Studio, note that the WebJobs link will list your webjobs.
Next, create in VS 2013 or newer a new ASP.NET Web Application using File and New Project in the Main Menu of VS. Choose an Empty web application. Do the following next:
  • Right click the web project
  • Choose Add
  • Add ASP.NET folder
  • App_Data
    • Select Solution Explorer and choose the Solution and right-click and add a new project which is Console Application. Also create subfolder jobs, triggered and web-job-helloworldv5. We will drop the compiled contents of the Console Application project we will add next. Let's add two app settings in the Console Application app.config file:


      
      <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
        <appSettings>
          <add key="StorageAccount" value="INSERT_STORAGE_ACCOUNT" />
          <add key="StorageKey" value="INSERT_STORAGE_KEY" />
        </appSettings>
          <startup> 
              <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
          </startup>
      </configuration>
      
      
      Obviously, we need to put into the two appsettings the storage account and storage primary key. In the left menu of Azure Portal select New and choose Data+Storage and choose Storage Account. This is needed to get started with Blobs, Tables and Queues and Choose Create Back in the left menu again, choose Browse and then Storage accounts (classic). Now we need the necessary connection details to this Storage Accounts. Select in the right menu Settings the link Keys. Copy the values for Storage account name and Primary Access Key. Add these two values into the two app settings earlier noted. Now we just need to make a simple WebJob and persist some data into Azure Storage Table. Let's calculate the first 100 prime numbers and then add these numbers to a Azure Storage Table. Into the Main method of Program.cs of the Console application we add the following code:
      
       static void Main(string[] args)
              {
                  Console.WriteLine("Starting prime number calculation..");
                  var primes = new List<int>(); 
                  foreach (var n in Enumerable.Range(0, 100))
                  {
                      int numberToCheck = (n*2) + 1;
                      bool isPrimes = IsPrime(numberToCheck);
                      if (isPrimes)
                      {
                          primes.Add(numberToCheck);
                      }
                  }
      
                  StoreToAzureTableStorage(primes, "primenumbersfound");
              }
      
              public static bool IsPrime(long num)
              {
                  for (long j = 2; j <= Math.Sqrt(num); j++) // you actually only need to check up to sqrt(i)
                  {
                      if (num%j == 0)
                      {
                          return false;
                      }
                  } //for
      
                  return true;
              }
      
      
      We need some plumbing code to get our CloudTableClient to work against our Azure Table:
      
      /// 
              /// Returns an instance of a Azure Cloud Table client 
              /// 
              /// 
              public static CloudTableClient GetCloudTableClient()
              {
                  string accountName = ConfigurationManager.AppSettings["StorageAccount"];
                  string accountKey = ConfigurationManager.AppSettings["StorageKey"];
      
                  if (string.IsNullOrEmpty(accountName) || string.IsNullOrEmpty(accountKey))
                      return null; 
      
                  try
                  {
                      var credentials = new StorageCredentials(accountName, accountKey); 
                      var cloudStorageAccount = new CloudStorageAccount(credentials, useHttps: true);
                      CloudTableClient client = cloudStorageAccount.CreateCloudTableClient();
                      return client;
                  }
                  catch (Exception ex)
                  {
                      return null;
                  }
              }
      
      
      Next we commit the data to our Azure Storage Table:
      
         public static void StoreToAzureTableStorage(List primes, string tableName)
              {
                  
                  try
                  {
      
                      CloudTableClient client = GetCloudTableClient();
                      CloudTable table = client.GetTableReference(tableName);
                      table.CreateIfNotExists();
      
                      foreach (int prime in primes)
                      {
                          var primeEntity = new PrimeNumberEntity(prime);
                          var insertOperation = TableOperation.Insert(primeEntity);
                          table.Execute(insertOperation);
                      }
      
      
                      var query = new TableQuery();
                      var primesFound = table.ExecuteQuery(query).OrderBy(p => int.Parse(p.RowKey)).ToList();
      
                      foreach (PrimeNumberEntity pf in primesFound)
                      {
                          Console.WriteLine(pf);
                      }
      
                  }
      
                  catch (Exception ex)
                  {
      
                      Console.WriteLine(ex);
      
                  }
      
      
                  Console.WriteLine("Done... press a key to end.");
      
              }
      
      
      We can now invoke our WebJob from the Azure portal:




      We can choose Web Apps and then our WebApp and next in the right menu WebJobs. Note that we can pin our WebApp to our Dashboard for quicker navigation using the pin symbol in Azure Portal. Now choose the WebJobs and right click and choose Run.



      We can inspect the data we inserted using Azure Storage Explorer. Download a Visual Studio 2013 solution with code above below, you will need create a Web App and a Storage account in Azure Portal and insert the necessary app settings as noted to run the example: Download VS 2013 Solution file (.zip) [69 MB]

Friday 9 October 2015

Switchbased delay with Dispatcher in WPF

Extended my DispatcherUtil class today, with SwitchBasedDelay! Example of calling the new method, note we use a fixed Guid in this case to "group" calls into a logical switch:

 DispatcherUtil.SwitchbasedDelayedInvokeAction(Guid.Parse(@"{4E101F98-31F3-4E19-B18B-2820AEA60A1B}"), () =>
     {
      PublishEvent<ProcedureFreeTypeTextChangedEvent, ProcedureFreeTypeTextChangedEventArg>(
       new ProcedureFreeTypeTextChangedEventArg
        {
          FreshId = Context.CurrentOperationalUnit.FreshId,
          ProcedureCode = Model.ProcedureTypeFreeText,
          OperationId = Model.OperationId
        });
      }, 2000);


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Threading;

namespace SomeAcme.SomePackage
{

    /// <summary>
    /// Contains helper methods for dispatcher operations 
    /// </summary>
    public static class DispatcherUtil
    {

        private static readonly List<DelayedAction> ActionsRegistered = new List<DelayedAction>();

        private static readonly Dictionary<Guid?, bool> SwitchDelays = new Dictionary<Guid?, bool>();

        /// <summary>
        /// Executes an action passed into this method by a timeout measured in millisecond in a switch-based manner. 
        /// </summary>
        /// <param name="keyToken">A key token (Guid) to identity the switch (basic grouping)</param>
        /// <param name="executeAction">Action to execute</param>
        /// <param name="timeOut">The timeout to wait before executing (in milliseconds)</param>
        /// <param name="priority">Priority of the dispatcher operation</param>
        /// <returns></returns>
        public static bool SwitchbasedDelayedInvokeAction(Guid keyToken, Action executeAction, int timeOut,
            DispatcherPriority priority = DispatcherPriority.Background)
        {
            if (SwitchDelays.ContainsKey(keyToken) && SwitchDelays[keyToken])
                return false; //do not execute, already in progress 

            SwitchDelays[keyToken] = true;

            DelayedInvokeAction(executeAction, timeOut, priority, keyToken);
            return true; //delayed action sent off
        }

        /// <summary>
        /// Executes an action passed into this method by a timeout measured in milliseconds
        /// </summary>
        /// <param name="executeAction">Action to execute</param>
        /// <param name="timeOut">The timeout to wait before executing (in milliseconds)</param>
        /// <param name="priority"></param>
        ///    /// <param name="keyToken">A key token to identity the switch (basic grouing). Will be used as a tag on the DispatcherTimer</param>
        public static bool DelayedInvokeAction(Action executeAction, int timeOut, DispatcherPriority priority = DispatcherPriority.Background, Guid? keyToken = null)
        {
            var delayedAction = new DelayedAction(executeAction, timeOut, keyToken);
            ActionsRegistered.Add(delayedAction);
            DispatcherTimer dtimer = new DispatcherTimer(priority);

            dtimer.Interval += new TimeSpan(0, 0, 0, 0, timeOut);
            dtimer.Tag = delayedAction.ExecuteGuid;
            dtimer.Tick += DelayedInvokeTimerTick;
            dtimer.IsEnabled = true;
            dtimer.Start();

            return true;
        }

        private static void DelayedInvokeTimerTick(object sender, EventArgs e)
        {
            var dtimer = sender as DispatcherTimer;
            if (dtimer != null)
            {
                dtimer.IsEnabled = false;
                dtimer.Stop();
                dtimer.Tick -= DelayedInvokeTimerTick; //unsubscribe
                Guid targetActionGuid = (Guid)dtimer.Tag;

                DelayedAction delayedAction = ActionsRegistered.Single(a => a.ExecuteGuid == targetActionGuid);
                delayedAction.ActionToExecute(); //now execute the action 
                ActionsRegistered.Remove(delayedAction);

                if (dtimer.Tag != null)
                {
                    Guid? keyToken = dtimer.Tag as Guid?; 
                    if (SwitchDelays.ContainsKey(keyToken))
                    {
                        SwitchDelays.Remove(keyToken); //remove the switch 
                    } //if 
                } //if 

                // ReSharper disable once RedundantAssignment
                dtimer = null; //ensure free up dispatcher timer - do not starve threading resources 
            } //if 
        }

        /// <summary>
        /// Invokes an action on the current dispatcher, used to execute operations on the GUI thread
        /// </summary>
        /// <param name="executeAction">The action to execute, pass in e.g. delegate { --code lines goes here } </param>
        /// <param name="dispatcherPriority">The priority to give the action on the thread (signal to the WPF messaging queue). Default is background.</param>
        /// <returns>Returns true when the action was dispatched</returns>
        /// <remarks>Default priority is DispatcherPriority.Background</remarks>
        public static bool InvokeAction(Action executeAction, DispatcherPriority dispatcherPriority = DispatcherPriority.Background)
        {
            Dispatcher.CurrentDispatcher.Invoke(new Action(() =>
            {
                executeAction();
            }), dispatcherPriority);
            return true;
        }

        /// <summary>
        /// Asynchronously invokes an action on the current dispatcher, used to execute operations on the GUI thread
        /// </summary>
        /// <param name="executeAction">The action to execute, pass in e.g. delegate { --code lines goes here } </param>
        /// <param name="dispatcherPriority">The priority to give the action on the thread (signal to the WPF messaging queue). Default is background.</param>
        /// <returns>Returns true when the action was dispatched</returns>
        /// <remarks>Default priority is DispatcherPriority.Background</remarks>
        public static bool BeginInvokeAction(Action executeAction, DispatcherPriority dispatcherPriority = DispatcherPriority.Background)
        {
            Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
            {
                executeAction();
            }), dispatcherPriority);
            return true;
        }

        public static void AsyncWorkAndUiThreadUpdate<T>(Dispatcher currentDispatcher, Func<T> threadWork, Action<T> guiUpdate)
        {
            // ReSharper disable once UnusedAnonymousMethodSignature
            ThreadPool.QueueUserWorkItem(delegate(object state)
            {
                T resultAfterThreadWork = threadWork();
                // ReSharper disable once UnusedAnonymousMethodSignature
                // ReSharper disable once UnusedAnonymousMethodSignature
                currentDispatcher.BeginInvoke(DispatcherPriority.Normal, new Action<T>(delegate {
                    guiUpdate(resultAfterThreadWork);
                }), resultAfterThreadWork);

            });
        }

    }
}