The PING console program under. Although it is simple, it can be seen that .NET is powerful!
The following is the program source code, I hope everyone will follow the post, discuss improvement, grow together! Namespace cornfield {using system.net; using system.net.sockets;
Class myping {
Public static void main (string [] argv) {if (argv.Length == 1 || argv.Length == 2) pinghost (argv); else {console.writeline ("Invalid Command."); console.writeline "USAGE: 1. myping
IPHostEntry clientInfo; try {if (hostclient.Length == 2) clientInfo = Dns.GetHostByName (hostclient [1]); else clientInfo = Dns.GetHostByName (Dns.GetHostName ());} catch (Exception) {file: // Analysis of the hostname error. Console.writeline ("Unknown Client {1}.", HostClient [1]); Return;} // Take client No. 0 port file: // *************** *************** Note *************************** File: / This can of course be set to the IP of the local or other host, and the message will return to the IP port, file: // actually this is one of the means of hackers, that is, using the packet agreement to attack the remote host. File: // Take advantage of such principles, plus multiple unlimited packets, remote hosts, file: // is not responsible for using this program for illegal attack activities, the author is not responsible !!!! !!!!!!!!!!!! File: // *************************************************** ******************************** * Endpoint ClientPoint = (endpoint) New IpendPoint (ClientInfo.addressList [0], 0) File: // Set ICMP Packet INT Datasize = 32; // ICMP Packet Size; INT Packetsize = DataSize 8; // Total Packet Length const IC ICMPPACKET PACKET = New ICMPCKET (ICMP_echo, 0, 0, 45, 0, DataSize; byte [] buffer = new byte [packetsize]; int index = packet.converttobyte (buffer); file: // packet error IF (index! = Packetsize) {console.writeline There is something wrong with packet data! "); Return;}
INT CKSUM_BUFFER_LENGTH = (int) Math.ceiling ((Double) INDEX) / 2);
UINT16 [] CKSUM_BUFFER = New uint16 [cksum_buffer_length];
int icmp_header_buffer_index = 0; for (int i = 0; i File: // Save the check and saved to packets packet.checksum = icmppacket.sumofcheck (cksum_buffer); // After saving the checksum, turn the packet into a packet byte [] Senddata = new byte [packetsize] Index = packet.convertTobyte (Senddata); File: // Packet error IF (index! = Packetsize) {console.writeLine ("there is something wrong with packet data!"); Return;} int nbytes = 0; file : // Start Int StartTime = Environment.tickCount; file: file: // Send Packet IF ((Nbytes = Socket.sendto) == -1) {Console .Writeline ("Socket Can Not Send Packet"; Byte [] receivedata = new byte [256]; file: // Receive data nbytes = 0; INT TIMEOUT = 0; int Timeconsume = 0; While (true) {nbytes = socket.ReceiveFrom (ReceiveData, 256, SocketFlags.none, Ref ClientPoint); if (nbytes == -1) {Console.Writeline ("Host Not Responding"); Break;} Else IF > 0) {timeConsume = System.Environment.TickCount - startTime; Console.WriteLine ( "Reply from" hostPoint.ToString () "in" timeConsume "MS: Bytes Received" nBytes); break;} timeConsume = Environment .Tickcount - startTime; if (Timeout> 1000) {Console.writeline ("time out"); break;}} file: // Close socket socket.close ();}} public class IcmpPacket {private Byte _type; // packet type private Byte _subCode; // the code word type private UInt16 _checkSum; // packet checksum private UInt16 _identifier; // identifier private UInt16 _sequenceNumber; // private serial number BYTE [] _DATA; file: // data packet /// ******************************************* ********************** /// Initialization message /// ****************** ************************************************** Public ICMPACKET (byte type, byte subcode, UInt16 checkSum, UInt16 identifier, UInt16 sequenceNumber, int dataSize) {_type = type; _subCode = subCode; _checkSum = checkSum; _identifier = identifier; _sequenceNumber = sequenceNumber; _data = new Byte [dataSize]; for (int i = 0; i < DataSize; i ) {_data [i] = (byte) '#';}} public uint16 checksum {get {return_checksum;} set {_checksum = value;} } /// *************************************************************** ************** / / Translate the entire ICMP message and data to BYTE packet /// **************** **************************************************** Public Int ConvertTobyte (Byte [] Buffer { Byte [] b_type = new Byte [1] {_ type}; Byte [] b_code = new Byte [1] {_ subCode}; Byte [] b_cksum = BitConverter.GetBytes (_checkSum); Byte [] b_id = BitConverter.GetBytes (_identifier ); Byte [] b_seq = bitconverter.getbytes (_sequencenumber); int i = 0; Array.copy (B_TYPE, 0, Buffer, I, B_TYPE.LENGTH); I = B_TYPE.LENGTH; Array.copy (B_CODE, 0, Buffer, I, B_Code.Length); i = b_code.length; array.copy B_cksum, 0, buffer, i, b_cksum.lendth; i = b_cksum.length; Array.copy (B_ID, 0, Buffer, I, B_ID.LENGTH); i = B_ID.LENGTH; Array.copy (B_SEQ, 0, Buffer, I, B_SEQ.LENGTH); i = B_SEQ.LENGTH; Array.copy (_Data, 0, buffer, i, _data.length); i = _data.length; Return I; } Public static uint16 sumofcheck (uint16 [] buffer {int CKSUM = 0; for (int i = 0; i CKSUM = (CKSUM >> 16) (CKSUM & 0xFFF); CKSUM = (CKSUM >> 16); return (uint16) (~ cksum); } }