Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CrossConnectivity.Current.IsRemoteReachable in MAUI #27623

Closed
vsfeedback opened this issue Feb 7, 2025 · 2 comments
Closed

CrossConnectivity.Current.IsRemoteReachable in MAUI #27623

vsfeedback opened this issue Feb 7, 2025 · 2 comments
Labels
area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info s/needs-info Issue needs more info from the author s/no-recent-activity Issue has had no recent activity

Comments

@vsfeedback
Copy link

This issue has been moved from a ticket on Developer Community.


[severity:It's more difficult to complete my work]
Hello, we are migrating our Xamarin app to MAUI.

In Xamarin we were used to check the availability of our APIs with CrossConnectivity.Current.IsRemoteReachable("https://xxxxxxxxxxxxxx", 33233))

We see this IsRemoteReachable is not actually present as method of the CrossConnectivity, how can we now check in MAUI?

Thank you!


Original Comments

Feedback Bot on 1/31/2025, 01:17 AM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

@jsuarezruiz
Copy link
Contributor

Can create a helper class and use sockets to ping the host like the original method:

public async Task<bool> IsRemoteReachable(string host, int port = 80, int msTimeout = 5000)
{
	if (string.IsNullOrEmpty(host))
		throw new ArgumentNullException("host");

	host = host
		.Replace("http://www.", string.Empty, StringComparison.CurrentCultureIgnoreCase)
		.Replace("http://", string.Empty, StringComparison.CurrentCultureIgnoreCase)
		.Replace("https://www.", string.Empty, StringComparison.CurrentCultureIgnoreCase)
		.Replace("https://", string.Empty, StringComparison.CurrentCultureIgnoreCase);

	return await Task.Run(() =>
	{
		try
		{
			var clientDone = new ManualResetEvent(false);
			var reachable = false;
			var hostEntry = new DnsEndPoint(host, port);
			using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
			{
				var socketEventArg = new SocketAsyncEventArgs { RemoteEndPoint = hostEntry };
				socketEventArg.Completed += (s, e) =>
				{
					reachable = e.SocketError == SocketError.Success;

					clientDone.Set();
				};

				clientDone.Reset();

				socket.ConnectAsync(socketEventArg);

				clientDone.WaitOne(msTimeout);

				return reachable;
			}
		}
		catch (Exception ex)
		{
			Debug.WriteLine("Unable to reach: " + host + " Error: " + ex);
			return false;
		}
	});
}

@jsuarezruiz jsuarezruiz added area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info s/needs-info Issue needs more info from the author labels Feb 7, 2025
Copy link
Contributor

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

@dotnet-policy-service dotnet-policy-service bot added the s/no-recent-activity Issue has had no recent activity label Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info s/needs-info Issue needs more info from the author s/no-recent-activity Issue has had no recent activity
Projects
None yet
Development

No branches or pull requests

2 participants