2014-11-21 32 views
-1

我有客户端服务器应用程序(Lan base)。最初,客户端应用程序成功连接到服务器,但后来在运行时,由于某种原因(例如,服务器机器关闭),连接正在丢失。这引发了一个类似UNABLE TO CONNECT TO HOST的错误。我如何在运行时检查连接的状态?如何在运行时使用C#.net(winforms)检查连接状态

的代码如下

Connection类

using MySql.Data.MySqlClient; 
namespace Connection_Checker 
{ 
public class Connection 
{ 
    public static MySqlConnection cnn = new MySqlConnection("Server=localhost;Database=arc;User Id=root;Password=root;"); 
} 
} 

形式

private void Form1_Load(object sender, EventArgs e) 
{ 
    Connection.cnn.Open(); 
} 

//and timer 
private void timer1_Tick(object sender, EventArgs e) 
{ 
    if (Connection.cnn.State == ConnectionState.Open) 
    { 
    this.Text = "Connected"; 
    } 
    else 
    { 
    this.Text = "Disconnected"; 
    return; 
    } 
} 

我的定时器是使=真正的

+4

不要让你的连接像这样打开。打开它,获取你的数据,关闭它。你有关计时器的问题吗?它是什么显示? – LarsTech 2014-11-21 01:38:02

+0

我的目标是在运行时和连接丢失的系统会说断开连接,我实现了使用定时器。 。 。 – 2014-11-21 01:42:27

+0

只需使用Try ...如果您希望数据连接成为片状,就可以抓住数据呼叫。 – LarsTech 2014-11-21 01:43:53

回答

1

如果我是你的形式我也不会在表单加载事件中打开连接。

而不是我的方法,我会在哪里获取的数据将是这样的

using MySql.Data.MySqlClient; 
namespace Connection_Checker 
{ 
public class Connection 
{ 
    public static connectionString = "Server=localhost;Database=arc;User Id=root;Password=root;"; 
} 
} 

在形式

private void GetData() 
{ 
try{ 
    using (MySqlSqlConnection connection = new MySqlSqlConnection(Connection.connectionString)) 
    { 
     connection.Open(); 
     //GetData 
     connection.Close(); 
     } 
} 
catch 
{ 
    MessageBox.Show("An error occurred while trying to fetch Data"); 
} 
} 

private void Form1_Load(object sender, EventArgs e) 
{ 
    //I would avoid this. 
    // Connection.cnn.Open(); 
} 

//I am not sure why you are having this. 
private void timer1_Tick(object sender, EventArgs e) 
{ 
    //if (Connection.cnn.State == ConnectionState.Open) 
    //{ 
    // this.Text = "Connected"; 
    //} 
    //else 
    //{ 
    // this.Text = "Disconnected"; 
    // return; 
    //} 
} 
+0

我不会假设异常总是连接错误,除非您自己检查异常错误。 – LarsTech 2014-11-21 02:02:00

+0

是的,你是对的。我的代码只是从.. – 2014-11-21 02:05:23

+1

开始你保存了我的一天先生的影子。 。 。非常感谢 。 。我的问题解决了。 。这是我第一次问这里,只需几分钟就能解决。 .. – 2014-11-21 02:15:26

0

捕获传输错误连接失败时不会告诉你。它只会告诉你,当你尝试使用它时连接不可用。

对于按照您的示例使用计时器进行连续在线监控,请使用此扩展方法直接查询TCP连接状态。

public static class Extensions 
{ 
    /// <summary> 
    /// Obtain the current state of the socket underpinning a TcpClient. 
    /// Unlike TcpClient.Connected this is reliable and does not require 
    /// capture of an exception resulting from a failed socket write. 
    /// </summary> 
    /// <param name="tcpClient">TcpClient instance</param> 
    /// <returns>Current state of the TcpClient.Client socket</returns> 
    public static TcpState GetState(this TcpClient tcpClient) 
    { 
    var foo = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections() 
     .SingleOrDefault(x => x.LocalEndPoint.Equals(tcpClient.Client.LocalEndPoint)); 
    return foo != null ? foo.State : TcpState.Unknown; 
    } 
} 

然而,正如已经被别人的意见所提到的,你对连续连接的依赖可能反映在您的应用程序的严重设计缺陷。你应该听从你给出的建议,并重新设计你的应用程序。 Hogging连接严重限制了可扩展性,并且不会让您受到网络管理员或数据库管理员的欢迎。