Ping or, as it is more correctly known, Internet control message protocol
Size: 96.88 KB
Language: en
Added: Jul 12, 2024
Slides: 9 pages
Slide Content
PING الاعداد : الاء الطيب. هبه عباس.
Ping or, as it is more correctly known, Internet control message protocol (ICMP), is a protocol used to report broken network connections or other router-level problems that end hosts might need to know. When a router can’t get its packet to the next hop, it discards the packet and sends an ICMP packet back to the sender. ICMP packets are not used to report lost routing problems for other ICMP packets in order to prevent network cascade effects. Many developers are familiar with the ping utility, which can be used to determine if a computer is switched on or not and how much delay there is over the connection to it. This protocol can be implemented in .NET to provide applications with the ability to check quickly if a computer to which it needs to connect is turned on. Create a new project in Visual Studio .NET. Add a new module to the project, and enter the following code:
public class PING { public struct IP_OPTION_INFORMATION { public byte TTL, Tos,Flags,OptionSize ; [ MarshalAs ( UnmanagedType.ByValTStr,SizeConst =128)] public string OptionsData ; } public struct ICMP_ECHO_REPLY { public uint Address, Status, RoundTripTime ; public ushort DataSize,Reserved ; public IP_OPTION_INFORMATION Options; } [ DllImport ("icmp. dll ", SetLastError =true)] public static extern uint IcmpSendEcho ( uint IcmpHandle , uint DestAddress , string RequestData , uint RequestSize , ref IP_OPTION_INFORMATION RequestOptns , ref ICMP_ECHO_REPLY ReplyBuffer , uint ReplySize , uint TimeOut ); [ DllImport ("icmp. dll ", SetLastError =true)] public static extern uint IcmpCreateFile (); public static IP_OPTION_INFORMATION pIPo ; public static ICMP_ECHO_REPLY pIPe ; }
With nearly all API code, it is rarely necessary to understand every parameter sent to each function. IcmpCreateFile creates a handle to resources used when generating ping requests. Where a program may issue large numbers of ping requests, then IcmpCloseHandle should be used to reclaim memory. IcmpSendEcho sends an ICMP echo request to a host as specified in the DestAddress parameter. The format of the outgoing ping is set in the RequestOptns parameter, and details of the reply (or lack thereof) are stored in the ReplyBuffer . Go to the form and draw a textbox named tbIP and a button named btnPing . Click on the button and add the following code:
You may notice that the IP address is converted from a string to a Uint32 (unsigned 32-bit integer) by the ConvertIPtoLong function. This is required because the DestAddress parameter of IcmpSendEcho uses a binary representation of IP addresses. So, add in the following function to implement convertIPtoLong : public UInt32 convertIPtoLong (string ip ) { string[] digits; digits = ip.Split (".". ToCharArray ()); return Convert.ToUInt32( Convert.ToUInt32(digits[3]) * Math.Pow (2,24) + Convert.ToUInt32(digits[2]) * Math.Pow (2,16) + Convert.ToUInt32(digits[1]) * Math.Pow (2,8) + Convert.ToUInt32(digits[0])); }
This function splits an IP address into its four constituent bytes, multiplies each byte by a power of 2, and adds them together. In the case of the loop-back address 127.0.0.1, this is converted to 127 + 1 × 224, or 16,777,343. You may also notice in the code above that a message box is displayed once IcmpSendEcho returns. This message could therefore describe to the user the result of the ping request. The function describeResponse per forms the task of converting the rather cryptic response codes into meaningful phrases. Enter the following code:
public string describeResponse ( uint code) { string Rcode = ""; switch(code) { case 0 : Rcode = " Success";break ; case 11001 : Rcode = "Buffer too Small";break ; case 11002 : Rcode = " Dest Network Not Reachable";break ; case 11003 : Rcode = " Dest Host Not Reachable";break ; case 11004 : Rcode = " Dest Protocol Not Reachable";break ; case 11005 : Rcode = " Dest Port Not Reachable";break ; case 11006 : Rcode = "No Resources Available";break ; case 11007 : Rcode = "Bad Option";break ; case 11008 : Rcode = "Hardware Error";break ; case 11009 : Rcode = "Packet too Big";break ; case 11010 : Rcode = " Rqst Timed Out";break ; case 11011 : Rcode = "Bad Request";break ; case 11012 : Rcode = "Bad Route";break ; case 11013 : Rcode = "TTL Exprd in Transit";break ; case 11014 : Rcode = "TTL Exprd Reassemb ";break; case 11015 : Rcode = "Parameter Problem";break ; case 11016 : Rcode = "Source Quench";break ; case 11017 : Rcode = "Option too Big";break ; case 11018 : Rcode = " Bad Destination";break ; case 11019 : Rcode = "Address Deleted";break ; case 11020 : Rcode = "Spec MTU Change";break ; case 11021 : Rcode = "MTU Change";break ; case 11022 : Rcode = " Unload";break ; case 11050 : Rcode = "General Failure";break ; } return Rcode ; }
Many of the response codes listed would be rare and would probably indicate a programming error instead of a real network error. The most common are Success and Dest host not available. C# programmers will also require the following namespaces in both the form and class file using System.Text ; using System.Runtime.InteropServices ; To test the application, run it from Visual Studio .NET, type the IP address (not domain name!) of a well-known Web server into the box provided, and press Ping. It should respond with the message “Success” if the computer is accessible or “ Dest Host Not Reachable” if it is not. Ping can be used for more than simply checking whether a computer is switched on or not; it can also be used to trace the route of packets over the Internet. This is achieved by sending a ping request with a TTL of 1, followed by a ping with a TTL of 2, and so on. At each hop, a router will report a dead ping request and send a packet back to the original host, which will contain the IP address of the router. This technique is used by the tracert utility. In .NET v2 (Whidbey), it is possible to retrieve statistics easily relating to the number and type of pings received and sent by your computer. Please refer to the IcmpV4Statistics class, as described in Chapter 13, for more information on this topic.