Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if windows firewall is blocking my program

I have an application that communicates with a NetApp device through their api. With the windows firewall on, the api commands will fail. With the firewall off, the api commands work. I don't receive any message like "Windows Firewall is blocking this program".

Looking through the documentation I believe that I found the TCP ports that need to be open for the api commands to work. How can I programatically detect if the ports are blocked so I can display a message to the user on the potential problem?

like image 434
Web Avatar asked Aug 04 '11 14:08

Web


2 Answers

The firewall manager exposes itself via COM and implements an IsPortAllowed.

like image 124
Alex K. Avatar answered Sep 21 '22 17:09

Alex K.


You can do it like this I think: give it a try: Change 1433 for the port you want to check.

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);
            }
        }
    }
}

Also, to see which ports are available in your machine run:

C:>netstat -an |find /i "listening"

TCP 0.0.0.0:25 0.0.0.0:0 LISTENING

TCP 0.0.0.0:80 0.0.0.0:0 LISTENING

TCP 0.0.0.0:135 0.0.0.0:0 LISTENING

like image 40
Carlos Quintanilla Avatar answered Sep 20 '22 17:09

Carlos Quintanilla