Thursday 12 November 2015

Reading a text file in VB6 and putting the contents into an array

VB6 or Visual Basic 6.0 is not used in many new projects, as it is replaced by .NET. But sometimes you are assigned at work to read and further develop or migrate Legacy code. Let's look at some simple VB6 code. We first read the contents of a file and put each line into a string array. Then we add the items to a listbox control.

Private Sub Command3_Click()

 Dim someFileHandle As Integer
 Dim fileName As String
 Dim someStrings() As String
 
 someFileHandle = FreeFile

 fileName = App.Path + "\fox.txt"
 
 ReDim someStrings(1000) As String

 Open fileName For Input As #someFileHandle
 
 Dim lineNo As Integer
 
 Do Until EOF(someFileHandle)
  Input #someFileHandle, someStrings(lineNo)
  lineNo = lineNo + 1
 Loop
 
 ReDim Preserve someStrings(lineNo - 1) As String
  
 List1.Clear
  
 For x = 0 To lineNo - 1
  List1.AddItem someStrings(x)
 Next x

End Sub


First we get a file handle to the file we want to open. We declare an integer and use the FreeFile method to get a filehandle. We then use the Open function to open a file and assign the file handle. Note the use of the pound sign (#) here. We also declare a large string array, which is one dimensional. We use the ReDim Preserve Method to resize the array to save some memory space and preserve the content. We use the Input Method to put each line into an array element before this is done. Note the use of EOF here. We finally loop through the array and add each array item to a listbox control. So now you should have some basic overview how you can read a file in VB6 into an array structure and loop through its content. How neat is that!

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]