Skip to content

Instantly share code, notes, and snippets.

@yallie
Created July 19, 2017 18:43
Show Gist options
  • Save yallie/df43f8426aef0c1d6527afb7f4262c2f to your computer and use it in GitHub Desktop.
Save yallie/df43f8426aef0c1d6527afb7f4262c2f to your computer and use it in GitHub Desktop.
IsServerRunning — checks whether a server is running at the specified endpoint
public static bool IsServerRunning(string url)
{
if (string.IsNullOrWhiteSpace(url))
{
throw new ArgumentNullException("url");
}
Uri uri;
try
{
uri = new Uri(url);
}
catch (Exception ex)
{
throw new ArgumentException("url", ex);
}
try
{
using (var tcp = new TcpClient())
{
tcp.Connect(uri.Host, uri.Port);
return true;
}
}
catch
{
return false;
}
}
@yallie
Copy link
Author

yallie commented Jul 19, 2017

A few notes:

  • The method checks whether some server is listening on the given endpoint.
  • It doesn't check whether the server implements certain Zyan protocol.
  • It doesn't support non-TCP based urls, IPC for example.
  • URL prefix is ignored, so it should work with tcp://, tcpex://, http://, whatever.
  • The method is kinda slow when the server is not available (has to wait for TCP connect timeout).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment