あなたはこのようにそれを行うことができると思います: 試してみてください: チェックしたいポートの 1433 を変更してください。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
namespace CheckPortStatus
{
class Program
{
static void Main(string[] args)
{
try
{
TcpClient tcp = new TcpClient();
tcp.Connect("localhost", Convert.ToInt16(1433));
Console.WriteLine("online");
}
catch (Exception ex)
{
Console.WriteLine("offline");
Console.WriteLine(ex.Message);
}
}
}
}
また、マシンで使用可能なポートを確認するには、次を実行します。
C:>netstat -an |find /i "listening"
TCP 0.0.0.0:25 0.0.0.0:0 リスニング
TCP 0.0.0.0:80 0.0.0.0:0 リスニング
TCP 0.0.0.0:135 0.0.0.0:0 リスニング