Prüfen Sie, ob ein port offen ist

Ich kann nicht scheinen, etwas zu finden, dass mir sagt, ob ein port in meinem router geöffnet ist oder nicht.
Ist das überhaupt möglich?

Den code habe ich jetzt nicht wirklich zu funktionieren scheint...

private void ScanPort()
{
    string hostname = "localhost";
    int portno = 9081;
    IPAddress ipa = (IPAddress) Dns.GetHostAddresses(hostname)[0];
    try
    {
        System.Net.Sockets.Socket sock =
                new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,
                                              System.Net.Sockets.SocketType.Stream,
                                              System.Net.Sockets.ProtocolType.Tcp);
        sock.Connect(ipa, portno);
        if (sock.Connected == true) //Port is in use and connection is successful
            MessageBox.Show("Port is Closed");
        sock.Close();
    }
    catch (System.Net.Sockets.SocketException ex)
    {
        if (ex.ErrorCode == 10061) //Port is unused and could not establish connection 
            MessageBox.Show("Port is Open!");
        else
            MessageBox.Show(ex.Message);
    }
}
Wie scheitert es? Sind die Ergebnisse nicht wie erwartet, sind Sie immer eine Ausnahme?
auch wenn der code verbindet den Hafen, es sollte sagen, der port ist offen, nicht geschlossen
Ich will es sagen, dass mein port offen ist, weil ich es weitergeleitet in meinem router, aber es ist nicht, um eine Verbindung...

InformationsquelleAutor Yuki Kutsuya | 2012-08-06

Schreibe einen Kommentar