2014-11-21 35 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連接嚴重限制了可擴展性,並且不會讓您受到網絡管理員或數據庫管理員的歡迎。