如何判断网络连接的方法集锦

在网络应用程序中经常会判断网络是否正常或者是否能够正常联机到远程服务器,就上述问题,目前提供了如下集中方法,供大家参考.
一.使用.Net 2.0提供的Ping命令
1 None.gif             AutoResetEvent waiter  =   new  AutoResetEvent( false );
2 None.gif            Ping myPing  =   new  Ping();
3 None.gif            myPing.PingCompleted  +=   new  PingCompletedEventHandler(myPing_PingCompleted);
4 None.gif             string  data  =   " OK " // Ping 发送的数据
5 None.gif              byte [] buffer  =  Encoding.ASCII.GetBytes(data);
6 None.gif             int  timeout  =   10000 ;
7 None.gif            PingOptions options  =   new  PingOptions( 64 true );
8 None.gif            myPing.SendAsync(_AppState.ServerName, timeout, buffer, options, waiter); 

二.使用本地tcp集合,此集合由操作系统层面提供,也是.Net2.0支持的
None.gif IPGlobalProperties properties  =  IPGlobalProperties.GetIPGlobalProperties();
None.gif            TcpConnectionInformation[] connections 
=  properties.GetActiveTcpConnections();
ExpandedBlockStart.gifContractedBlock.gif            
foreach  (TcpConnectionInformation info  in  connections)  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (info.RemoteEndPoint.Equals(targetEndPoint)) dot.gif{
InBlock.gif                    _AppState.OnlineStatus 
= Constant.ONLINE_STATUS_ONLINE;
InBlock.gif                    isOnline 
= true;
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

ExpandedBlockEnd.gif            }
其中在集合中遍历是否存在对应的远程主机名称和端口号,使用这个方法的前提时必须能在第一次运行的时候连接到远程服务器.否则在集合中是查找不到的.

转载于:https://www.cnblogs.com/DreamWinter/articles/308180.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值