Sometimes your application connects to other servers for any purpose such as querying a database, sending e-mails, sending SMS and etc. You need to make sure that the server is reachable across the network. One simple method is to ping it using the command line by writing the following command:
Ping [server IP address]
But if you want to make sure the server is reachable across the network from your application! The good news is that the System.Net.NetworkInformation has a Ping class that is used to ping network servers. In this article we will see how to use the Ping class to check the availability of a server. You can use Ping and related classes to check whether a computer is reachable across the network.
Using the Code:
To make your application make sure a server is reachable across the network, follow these steps:
using System.Net;
using System.Net.NetworkInformation;
private void PingButton_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(IPTextBox.Text))
return;
string host = IPTextBox.Text;
IPAddress hostAddress = Dns.GetHostEntry(host).AddressList[0];
Ping ping = new Ping();
PingReply pingReply = ping.Send(hostAddress);
if (pingReply.Status == IPStatus.Success)
{
MessageBox.Show("Server ( " + pingReply.Address.ToString()
+ " ) is reachable.", pingReply.Status.ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Server is unreachable.",pingReply.Status.ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
catch (PingException ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Now you have an application that can check whether a computer is reachable across the network.
15 March 2022
17 February 2022
09 December 2019