c#如何检查局域网内服务器是否连通

netboygg 2006-01-19 09:04:17
可用ping方法吧?
但是主机设置防ping之后该怎么测试连通?
...全文
275 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
superfishmanweb 2006-01-23
  • 打赏
  • 举报
回复
用 udp 做广播, try 出错的话就是没有连通
netboygg 2006-01-21
  • 打赏
  • 举报
回复
2种都可行哈 不过还是第一种简便些
singlepine 2006-01-21
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/4528/4528575.xml?temp=.1684992
showmetoyou 2006-01-20
  • 打赏
  • 举报
回复
学习!
Snowdust 2006-01-19
  • 打赏
  • 举报
回复
using System.Diagnostics;
public static string CmdPing(string _strHost)
{
string m_strHost = _strHost;

Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
string pingrst = string.Empty;
process.StartInfo.Arguments = "ping " + m_strHost + " -n 1";
process.Start();
process.StandardInput.AutoFlush = true;
string temp = "ping " + m_strHost + " -n 1" ;
process.StandardInput.WriteLine(temp);
process.StandardInput.WriteLine("exit");
string strRst = process.StandardOutput.ReadToEnd();
if(strRst.IndexOf("(0% loss)")!=-1)
pingrst = "连接";
else if( strRst.IndexOf("Destination host unreachable.")!=-1)
pingrst = "无法到达目的主机";
else if(strRst.IndexOf("Request timed out.")!=-1)
pingrst = "超时";
else if(strRst.IndexOf("Unknown host")!=-1)
pingrst = "无法解析主机";
else
pingrst = strRst;
process.Close();
return pingrst ;
}

private void button1_Click(object sender, System.EventArgs e)
{
string str = CmdPing("192.168.1.100");
MessageBox.Show(str);
}
张赐 2006-01-19
  • 打赏
  • 举报
回复
直接连接服务器啊
可以在连接语句上try一下,抱错的话就连接失败啊,然后return,什么都不做了

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧