Webmasters, gagnez de l'argent en affichant des bannières contextuelles Oxado
SourceForge.net Logo
Support This Project
   LeCouteauSuisse Project
Welcome ! [ Home ]  ·  [ Downloads ]  ·  [ Forums ]  ·  [ News ]  ·  [ Documentations ]  
[ Home ]

[ Documentations ]

Create the Alert tool


We must create an little tool to send alert on TCP connection to the generator MachineAlertGenerator.

  1. Create a new project in the solution
  2. Select a 'Visual C#' 'Console Application' project
  3. Give the name 'Alert' for the project and add it to Solution

  4. We add code to take 4 parameters, format and send it over a TCP connection to the generator. Open the file Program.cs and copy the code :
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    
    namespace Alert
    {
        class Program
        {
            static void Main(string[] args)
            {
                const int DEFAULT_TCP_PORT = 12121;
    
                if (args.Length < 4 || args.Length>6)
                {
                    Console.WriteLine("Syntax : Alert <Kind> <Localisation> <Product> <Quantity> [<server> [<port>]]");
                    return;
                }
                try
                {
                    string kind, localisation, product, quantity, server, message;
                    int port;
                    TcpClient client;
                    NetworkStream stream;
    
                    kind = args[0];
                    localisation = args[1];
                    product = args[2];
                    quantity = args[3];
                    if (args.Length == 5)
                    {
                        server = args[4];
                        port = DEFAULT_TCP_PORT;
                    }
                    else if (args.Length == 6)
                    {
                        server = args[4];
                        if (!int.TryParse(args[5], out port))
                        {
                            Console.WriteLine("Error port must be a number !");
                            return;
                        }
                    }
                    else
                    {
                        server = "localhost";
                        port = DEFAULT_TCP_PORT;
                    }
    
                    client = new TcpClient();
    
                    client.Connect(server, port);
                    stream = client.GetStream();
    
                    if (stream.CanWrite)
                    {
                        // a message alert is 256 bytes length
                        // structure of message alert is :
                        // [0,63] : kind
                        // [64,127] : localisation
                        // [128,191] : product
                        // [192,255] : quantity
                        message = Pad(kind) + Pad(localisation) + Pad(product) + Pad(quantity);
                        Byte[] sendBytes = ASCIIEncoding.ASCII.GetBytes(message);
                        stream.Write(sendBytes, 0, sendBytes.Length);
                        stream.Close();
                        stream.Dispose();
                    }
                    client.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error sending alert !\n" + ex.ToString());
                    return;
                }
            }
    
            static string Pad(string input)
            {
                if (input.Length > 64)
                    return input.Substring(0, 64);
    
                return input + new string(' ', 64 - input.Length);
            }
        }
    }
    


LeCouteauSuisse