当前位置:编程学习 > C#/ASP.NET >>

一段国外的PING程序(C#)

答案:比较有用。

    //**************************************
    //     
    // Name: Ping .NET Class!
    // Description:Ping a machine from .NET.
    //     This code is CLR compliant.
    // By: Carl Mercier
    //
    // Assumes:The code is a complete consol
    //     e application which can easilly modified
    //     to become Windows Form.
    //
    //This code is copyrighted and has    // limited warranties.Please see http://
    //     www.Planet-Source-Code.com/xq/ASP/txtCod
    //     eId.335/lngWId.10/qx/vb/scripts/ShowCode
    //     .htm    //for details.    //**************************************
    //     
    
    using System;
    namespace myping
    {
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class myping
    {
    /// <summary>
    /// The main entry point for the applica
    //     tion.
    /// </summary>
    //Declare some Constant Variables
    const int SOCKET_ERROR = -1;
    const int ICMP_ECHO = 8;
    [STAThread]
    static void Main(string[] args)
    {
    //
    // TODO: Add code to start application h
    //     ere
    //
    if(args.Length==0)
    {
    //If user did not enter any Parameter in
    //     form him
    Console.WriteLine("Usage:myping <hostname> /r") ;
    Console.WriteLine("<hostname> The name of the Host who you want to ping");
    Console.WriteLine("/r Optional Switch to Ping the host continuously") ;
    }
    else if(args.Length==1)
    {
    //Just the hostname provided by the user
    //     
    //call the method "PingHost" and pass th
    //     e HostName as a parameter
    PingHost(args[0]) ;
    }
    else if(args.Length==2)
    {
    //the user provided the hostname and the
    //     switch
    if(args[1]=="/r")
    {
    //loop the ping program
    while(true)
    {
    //call the method "PingHost" and pass th
    //     e HostName as a parameter
    PingHost(args[0]) ;
    }
    }
    else
    {
    //if the user provided some other switch
    //     
    PingHost(args[0]) ;
    }
    }
    else
    {
    //Some error occured
    Console.WriteLine("Error in Arguments") ;
    }
    }
    public static void PingHost(string host)
    {
    //Declare the IPHostEntry
    System.Net.IPHostEntry serverHE, fromHE;
    int nBytes = 0;
    int dwStart = 0, dwStop = 0;
    //Initilize a Socket of the Type ICMP
    System.Net.Sockets.Socket socket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,
    System.Net.Sockets.SocketType.Raw, System.Net.Sockets.ProtocolType.Icmp);
    // Get the server endpoint
    try
    {
    serverHE = System.Net.Dns.GetHostByName(host);
    }
    catch(Exception)
    {
    Console.WriteLine("Host not found"); // fail
    return ;
    }
    // Convert the server IP_EndPoint to an
    //     EndPoint
    System.Net.IPEndPoint ipepServer = new System.Net.IPEndPoint(serverHE.AddressList[0], 0);
    System.Net.EndPoint epServer = (ipepServer);
    // Set the receiving endpoint to the cli
    //     ent machine
    fromHE = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName());
    System.Net.IPEndPoint ipEndPointFrom = new System.Net.IPEndPoint(fromHE.AddressList[0], 0);
    System.Net.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 Methos Serialize which counts
    //The total number of Bytes in the Packe
    //     t
    Index = Serialize(
    packet,
    icmp_pkt_buffer,
    PacketSize,
    PingData );
    //Err

上一个:实现一个Web Application Server(2)(zt)
下一个:Using the Remoting Callbacks in .Net Applications

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,