当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > Asp.net编写的PING工具

ASP.NET
asp.net 使用Silverlight操作ASPNETDB数据库
ASP.NET 前后台调用方法
ASP.NET中等安全模式的一些经验分享
asp.net 打印控件使用方法
网站开发技术:ASP.NET 2.0搭建网站
ASP.NET实例教程:创建数据透视表
ASP.NET 4进行SEO优化提高网站排名和权重
ASP.NET实例教程:订阅 GeoRSS 订阅源
ASP.NET页面间数据传递的9种方法
ASP.NET教程:网页表单多个按钮完成不同功能
Asp.net的服务器推技术 (Server Push)
asp.net 无刷新附件上传实现方法
ASP.NET 定制简单的错误处理页面实现代码
c# 在WebBrowser中用SendMessage模拟鼠标点击
asp.net 表单验证新思路
.NET从优酷专辑中采集所有视频及信息(VB.NET代码)
VS2005 水晶报表在时部署时遇到的问题
ASP.NET 输出图片简单代码
一天精通asp.net的学习经验小结
DataGridView中绑定DataTable数据及相关操作实现代码

ASP.NET 中的 Asp.net编写的PING工具


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 43 ::
收藏到网摘: n/a

导 读:PING 是一个用来检测网络连接速度的使用工具,下面的文章将介绍在C#中利用System.Net.Sockets 来创建一个自己的PING 工具。

--------------------------------------------------------------------------------

PING 是一个用来检测网络连接速度的工具,它会在本机和给出的远程主机名之间建立一个SOCKET 连接并向其发送一个ICMP协议格式的数据包,然后远程主机作出响应,发回一个数据包,通过计算发送到接收数据包的时间间隔,我们可以确定连接的速度。

使用方法 ping <hostname> [/r]

<hostname> 主机名

[/r] 可选属性,决定是否连续的 ping 远程主机。

下面是代码:

///ping.cs

namespace SaurabhPing

{

using System;

using System.Net;

using System.Net.Sockets;

/// <summary>

/// 主要的类:ping

/// </summary>

class Ping

{

//声明几个常量

const int SOCKET_ERROR = -1;

const int ICMP_ECHO = 8;

/// <summary>

/// 这里取得Hostname参数

/// </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=="/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>

/// 主要的方法,用来取得IP,

/// 并计算响应时间

/// </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,

icm