Ping.aspx

<%@ Page Language="C#" Buffer="false" %>
<%@ Import Namespace="System.Net.NetworkInformation" %>

<script type="text/C#" runat="server">
        
public static string Ping(string address)
{
    Ping pingSender 
= new Ping();
    PingReply reply 
= null;
    reply 
= pingSender.Send(address);
    
if (reply.Status != IPStatus.Success)
        
return reply.Status.ToString();
    
string result =  String.Format("Reply from {0}: bytes={1} time={2}ms TTL={3}",
        reply.Address,
        reply.Buffer.Length,
        reply.RoundtripTime,
        reply.Options.Ttl);
    
return result;
}

protected void Page_Load(object sender, EventArgs e)
{
    Server.ScriptTimeout 
= 300;
    Response.Write(
        String.Format(
@"
<html>
<head>
    <style type=""text/css"">
        body {{ font-size: 14px; font-family: verdana; }}
    </style>    
</head>
<body>
<pcolor: red;"">点浏览器的停止按钮停止</p>
<form action=""ping.aspx"">
    <input type=""text"" name=""ip"" value=""{0}"" />
    <input type=""submit"" value=""ping"" />
    <input type=""checkbox"" id=""chk"" checked />自动滚到最底    
</form>
", (Request["ip"]==null?"127.0.0.1":Request["ip"])
        )
    );

    Response.Write(
"<script type='text/javascript'>function setScroll(){ if (document.getElementById('chk').checked) document.getElementById('result').scrollTop = document.getElementById('result').clientHeight+document.getElementById('result').scrollHeight;}; window.setInterval(setScroll, 50);<" + "/script>");
    Response.Write(
"<div id='a'></div><textarea id='result' style='width: 100%; height: 90%;'>");



    
if (Request["ip"== null)
        
return;
    
while (true)
    {
        
if (!Response.IsClientConnected)
            
break;
        System.Threading.Thread.Sleep(
200);
        
try
        {
            Response.Write(Ping(Request[
"ip"]) + "\r\n");
        }
        
catch (Exception ex)
        {
            Response.Write(ex.Message 
+ "\r\n");
        }
        Response.Flush();
    }

    Response.Write(
"</textarea></body></html>");
}
</script>
posted @ 2008-08-12 14:28  王其荣的博客  阅读(255)  评论(0编辑  收藏  举报