从多个配置服务器列表中尝试连接web服务

从多个配置服务器列表中尝试连接
其实比较简单,主要有两个比较重要的变量,一个是服务器的索引,一个是尝试连接次数,当然,这些都是可以自己自由设置的。
下面是我写的一段列程,是我以前一个项目里涉及到的,我精简了一下,和大家探讨研究


None.gif public   bool  Conn( ref  McServices.Services ms)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//ConnCount  尝试连接次数
InBlock.gif            
//ServerIndex  配置的服务器索引
InBlock.gif
            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Threading.Thread.Sleep(
3000);//堵塞3秒
InBlock.gif
                ms = new McServices.Services();
InBlock.gif                ms.Url 
= ConfigurationSettings.AppSettings["Server"+ServerIndex];
InBlock.gif                
if(ms.CheckServices())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ConnCount 
= 0;
InBlock.gif                    ServerIndex 
= 1;
InBlock.gif                    Console.WriteLine(ms.Url 
+ "连接成功!");
InBlock.gif
InBlock.gif                    
return true;
ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception e)//出现连接失败,则抛出异常
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                
if(ConnCount>=3)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ConnCount
=0;
InBlock.gif                    
if(ServerIndex>3)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Console.WriteLine(
"所有服务器尝试连接失败!");  
InBlock.gif                        
return false
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{ServerIndex ++;}
InBlock.gif
InBlock.gif        
InBlock.gif                    
return Conn(ref ms);
InBlock.gif                    
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Console.WriteLine( ms.Url 
+ "尝试连接失败" + (ConnCount+1+ "次,等待3秒以后尝试连接!");
InBlock.gif
InBlock.gif                    ConnCount 
++;
InBlock.gif                    
return Conn(ref ms);
InBlock.gif                    
ExpandedSubBlockEnd.gif                }

InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return false;
ExpandedBlockEnd.gif        }

当然,其实还可以加入后台线程,作为检测是否吊线啊,正在连接啊,连接成功之类的检测功能,这些就大家自由发挥了

转载于:https://www.cnblogs.com/KeithDan/archive/2006/07/18/454167.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值