简体   繁体   中英

How can i check if MySQL server is alive from C# MOBILE before i connect to my database

I need a quick way to check if the server that runs MySQL is started from C# mobile. I tried with Socket ... very slowly !!! ... and Ping class does not exist in C # mobile

IPHostEntry myHostEntry = Dns.GetHostEntry("192.168.2.222");
IPEndPoint host = new IPEndPoint(myHostEntry.AddressList[0], 3306);

Socket s = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
s.Connect(host);
if (s.Connected) { 
      MessageBox.Show("Ping Ok!!!"); 
} else {
       MessageBox.Show("Ping not Ok !!!");
}

Does anyone have any suggestions ???

If you can host some kind of script on the server, where MySQL is located then write a simple script that connects to local MySQL and prints 1 on success and 0 on failure. Query the script with HTTP from C# mobile. This would be most reliable solution imho.

Adjust your timeouts and remove the dns-resolve.

IPEndPoint host = new IPEndPoint(IPAddress.Parse("192.168.2.222"), 3306);

Socket s = new Socket(AddressFamily.InterNetwork,
s.ReceiveTimeout = 100;
s.SendTimeout = 100;
SocketType.Stream, ProtocolType.Tcp);
s.Connect(host);
if (s.Connected) { 
      MessageBox.Show("Ping Ok!!!"); 
} else {
       MessageBox.Show("Ping not Ok !!!");
}

But this can unreliable in a high latency environment (mobile-networks)

The server might be alive and healthy, but your "health check" may timeout leading you to think it's dead; on the other hand, you may find out your server is perfectly healthy and you proceed with your queries, which in turn timeout (it's not that different a situation).

I'd suggest you assume the server is alive AND assume every query can and will timeout, handling appropriately the various conditions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM