如题

解决方案 »

  1.   

    就是用Socket.Connect连接IP地址的指定端口...捕捉Exception..代码如下:
    static void Main(string[] args)
    {
    IPAddress ip = IPAddress.Parse("127.0.0.1");
    for (int i = 0; i < 1024 ;i++) {
    try {
    IPEndPoint point = new IPEndPoint(ip,i);
    Socket sock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
    sock.Connect(point);
    Console.WriteLine("连接端口{0}成功!",point);
    }catch(SocketException e) {
    if (e.ErrorCode != 10061) {
    Console.WriteLine(e.Message);
    }
    Console.WriteLine("连接{0}失败", i);
    }

    }
    }