How to check if a TCP port is blocked using C#
Feb 9, 2010
How to check if a TCP port is blocked using C#?
In order to check if a TCP port is blocked, use the System.Net and System.Net.Sockets classes as shown below:
protected void Button1_Click(object sender, EventArgs e)
{
string host = "localhost";
int port = 6900;
IPAddress addr = (IPAddress)Dns.GetHostAddresses(host)[0];
try
{
TcpListener tcpList = new TcpListener(addr, port);
tcpList.Start();
}
catch (SocketException sx)
{
// Catch exception here if port is blocked
}
}
Share
Labels: