sskke 发表于 2005-4-14 15:37

Asp.net编写的PING工具

<P>    Asp.net编写的PING工具
     
     
    PING 是一个用来检测网络连接速度的工具,它会在本机和给出的远程主机名之间建立一个SOCKET 连接并向其发送一个ICMP协议格式的数据包,然后远程主机作出响应,发回一个数据包,通过计算发送到接收数据包的时间间隔,我们可以确定连接的速度。
     
     
     
     此程序可以使用于 asp+asp.net 虚拟主机,时代互联(<a href="http://www.now.net.cn" target="_blank" >www.now.net.cn</A>)的虚拟主机可以支持 。虚拟主机的其他的配置问题,可以见:<a href="http://www.now.net.cn/host" target="_blank" >www.now.net.cn/host</A>
     主机+域名+邮箱 的优惠套餐,可以见 : <a href="http://www.now.net.cn/special/host.net" target="_blank" >http://www.now.net.cn/special/host.net</A>
     
     
     
    使用方法 ping &lt;hostname&gt;
     
    &lt;hostname&gt; 主机名
     
    可选属性,决定是否连续的 ping 远程主机。
     
     
    下面是代码:
     
    ///ping.cs
     
    namespace SaurabhPing
     
    {
     
    using System;
     
    using System.Net;
     
    using System.Net.Sockets;
     
    /// &lt;summary&gt;
     
    /// 主要的类:ping
     
    /// &lt;/summary&gt;
     
    class Ping
     
    {
     
    //声明几个常量
     
    const int SOCKET_ERROR = -1;
     
    const int ICMP_ECHO = 8;
     
    /// &lt;summary&gt;
     
    /// 这里取得Hostname参数
     
    /// &lt;/summary&gt;
     
    public static void Main(string[] argv)
     
    {
     
    if(argv.Length==0)
     
    {
     
    //If user did not enter any Parameter inform him
     
    Console.WriteLine("Usage:Ping &lt;hostname&gt; /r") ;
     
    Console.WriteLine("&lt;hostname&gt; 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) ;
     
    }
     
    else if(argv.Length==2)
     
    {
     
    //the user provided the hostname and the switch
     
    if(argv=="/r")
     
    {
     
    //loop the ping program
     
    while(true)
     
    {
     
    //call the method "PingHost" and pass the HostName as a parameter
     
    PingHost(argv) ;
     
    }
     
    }
     
    else
     
    {
     
    //if the user provided some other switch
     
    PingHost(argv) ;
     
    }
     
    }
     
    else
     
    {
     
    //Some error occurred
     
    Console.WriteLine("Error in Arguments") ;
     
    }
     
    }
     
     
    /// &lt;summary&gt;
     
    /// 主要的方法,用来取得IP,
     
    /// 并计算响应时间
     
    /// &lt;/summary&gt;
     
    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);
     
    EndPoint epServer = (ipepServer);
     
     
    // Set the receiving endpoint to the client machine
     
    fromHE = DNS.GetHostByName(DNS.GetHostName());
     
    IPEndPoint ipEndPointFrom = new IPEndPoint(fromHE.AddressList, 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;
     
    //Initilize the Packet.Data
     
    for (int i = 0; i &lt; PingData; i++)
     
    {
     
    packet.Data = (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);
     
    Double dtemp = Math.Ceil( double_length / 2);
     
    int cksum_buffer_length = Convert.ToInt32(dtemp);
     
    //Create a Byte Array
     
    UInt16 [] cksum_buffer = new UInt16;
     
    //Code to initialize the Uint16 array
     
    int icmp_header_buffer_index = 0;
     
    for( int i = 0; i &lt; cksum_buffer_length; i++ ) {
     
    cksum_buffer =
     
    BitConverter.ToUInt16(icmp_pkt_buffer,icmp_header_buffer_index);
     
    icmp_header
     
    以上代码可以直接在时代互联的ASP+ASP.NET 虚拟主机上直接运行,欢迎试用 !到<a href="http://www.now.net.cn/host" target="_blank" >www.now.net.cn/host</A> 订购后可试用 !
     
     关于ASP+ASP.NET高速虚拟主机,也有同时支持JSP(JAVA) 和PHP的综合虚拟主机。也可以试用,其他问题可以有免费网络电话咨询,
     欢迎随时联系 <a href="http://www.now.net.cn/customer/moreline.net" target="_blank" >http://www.now.net.cn/customer/moreline.net</A> </P>
<P>时代互联(<a href="http://www.now.net.cn" target="_blank" >www.now.net.cn</A>) 的虚拟主机,免费配有 CDN 镜象网络加速 ,送域名 邮箱,流量访问系统,留言版,计数器!
     
    虚拟主机在线订购试用,请到: <a href="http://www.now.net.cn/host" target="_blank" >http://www.now.net.cn/host</A>
    域名+空间的优惠套餐可见 : <a href="http://www.now.net.cn/special/" target="_blank" >http://www.now.net.cn/special/</A>
     
    如有问题要咨询,时代互联提供免费咨询电话,点击这里: <a href="http://www.now.net.cn/customer/moreline.net" target="_blank" >http://www.now.net.cn/customer/moreline.net</A>
    QQ 联系 :405881840 108786612 </P>
<P>----------------------------------------------------------------------------------------------------------------------------</P>
<P>
       主机租用,折上有礼,租用9折优惠加送价值3600的CDN 主机加速器 !详情见: <a href="http://www.todayidc.com" target="_blank" >http://www.todayidc.com</A>
     
    企业邮局—— 立企业的统一形象,自由增加帐号 划分大小,不限制附件大小,繁、简、英3种界面智能显示。强大Anti-SPAM反垃圾邮件使用专业杀 引擎Anti-VIRUS,在线实时自动杀  ,价 超低! <a href="http://www.now.net.cn/email/" target="_blank" >http://www.now.net.cn/email/</A>
     
    智能建站——3分钟就建立了数据库功能的网络交易平台,只要填写公司的资料或图片,就可建成网站。免费试用到 : <a href="http://www.now.net.cn/auto/" target="_blank" >http://www.now.net.cn/auto/</A>
     
    繁简通—— 300 元就给你一个完全同 的繁体网站,方便开拓 外市场!免费试用到: <a href="http://www.now.net.cn/fjt/" target="_blank" >http://www.now.net.cn/fjt/</A>
     
    动态域名——380元 就可以在公司里建立一个可以固定访问的服务器,可以应用于保密的资料服务器,视频服务器,网站服务器,邮件服务器,免费试用到: <a href="http://www.now.net.cn/cyberip/" target="_blank" >http://www.now.net.cn/cyberip/</A>  </P>
页: [1]
查看完整版本: Asp.net编写的PING工具