Exemple d’un programme de Ping

Author:

Exemple,de,Connexion,à,un,Serveur,Mail
{filelink=17095}

/*
 * Auteur Original
C# Network Programming
par Richard Blum

Publié par: Sybex
ISBN: 0782141765
*/
using System;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

class ICMP
{
   public byte Type;
   public byte Code;
   public UInt16 Checksum;
   public int tailleMessage;
   public byte[] Message = new byte[1024];

   public ICMP()
   {
   }

   public ICMP(byte[] données, int taille)
   {
      Type = données[20];
      Code = données[21];
      Checksum = BitConverter.ToUInt16(données, 22);
      tailleMessage = taille - 24;
      Buffer.BlockCopy(données, 24, Message, 0, tailleMessage);
   }

   public byte[] getBytes()
   {
      byte[] data = new byte[tailleMessage + 9];
      Buffer.BlockCopy(BitConverter.GetBytes(Type), 0, data, 0, 1);
      Buffer.BlockCopy(BitConverter.GetBytes(Code), 0, data, 1, 1);
      Buffer.BlockCopy(BitConverter.GetBytes(Checksum), 0, data, 2, 2);
      Buffer.BlockCopy(Message, 0, data, 4, tailleMessage);
      return data;
   }

   public UInt16 getChecksum()
   {
      UInt32 chcksm = 0;
      byte[] data = getBytes();
      int packetsize = tailleMessage + 8;
      int index = 0;

      while ( index < packetsize)
      {
         chcksm += Convert.ToUInt32(BitConverter.ToUInt16(data, index));
         index += 2;
      }
      chcksm = (chcksm >> 16) + (chcksm & 0xffff);
      chcksm += (chcksm >> 16);
      return (UInt16)(~chcksm);
   }
}

public class ProgrammePing : Form
{
   private static int debutPing, ArretPing, duree;
   private static TextBox txtHote, txtPaquet;
   private static ListBox resultats;
   private static Thread pinger;
   private static Socket socket;

   public ProgrammePing()
   {
      Text = "Programme de ping";
      Size = new Size(400, 380);

      Label label1 = new Label();
      label1.Parent = this;
      label1.Text = "IP/Nom de Hôte à pinguer:";
      label1.AutoSize = true;
      label1.Location = new Point(10, 30);

      txtHote = new TextBox();
      txtHote.Parent = this;
      txtHote.Size = new Size(200, 2 * Font.Height);
      txtHote.Location = new Point(10, 55);

      resultats = new ListBox();
      resultats.Parent = this;
      resultats.Location = new Point(10, 85);
      resultats.Size = new Size(360, 18 * Font.Height);

      Label label2 = new Label();
      label2.Parent = this;
      label2.Text = "Le paquet";
      label2.AutoSize = true;
      label2.Location = new Point(10, 330);

      txtPaquet = new TextBox();
      txtPaquet.Parent = this;
      txtPaquet.Text = "Teste de paquet";
      txtPaquet.Size = new Size(200, 2 * Font.Height);
      txtPaquet.Location = new Point(80, 325);

      Button sendit = new Button();
      sendit.Parent = this;
      sendit.Text = "Lancer le Ping";
      sendit.Location = new Point(220,52);
      sendit.Size = new Size(90, 2 * Font.Height);
      sendit.Click += new EventHandler(ButtonSendOnClick);

      Button btnStop = new Button();
      btnStop.Parent = this;
      btnStop.Text = "Arrêter";
      btnStop.Location = new Point(295,52);
      btnStop.Size = new Size(5 * Font.Height, 2 * Font.Height);
      btnStop.Click += new EventHandler(ButtonStopOnClick);

      Button btnFermer = new Button();
      btnFermer.Parent = this;
      btnFermer.Text = "Fermer";
      btnFermer.Location = new Point(300, 320);
      btnFermer.Size = new Size(5 * Font.Height, 2 * Font.Height);
      btnFermer.Click += new EventHandler(ButtonCloseOnClick);

      socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
      socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000);
   }

   void ButtonSendOnClick(object obj, EventArgs ea)
   {
      pinger = new Thread(new ThreadStart(EnvoyerPing));
      pinger.IsBackground = true;
      pinger.Start();
   }

   void ButtonStopOnClick(object obj, EventArgs ea)
   {
      pinger.Abort();
   }

   void ButtonCloseOnClick(object obj, EventArgs ea)
   {
      socket.Close();
      Close();
   }

   void EnvoyerPing()
   {
      IPHostEntry iphe = Dns.GetHostEntry(txtHote.Text);
      IPEndPoint iep = new IPEndPoint(iphe.AddressList[0], 0);
      EndPoint ep = (EndPoint)iep;
      ICMP paquet = new ICMP();
      int recv, i = 1;

      paquet.Type = 0x08;
      paquet.Code = 0x00;
      Buffer.BlockCopy(BitConverter.GetBytes(1), 0, paquet.Message, 0, 2);
      byte[] data = Encoding.ASCII.GetBytes(txtPaquet.Text);
      Buffer.BlockCopy(data, 0, paquet.Message, 4, data.Length);
      paquet.tailleMessage = data.Length + 4;
      int packetsize = paquet.tailleMessage + 4;

      resultats.Items.Add("Ping de l'Hôte: " + txtHote.Text);
      while(true)
      {
         paquet.Checksum = 0;
         Buffer.BlockCopy(BitConverter.GetBytes(i), 0, paquet.Message, 2, 2);
         UInt16 chcksum = paquet.getChecksum();
         paquet.Checksum = chcksum;

         debutPing = Environment.TickCount;
         socket.SendTo(paquet.getBytes(), packetsize, SocketFlags.None, iep);
         try
         {
            data = new byte[1024];
            recv = socket.ReceiveFrom(data, ref ep);
            ArretPing = Environment.TickCount;
            duree = ArretPing - debutPing;
            resultats.Items.Add("Réponse de l'Hôte: " + ep.ToString() + ", seq: " + i +
                      ", time = " + duree + "ms");
         } catch (SocketException)
         {
            resultats.Items.Add("Aucune Réponse de la part de hôte");
         }
         i++;
         Thread.Sleep(3000);
      }
   }

   public static void Main()
   {
      Application.Run(new ProgrammePing());
   }
}

Leave a Reply

Your email address will not be published. Required fields are marked *