如何确定C#中是否连接/断开以太网电缆 [英] How to determine if an ethernet cable is connected/disconnected in C#

查看:307
本文介绍了如何确定C#中是否连接/断开以太网电缆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们想在C#应用程序中确定是否连接了以太网电缆? 

We like to determine in C# application if the ethernet cable is connected or not? 

目前我们尝试了以下代码:

At the moment we have tried the following code:

        public Client(string csIPadresse)
        {
            try
            {
                m_csIPadresse = csIPadresse;
                ipAddress = IPAddress.Parse(m_csIPadresse);
                remoteEP = new IPEndPoint(ipAddress, 20007);
            }
            catch (Exception ex)
            {
            }
        }

        public bool Connect()
        {
            bool bConnected;
            try
            {
                m_sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                m_sender.Connect(remoteEP);
                bConnected = m_sender.Connected;
                return true;
            }
            catch (Exception ex)
            {
                bConnected = m_sender.Connected;
                m_sender.Close();
                return false;
            }
        }

但m_sender.Connected始终为true,但电缆未连接。

But m_sender.Connected is always true, although the cable isn't connected.

有没有人知道这个问题的解决方案? 

Does anybody knows a solution for the problem? 

谢谢,

Andreas

推荐答案

嗨Andreas,

Hi Andreas,


这里
。希望这会有所帮助。

There is an archived post regarding the same at Here. Hope this will be helpful.


这篇关于如何确定C#中是否连接/断开以太网电缆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆