| 1:2:
 3:
 4:
 5:
 6:
 7:
 8:
 9:
 10:
 11:
 12:
 13:
 14:
 15:
 16:
 17:
 18:
 19:
 20:
 21:
 22:
 23:
 24:
 25:
 26:
 27:
 28:
 29:
 30:
 31:
 32:
 33:
 34:
 35:
 36:
 37:
 38:
 39:
 40:
 41:
 42:
 43:
 44:
 45:
 46:
 47:
 48:
 49:
 50:
 51:
 52:
 53:
 54:
 55:
 56:
 57:
 58:
 59:
 60:
 61:
 62:
 63:
 64:
 65:
 66:
 67:
 68:
 69:
 70:
 71:
 72:
 73:
 74:
 75:
 76:
 77:
 78:
 79:
 80:
 81:
 82:
 83:
 84:
 85:
 86:
 87:
 88:
 89:
 90:
 91:
 92:
 93:
 94:
 95:
 96:
 97:
 98:
 99:
 100:
 101:
 102:
 103:
 104:
 105:
 106:
 107:
 108:
 109:
 110:
 111:
 112:
 113:
 114:
 115:
 116:
 117:
 118:
 
 | using System;using System.Net;
 using System.Net.Sockets;
 using System.Text;
 
 public class SimplePing
 {
 public static void Main(string[] argv)
 {
 
 int port;
 string ip;
 byte[] data = new byte[1024];
 int receive;
 Start:
 Console.WriteLine("\n");
 Console.WriteLine("Benutzereigaben");
 Console.WriteLine("═══════════════════════════");
 Console.Write("Empfänger IP:");
 ip = Convert.ToString(Console.ReadLine());
 Console.Write("Auf welchen Port:");
 port = Convert.ToInt16(Console.ReadLine());
 
 
 Socket host = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
 
 IPEndPoint iep = new IPEndPoint(IPAddress.Parse(ip), port);
 EndPoint ep = (EndPoint)iep;
 
 
 
 data = Encoding.ASCII.GetBytes("11111111111111");
 
 ICMP packet = new ICMP();
 packet.Code = 0x00;
 packet.Type = 0x00;
 packet.Message = data;
 packet.MessageSize = data.Length;
 int packetsize = packet.MessageSize + 4;
 UInt16 chcksum = packet.getChecksum();
 packet.Checksum = chcksum;
 
 host.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 10000);
 host.SendTo(packet.getBytes(), packetsize, SocketFlags.None, ep);
 
 data = new byte[1024];
 receive = host.ReceiveFrom(data, ref ep);
 
 ICMP response = new ICMP(data, receive);
 
 Console.WriteLine("\n");
 Console.WriteLine("Antwort von: {0}", ep.ToString());
 Console.WriteLine("  Type {0}", response.Type);
 Console.WriteLine("  Code: {0}", response.Code);
 int Identifier = BitConverter.ToInt16(response.Message, 0);
 int Sequence = BitConverter.ToInt16(response.Message, 2);
 Console.WriteLine("  Identifier: {0}", Identifier);
 Console.WriteLine("  Sequence: {0}", Sequence);
 Console.WriteLine("  CheckSum: {0}",chcksum);
 goto Start;
 
 host.Close();
 
 
 Console.ReadKey();
 
 
 }
 }
 
 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, 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, 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);
 }
 }
 |