简体   繁体   中英

How to check if server is reachable in PCL project xamarin?

I have been searching for solution to test or check the connectivity between my device and the server that contains my database. Note: I want to implement the solution on my PCL project.

You can use James Montemagno's Connectivity Plugin for this.

You can either check if the device you are using is connected by calling;

bool isConnected = CrossConnectivity.Current.IsConnected;

Or, you can 'ping' a server to see if it can be reached. You can do that like this:

var isReachable = await CrossConnectivity.Current.IsReachable("google.com", 5000);

You can also provide an IP address. The second parameter is the timeout which defaults to 5 seconds. There is also the specific IsRemoteReachable method which also let's you specify a port number.

This library can be used in a PCL.

Note; for Android you will need the ACCESS_NETWORK_STATE and ACCESS_WIFI_STATE permissions, it should be added automatically when you install the NuGet package.

In the most cases it is completed with ICMP pings. You can ping the server every 1-5 secs depending on your appliation. But I suggest to use multicast address and ping that from the server because of performance and securtiy reasons (the last one needs firewall be configurated on server side too). You can determine connectivity lost if some of the replys (at least 3) doesn't reach the target host.

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