如何使用C#.net(winforms)在运行时检查连接状态
我有客户机-服务器应用程序(局域网基础)。最初,客户端应用程序成功连接到服务器,但在运行时,由于某种原因(例如服务器计算机关闭),连接正在丢失。这会触发一个错误,例如如何使用C#.net(winforms)在运行时检查连接状态,c#,winforms,C#,Winforms,我有客户机-服务器应用程序(局域网基础)。最初,客户端应用程序成功连接到服务器,但在运行时,由于某种原因(例如服务器计算机关闭),连接正在丢失。这会触发一个错误,例如无法连接到主机。如何在运行时检查连接状态 代码如下 连接类 using MySql.Data.MySqlClient; namespace Connection_Checker { public class Connection { public static MySqlConnection cnn = new MyS
无法连接到主机
。如何在运行时检查连接状态
代码如下
连接类
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;");
}
}
形式
我的计时器为enable=true如果我是表单中的您,我将不会在表单加载事件中打开连接 取而代之的是,我获取数据的方法是这样的
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;
//}
}
捕获传输错误不会告诉您连接何时失败。它只告诉您,当您尝试使用该连接时,该连接不可用 根据您的示例,要使用计时器进行连续状态监视,请使用此扩展方法直接查询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;
}
}
公共静态类扩展
{
///
///获取支持TcpClient的套接字的当前状态。
///与TcpClient.Connected不同,这是可靠的,不需要
///捕获因套接字写入失败而导致的异常。
///
///TcpClient实例
///TcpClient.Client套接字的当前状态
公共静态TcpState GetState(此TcpClient TcpClient)
{
var foo=IPGlobalProperties.GetIPGlobalProperties().GetActiveTCPCConnections()
.SingleOrDefault(x=>x.LocalEndPoint.Equals(tcpClient.Client.LocalEndPoint));
返回foo!=null?foo.State:TcpState.Unknown;
}
}
然而,正如其他人在评论中已经提到的,您对连续连接的依赖可能反映了应用程序中严重的设计缺陷。你应该听从别人的建议,重新设计你的应用程序。占用连接严重限制了可扩展性,不会使您受到网络管理员或数据库管理员的欢迎。不要让连接像这样打开。打开它,获取数据,关闭它。你的问题是关于计时器的吗?它显示的是什么?我的目标是在运行时和连接丢失系统会说断开,我实现了使用定时器。如果你认为你的数据连接是如此脆弱,那就试试看吧。你的问题是什么?你需要具体解释发生了什么,以及这与你想要发生的有什么不同。看,影子先生,回答?顺便说一句,谢谢。除非你检查异常错误本身,否则我不会认为异常总是连接错误。你救了我一天,先生。非常感谢你。我的问题解决了。这是我第一次在这里提问,仅仅几分钟就解决了。。
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;
}
}