酷狗歌曲带我到山顶:如何用JSP或ASP判断网络状况

来源:百度文库 编辑:高校问答 时间:2024/05/02 07:53:41
用SOCKET完成

如何在ASP中实现PING

使用WSH调用系统的Ping命令,将Ping的结果重定向到一个文本文件中去,再把文本文件显示到网页中

具体做法如下:
首先, 建一个.BAT文件(例如:myPing.BAT:),这个文件要在ASP中调用,文件代码如下:
ping -a %1 > d:\INetPub\cgi-bin\%2.txt
(%1)是将来要ping的地址, (%2)是存储ping结果的文件. 以下是ASP的代码:

<%
Set FileSys = Server.CreateObject("Scripting.FileSystemObject")
FileName = FileSys.GetTempName

Set WShShell = Server.CreateObject("WScript.Shell")

IP = "xxx.xxx.xxx.xxx" '你要ping的地址
RetCode = WShShell.Run("d:\Inetpub\cgi-bin\myPing.bat " & IP & " " & FileName, 1, True)

if RetCode = 0 Then
'没有错误
else
Response.Redirect "PingErrors.htm"
end if

Set TextFile = FileSys.OpenTextFile("d:\InetPub\cgi-bin\" & FileName & ".txt", 1)
TextBuffer = TextFile.ReadAll

For i = 1 to Len(TextBuffer)

If Mid(TextBuffer,i,1) = chr(13) Then

Response.Write("<BR>")

else

Response.Write(Mid(TextBuffer,i,1))

end if

Next

TextFile.Close

FileSys.DeleteFile "d:\Inetpub\cgi-bin\" & FileName & ".txt"

%>

asp
精确的判断网络是否可用
Socket类的Connected属性往往不能精确的判断出网络是否连接,下面这段代码可以解决这个问题

/// <summary>
/// 是否已经连接
/// </summary>
public virtual bool Connected
{
get
{
try
{
//检查socket的状态是否可读
if(m_socket.Connected && m_socket.Poll(0, SelectMode.SelectRead))
{
byte[] aByte = new byte[1];
//因为TCP/IP协议无法精确的判断网络是否可用
//试读一个字符,Peek参数指定读取的字符不会从缓冲区中移除
//假如可读则表示连接可用
if(m_socket.Receive(aByte, 0, 1, SocketFlags.Peek) != 0)
return true;
Close("Disconnected.");
return false;
}
}
catch(SocketException e)
{
OnException(e);
}
return m_socket.Connected;
}
}
================

回答者:欧阳狂野 - 魔法师 四级

不错的注意,刚刚验证过!!

高手呢?还有没有啊?