Advanced Ping Program : Ping « Network « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Class Interface
3. Collections Data Structure
4. Components
5. Data Types
6. Database ADO.net
7. Design Patterns
8. Development Class
9. Event
10. File Stream
11. Generics
12. GUI Windows Form
13. Language Basics
14. LINQ
15. Network
16. Office
17. Reflection
18. Regular Expressions
19. Security
20. Services Event
21. Thread
22. Web Services
23. Windows
24. XML
25. XML LINQ
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / C Sharp » Network » PingScreenshots 
Advanced Ping Program
Advanced Ping Program

/*
C# Network Programming 
by Richard Blum

Publisher: 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;

public class AdvPing : Form
{
   private static int pingstart, pingstop, elapsedtime;
   private static TextBox hostbox, databox;
   private static ListBox results;
   private static Thread pinger;
   private static Socket sock;

   public AdvPing()
   {
      Text = "Advanced Ping Program";
      Size = new Size(400380);

      Label label1 = new Label();
      label1.Parent = this;
      label1.Text = "Enter host to ping:";
      label1.AutoSize = true;
      label1.Location = new Point(1030);

      hostbox = new TextBox();
      hostbox.Parent = this;
      hostbox.Size = new Size(200* Font.Height);
      hostbox.Location = new Point(1055);

      results = new ListBox();
      results.Parent = this;
      results.Location = new Point(1085);
      results.Size = new Size(36018 * Font.Height);

      Label label2 = new Label();
      label2.Parent = this;
      label2.Text = "Packet data:";
      label2.AutoSize = true;
      label2.Location = new Point(10330);

      databox = new TextBox();
      databox.Parent = this;
      databox.Text = "test packet";
      databox.Size = new Size(200* Font.Height);
      databox.Location = new Point(80325);

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

      Button stopit = new Button();
      stopit.Parent = this;
      stopit.Text = "Stop";
      stopit.Location = new Point(295,52);
      stopit.Size = new Size(* Font.Height, * Font.Height);
      stopit.Click += new EventHandler(ButtonStopOnClick);

      Button closeit = new Button();
      closeit.Parent = this;
      closeit.Text = "Close";
      closeit.Location = new Point(300320);
      closeit.Size = new Size(* Font.Height, * Font.Height);
      closeit.Click += new EventHandler(ButtonCloseOnClick);

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

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

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

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

   void sendPing()
   {
      IPHostEntry iphe = Dns.Resolve(hostbox.Text);
      IPEndPoint iep = new IPEndPoint(iphe.AddressList[0]0);
      EndPoint ep = (EndPoint)iep;
      ICMP packet = new ICMP();
      int recv, i = 1;

      packet.Type = 0x08;
      packet.Code = 0x00;
      Buffer.BlockCopy(BitConverter.GetBytes(1)0, packet.Message, 02);
      byte[] data = Encoding.ASCII.GetBytes(databox.Text);
      Buffer.BlockCopy(data, 0, packet.Message, 4, data.Length);
      packet.MessageSize = data.Length + 4;
      int packetsize = packet.MessageSize + 4;

      results.Items.Add("Pinging " + hostbox.Text);
      while(true)
      {
         packet.Checksum = 0;
         Buffer.BlockCopy(BitConverter.GetBytes(i)0, packet.Message, 22);
         UInt16 chcksum = packet.getChecksum();
         packet.Checksum = chcksum;

         pingstart = Environment.TickCount;
         sock.SendTo(packet.getBytes(), packetsize, SocketFlags.None, iep);
         try
         {
            data = new byte[1024];
            recv = sock.ReceiveFrom(data, ref ep);
            pingstop = Environment.TickCount;
            elapsedtime = pingstop - pingstart;
            results.Items.Add("reply from: " + ep.ToString() ", seq: " + i +
                      ", time = " + elapsedtime + "ms");
         catch (SocketException)
         {
            results.Items.Add("no reply from host");
         }
         i++;
         Thread.Sleep(3000);
      }
   }

   public static void Main()
   {
      Application.Run(new AdvPing());
   }
}
class ICMP
{
   public byte Type;
   public byte Code;
   public UInt16 Checksum;
   public int MessageSize;
   public byte[] Message = new byte[1024];

   public ICMP()
   {
   }

   public ICMP(byte[] data, int size)
   {
      Type = data[20];
      Code = data[21];
      Checksum = BitConverter.ToUInt16(data, 22);
      MessageSize = size - 24;
      Buffer.BlockCopy(data, 24, Message, 0, MessageSize);
   }

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

   public UInt16 getChecksum()
   {
      UInt32 chcksm = 0;
      byte[] data = getBytes();
      int packetsize = MessageSize + 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);
   }
}


           
       
Related examples in the same category
1. Success
2. Ping and PingReply
3. Ping Success and Send
4. Simple Ping
w__w___w_.j__a___v___a___2s.___c___om__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.