答案:namespace SaurabhPing
{
using System;
using System.Net;
using System.Net.Sockets;
/// <summary>
/// The Main Ping Class
/// </summary>
class Ping
{
//Declare some Constant Variables
const int SOCKET_ERROR = -1;
const int ICMP_ECHO = 8;
/// <summary>
/// The Starting Point of the Class
/// It Takes the Hostname parameter
/// </summary>
public static void Main(string[] argv)
{
if(argv.Length==0)
{
//If user did not enter any Parameter inform him
Console.WriteLine("Usage:Ping <hostname> /r") ;
Console.WriteLine("<hostname> The name of the Host who you want to ping");
Console.WriteLine("/r Ping the host continuously") ;
}
else if(argv.Length==1)
{
//Just the hostname provided by the user
//call the method "PingHost" and pass the HostName as a parameter
PingHost(argv[0]) ;
}
else if(argv.Length==2)
{
//the user provided the hostname and the switch
if(argv[1]=="/r")
{
//loop the ping program
while(true)
{
//call the method "PingHost" and pass the HostName as a parameter
PingHost(argv[0]) ;
}
}
else
{
//if the user provided some other switch
PingHost(argv[0]) ;
}
}
else
{
//Some error occurred
Console.WriteLine("Error in Arguments") ;
}
}
/// <summary>
/// This method takes the "hostname" of the server
/// and then it ping's it and shows the response time
/// </summary>
public static void PingHost(string host)
{
//Declare the IPHostEntry
IPHostEntry serverHE, fromHE;
int nBytes = 0;
int dwStart = 0, dwStop = 0;
//Initilize a Socket of the Type ICMP
Socket socket =
new Socket(AddressFamily.AfINet, SocketType.SockRaw, ProtocolType.ProtICMP);
// Get the server endpoint
try
{
serverHE = DNS.GetHostByName(host);
}
catch(Exception)
{
Console.WriteLine("Host not found"); // fail
return ;
}
// Convert the server IP_EndPoint to an EndPoint
IPEndPoint ipepServer = new IPEndPoint(serverHE.AddressList[0], 0);
EndPoint epServer = (ipepServer);
// Set the receiving endpoint to the client machine
fromHE = DNS.GetHostByName(DNS.GetHostName());
IPEndPoint ipEndPointFrom = new IPEndPoint(fromHE.AddressList[0], 0);
EndPoint EndPointFrom = (ipEndPointFrom);
int PacketSize = 0;
IcmpPacket packet = new IcmpPacket();
// Construct the packet to send
packet.Type = ICMP_ECHO; //8
packet.SubCode = 0;
packet.CheckSum = UInt16.Parse("0");
packet.Identifier = UInt16.Parse("45");
packet.SequenceNumber = UInt16.Parse("0");
int PingData = 32; // sizeof(IcmpPacket) - 8;
packet.Data = new Byte[PingData];
//Initilize the Packet.Data
for (int i = 0; i < PingData; i++)
{
packet.Data[i] = (byte)'#';
}
//Variable to hold the total Packet size
PacketSize = PingData + 8;
Byte [] icmp_pkt_buffer = new Byte[ PacketSize ];
Int32 Index = 0;
//Call a Method Serialize which counts
//The total number of Bytes in the Packet
Index = Serialize(
packet,
icmp_pkt_buffer,
PacketSize,
PingData );
//Error in Packet Size
if( Index == -1 )
{
Console.WriteLine("Error in Making Packet");
return ;
}
// now get this critter into a UInt16 array
//Get the Half size of the Packet
Double double_length = Convert.ToDouble(Index);<
上一个:用Vb.net获得计算机名称和及所有的IP地址。
下一个:记得原来有人问过用socket实现smtp,这是我的一部分代码