.NET 1.1 ping kode:
using System;
using System.Net;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct ICMP_ECHO_REPLY
{
public uint Address;
public uint Status;
public uint RoundTripTime;
public ushort DataSize;
public ushort Reserved;
public IntPtr Data;
public IP_OPTION_INFORMATION Options;
}
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct IP_OPTION_INFORMATION
{
public byte TTL;
public byte TOS;
public byte Flags;
public byte OptionsSize;
public IntPtr OptionsData;
public int RealOptionData;
}
public class Icmp
{
public const int IP_SUCCESS = 0;
public const int IP_BUF_TOO_SMALL = 11001;
public const int IP_REQ_TIMED_OUT = 11010;
[DllImport("icmp.dll")]
public static extern IntPtr IcmpCreateFile();
[DllImport("icmp.dll")]
public static extern uint IcmpSendEcho(IntPtr icmpHandle, uint ipAddr, ref int requestData, ushort requestSize, IntPtr optionInfo, ref ICMP_ECHO_REPLY replyBuffer, uint replySize, int timeout);
[DllImport("icmp.dll")]
public static extern bool IcmpCloseHandle(IntPtr icmpHandle);
public static bool Ping(string host)
{
uint addr = BitConverter.ToUInt32(IPAddress.Parse(host).GetAddressBytes(), 0);
IntPtr h = IcmpCreateFile();
int req = 123456789;
ICMP_ECHO_REPLY rep = new ICMP_ECHO_REPLY();
uint retval = IcmpSendEcho(h, addr, ref req, 4, IntPtr.Zero, ref rep, 32, 10);
IcmpCloseHandle(h);
return (retval != 0 && rep.Status == IP_SUCCESS);
}
}
public class TestClass
{
public static void Main(string[] args)
{
Console.WriteLine("min server : " + Icmp.Ping("192.168.1.10"));
Console.WriteLine("ikke eksisterende : " + Icmp.Ping("192.168.1.25"));
Console.WriteLine("
www.google.dk (svarer) : " + Icmp.Ping("216.239.59.104"));
Console.WriteLine("
www.eksperten.dk (svarer ikke) : " + Icmp.Ping("62.199.138.148"));
}
}