C# 判断

njyang110 2013-01-17 11:40:56
winform一个程序
运行程序的电脑是双卡网 又能上外网又能上内网,
这个程序就是判断内网中另外一台电脑[特定IP地址的电脑]是否开着机,还有就是是否能上外网。
能上外网的标准是,在机房有一台服务器,能连到这个电脑。
同事说用心跳包来检测,我不知道怎么弄,请教大牛怎么写呢?
...全文
254 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
liulangdeyuyu 2013-01-25
  • 打赏
  • 举报
回复
简单啊 用ping 啊 一个是ping 内网的一台机器 一个是ping百度
玲cc 2013-01-24
  • 打赏
  • 举报
回复
开一个定时器,隔一段时间去ping你的测试机和服务机。 在System.Net.NetworkInformation Namespace里。

public static string PingHost(string host)
{
    //string to hold our return messge
    string returnMessage = string.Empty;

    //IPAddress instance for holding the returned host
    IPAddress address = GetIpFromHost(ref host);

    //set the ping options, TTL 128
    PingOptions pingOptions = new PingOptions(128, true);

    //create a new ping instance
    Ping ping = new Ping();

    //32 byte buffer (create empty)
    byte[] buffer = new byte[32];

    //first make sure we actually have an internet connection
    if (HasConnection())
    {
        //here we will ping the host 4 times (standard)
        for (int i = 0; i < 4; i++)
        {
            try
            {
                //send the ping 4 times to the host and record the returned data.
                //The Send() method expects 4 items:
                //1) The IPAddress we are pinging
                //2) The timeout value
                //3) A buffer (our byte array)
                //4) PingOptions
                PingReply pingReply = ping.Send(address, 1000, buffer, pingOptions);

                //make sure we dont have a null reply
                if (!(pingReply == null))
                {
                    switch (pingReply.Status)
                    {
                        case IPStatus.Success:
                            returnMessage = string.Format("Reply from {0}: bytes={1} time={2}ms TTL={3}", pingReply.Address, pingReply.Buffer.Length, pingReply.RoundtripTime, pingReply.Options.Ttl);
                            break;
                        case IPStatus.TimedOut:
                            returnMessage = "Connection has timed out...";
                            break;
                        default:
                            returnMessage = string.Format("Ping failed: {0}", pingReply.Status.ToString());
                            break;
                    }
                }
                else
                    returnMessage = "Connection failed for an unknown reason...";
            }
            catch (PingException ex)
            {
                returnMessage = string.Format("Connection Error: {0}", ex.Message);
            }
            catch (SocketException ex)
            {
                returnMessage = string.Format("Connection Error: {0}", ex.Message);
            }
        }
    }
    else
        returnMessage = "No Internet connection found...";

    //return the message
    return returnMessage;
}
可参见http://stackoverflow.com/questions/3689728/ping-a-hostname-on-the-network
wanghui0380 2013-01-24
  • 打赏
  • 举报
回复
这个需要在客户机上安装,而且是物理系统就内置 公安系统就有这种设备,我曾经抱了一台公安系统的机器到我公司重新装系统,都已经格式化了重装了系统,正在连我公司的网打补丁,结果5分钟不到,公安系统的人就打电话了说:“上面检测到机器非法连接外网”,我当时比较郁闷说我都格式化了你们咋知道,人家告诉我他们有专用硬件滴
njyang110 2013-01-24
  • 打赏
  • 举报
回复
自己顶一下 求人来帮
lugang386 2013-01-17
  • 打赏
  • 举报
回复
机房的服务器,和你这台电脑,建立个TCP连接,机房服务器为TCPserver ,你这台电脑为TCPClient,TCPClient 定时给 TCPserver 发送一个消息包,TCPserver 同时发回消息包一个消息包,能够互相通讯说明心跳包检测正常,反之异常,网络中断
njyang110 2013-01-17
  • 打赏
  • 举报
回复
引用 1 楼 caozhy 的回复:
为什么不把程序部署在被检测的那台机器上,让它主动给你的监控机器发送数据表明自己的状态呢?
因为局域网内那个东西可能不是电脑 比如考勤机 需要读取考勤机的考勤记录,所以要判断
threenewbee 2013-01-17
  • 打赏
  • 举报
回复
为什么不把程序部署在被检测的那台机器上,让它主动给你的监控机器发送数据表明自己的状态呢?
njyang110 2013-01-17
  • 打赏
  • 举报
回复
引用 3 楼 lugang386 的回复:
机房的服务器,和你这台电脑,建立个TCP连接,机房服务器为TCPserver ,你这台电脑为TCPClient,TCPClient 定时给 TCPserver 发送一个消息包,TCPserver 同时发回消息包一个消息包,能够互相通讯说明心跳包检测正常,反之异常,网络中断
只写一个程序 就是当前这个电脑 写什么TCP 要是能写就没这么麻烦了 只能写一个程序

110,539

社区成员

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

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

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