2

I am trying to create the following functionality in inno setup.

The user is asked to enter a port they wish my application to communicate on. Once they have entered a port they can hit a check button. This check button will run some code to see whether the port on the installation machine is available.

So far I am fine with creating the input box for the user to enter the port they wish to test. I have the following code to test whether the port is available.

int port = 456; //<--- This is your value
bool isAvailable = true;

// Evaluate current system tcp connections. This is the same information provided
// by the netstat command line application, just in .Net strongly-typed object
// form.  We will look through the list, and if our port we would like to use
// in our TcpClient is occupied, we will set isAvailable to false.
IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();

foreach (TcpConnectionInformation tcpi in tcpConnInfoArray)
{
    if (tcpi.LocalEndPoint.Port == port)
    {
        isAvailable = false;
        break;
    }
}

Console.WriteLine("Port is available");

My first question is: is it possible to return to inno setup the true or false value from the port test code? If its false then I need to let the user know.

My second question is: is the port test code correct?

My third question is: am I right in thinking I will then potentially need to open that port in the firewall.

Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
user589195
  • 4,180
  • 13
  • 53
  • 81
  • 1
    1) that code is in C#, you rather meant if it's possible to run the application and return its result into the installer; yes, that is possible. 2) I don't know C# and don't know what you mean by test port at all (if it's possible to pass through firewall, or something ?). 3) I guess so. – TLama Jul 05 '13 at 09:21
  • Thanks for the quick response tlama. Yes I would like to compile the c# into a batch file, and then run the batch file when the "check" button is clicked. Then return the result of the code to the installer, to feedback the result to the user. Would you be able to post an example of how to do this? My aim is to check if a port is available for program to communicate through, IE nothing else using it and not blocked by the firewall. – user589195 Jul 05 '13 at 09:26
  • 2
    Might be [`this way`](http://pastebin.com/AV7ME6TK), although I don't think it's a good way to execute a C# program from installer. Think about prerequisites needed for its execution. I would prefer to call native Windows API functions from InnoSetup script instead, or directly make a simple request to the target server. – TLama Jul 05 '13 at 09:38
  • I did think it would be better to try and perform this functionality from with the innoscript code, but I know very little about the language ( my grounding is in .net ). If someone could give me some pointers on how to perform what I want to do in innosetup that would be fantastic. – user589195 Jul 05 '13 at 09:41
  • 2
    This kind of code isn't useful. There's no guarantee whatsoever that the port is in use when you run the installer. And the port number is never not a detail, the client code that uses your server needs to know the port number as well. Pick a number and allow an admin to change it to work around a conflict. And allow the port to pass through the firewall. A setting with scope = application will do just fine. – Hans Passant Jul 05 '13 at 11:25
  • Hans, thanks for the response. So the problem with this code is that it will only flag a port in use if it is currently being used by another application? If this is the case I can see the limitations of this, but I still think it could be of use. – user589195 Jul 05 '13 at 11:54
  • Possible duplicate of [How to check if port is usable in Inno Setup?](https://stackoverflow.com/questions/21701847/how-to-check-if-port-is-usable-in-inno-setup) – Martin Ba Jul 28 '17 at 10:54

0 Answers0