使用UDP协议有没有什么办法判断远程主机没有开启呢?

解决方案 »

  1.   

    UDP端口扫描【C#】
    http://hi.baidu.com/aeok/blog/item/9d0abf4c3cd916ffd62afcb4.html
      

  2.   

    UdpClient u = new UdpClient();
    IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"),7000);
    u.Connect(iep);
    byte[] b = Encoding.UTF8.GetBytes("Hello");
    u.Send(b, b.Length);
    IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0);
    while (true)
    {
    try
    {               
        u.Receive(ref ep);
    }
    catch (Exception e)
    {
     Console.WriteLine(e.Message);
     break;
    }
    }
    .......
    似乎楼上的那个方法还是有问题,我的代码如上:服务器不开,运行起来就报错,说:远程主机强迫关闭一个连接,然后程序终止了。这样不是很好,想即知道远程未开,程序又不终止的,不知有没有什么办法?