.aspx页:
一个textbox(txtIP)输入服务器地址,一个button(Btn_ok)点击测试,一个listbox(lboxContent)显示测试信息
.aspx.cs页:
using System.Net.NetworkInformation;
using System.Net;
using System.Text;
using System.IO;
protected void Btn_ok_Click(object sender, EventArgs e)
{
this.lboxContent.Items.Clear();
//远程服务器IP
string ipStr = txtIP.Text.ToString().Trim();
//构造Ping实例
System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping();
//Ping 选项设置
PingOptions options = new PingOptions();
options.DontFragment = true;
//测试数据
string data = "test data abcabc";
byte[] buffer = Encoding.ASCII.GetBytes(data);
//设置超时时间
int timeout = 120;
//调用同步 send 方法发送数据,将返回结果保存至PingReply实例
PingReply reply = pingSender.Send(ipStr, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
lboxContent.Items.Add("答复的主机地址:" + reply.Address.ToString());
lboxContent.Items.Add("往返时间:" + reply.RoundtripTime);
lboxContent.Items.Add("生存时间(TTL):" + reply.Options.Ttl);
lboxContent.Items.Add("是否控制数据包的分段:" + reply.Options.DontFragment);
lboxContent.Items.Add("缓冲区大小:" + reply.Buffer.Length);
}
else
lboxContent.Items.Add(reply.Status.ToString());
}