Sometimes your application connects to remote services for any purpose such as querying a database, sending e-mails, sending SMS and etc. You need to make sure that the service is running and accept incoming connections across the network. One simple method is to telnet it using the command line by writing the following command line:
Telnet [Server IP Address] [Service Port Number]
But if you want to make sure that the service is running from your application!
Let us show what the telnet command does?
It tries to establish a TCP/IP connection to the server on a specific port number. The good news is that the System.Net.Sockets has the Socket class that provides a rich set of methods and properties for network communications.
The Socket class allows you to perform both synchronous and asynchronous data transfer using any of the communication protocols listed in the ProtocolType enumeration.
In this article we will see how to use the Socket class to check that a service is running on a remote server.
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.Sockets;
private Socket clientSocket;
private IPAddress hostAddress;
private void TelnetButton_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(IPTextBox.Text))
return;
if (string.IsNullOrEmpty(PortTextBox.Text))
return;
int port;
hostAddress = Dns.GetHostEntry(IPTextBox.Text).AddressList[0];
int.TryParse(PortTextBox.Text, out port);
if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
else if (hostAddress.AddressFamily == AddressFamily.InterNetworkV6)
clientSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
SocketAsyncEventArgs telnetSocketAsyncEventArgs = new SocketAsyncEventArgs();
telnetSocketAsyncEventArgs.RemoteEndPoint = new IPEndPoint(hostAddress, port);
telnetSocketAsyncEventArgs.Completed += new
EventHandler<SocketAsyncEventArgs>(telnetSocketAsyncEventArgs_Completed);
clientSocket.ConnectAsync(telnetSocketAsyncEventArgs);
}
catch (SocketException ex)
{
MessageBox.Show(ex.Message, "Service Is not Running",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
}
}
10- In the above code:
private void telnetSocketAsyncEventArgs_Completed(object sender, SocketAsyncEventArgs e)
{
try
{
if (e.SocketError == SocketError.Success)
{
if (e.LastOperation == SocketAsyncOperation.Connect)
{
MessageBox.Show("Service Is Running", hostAddress.ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("Service Is not Running", e.SocketError.ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (SocketException ex)
{
MessageBox.Show(ex.Message, "Service Is not Running",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Now you have an application that can telnet a remote service and tells you if the service is running or not.
15 March 2022
17 February 2022
09 December 2019