c#检测网络连接(主要是局域网)

c#检测网络连接问题我没有看到好的方法,都是通过与外网(或者局域网服务器)传递信息检测的。

我看些下下来了

代码:

   private void button1_Click(object sender, EventArgs e)
        {
            string ip;
            ip = "10.1.148.1";
           // string ip = "192.192.132.229";

          //  string strRst = CmdPing(ip);

         //   MessageBox.Show(strRst);
              string   str   =   CmdPingh(ip);
             MessageBox.Show(str);  

        }
        private static string CmdPing(string strIp)//方法1

{

Process p = new Process();

p.StartInfo.FileName = "cmd.exe";

p.StartInfo.UseShellExecute = false;

p.StartInfo.RedirectStandardInput = true;

p.StartInfo.RedirectStandardOutput = true;

p.StartInfo.RedirectStandardError = true;

p.StartInfo.CreateNoWindow = true;

string pingrst;

p.Start();

p.StandardInput.WriteLine("ping -n 1 "+strIp);

p.StandardInput.WriteLine("exit");

string strRst = p.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;

p.Close();

return pingrst;

}
 public   static   string   CmdPingh(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 button2_Click(object sender, EventArgs e)
 {
     jcip();
 }  

private void jcip()//方法2
{
    Ping pingSender = new Ping ();
            PingOptions options = new PingOptions ();

            // Use the default Ttl value which is 128,
            // but change the fragmentation behavior.
            options.DontFragment = true;

            // Create a buffer of 32 bytes of data to be transmitted.
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes (data);
            int timeout = 120;
            PingReply reply = pingSender.Send ("10.1.148.1", timeout, buffer, options);
            if (reply.Status == IPStatus.Success)
            {
      & 

c#检测网络连接(主要是局域网)

时间: 2024-08-19 12:20:13

c#检测网络连接(主要是局域网)的相关文章

使用java检测网络连接状况

windows中可以通过在cmd中使用ping命令来检测网络连接状况,如下: 网络连接正常时: 网络未连接时: 在java中可以通过调用ping命令来判断网络是否连接正常: package module.system.common; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; /** * 判断网络连接

android检测网络连接状态示例讲解

网络的时候,并不是每次都能连接到网络,因此在程序启动中需要对网络的状态进行判断,如果没有网络则提醒用户进行设置 Android连接首先,要判断网络状态,需要有相应的权限,下面为权限代码(AndroidManifest.xml): 复制代码 代码如下: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><uses-permission android:name="a

iOS使用Reachability实时检测网络连接状况

//在程序的启动处,开启通知 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //..... //开启网络状况的监听 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name: kR

检测网络连接状态

#import "AFNetworkReachabilityManager.h" - (BOOL)isNetWorkReachable{ __block CGRect rect = _nonNetworkLabel.frame; //这里的声明前面加__block,作用是在块里可以修改rect的值: AFNetworkReachabilityManager *afNetworkReachabilityManager = [AFNetworkReachabilityManager sha

IOS检测网络连接状态(转)

IOS检测网络连接状态(转) 使用之前请从Apple网站下载示例:点此下载 然后将Reachability.h 和 Reachability.m 加到自己的项目中,并引用 SystemConfiguration.framework,就可以使用了. Reachability 中定义了3种网络状态: // the network state of the device for Reachability 1.5. typedef enum { NotReachable = 0, //无连接 Reach

Android 检测网络连接状态

Android APP需要连接网络的时候,并不是每次都能连接到网络,因此需要在程序对当前设备的网络状态进行检测,以便及时对用户进行提醒. 判断网络状态,需要有相应的权限,权限代码如下(AndroidManifest.xml): <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.perm

MFC检测网络连接和ping IP地址

/****************************************************************** * 函数介绍:运行程序 * 输入参数: * 输出参数: * 返回值 : *******************************************************************/ DWORD CCommonFun::WinExecAndWait32(LPCTSTR lpszAppPath, LPCTSTR lpParameters,

常用三方,Reachability 检测网络连接

常用三方 Reachability 检 测网络连接 用来检查网络连接是否可用:包括WIFI和 WWAN(3G/EDGE/CDMA等)两种工作模式. 可以从Apple网站下载到: http://developer.apple.com/library/ios/#samplecode/Reachab ility/History/History.html#//apple_ref/doc/uid/DTS40007324-R evisionHistory-DontLinkElementID_1. 现在有更好

检测网络连接状态_转

1. 根据ifconfig中相应网口的RUNNING字段检查 int check_net(const char *eth) { int ret = 1; char buf[256]; FILE *fp; memset(buf, 0, 256); sprintf(buf, "ifconfig %s | grep 'RUNNING'", eth); fp = popen(buf, "r"); if(fp == NULL) { kprintf(KRELEASE, E_DU