NetTalk Central

Author Topic: Ephemeral ports with NetTalk?  (Read 4054 times)

Flint G

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • Email
Ephemeral ports with NetTalk?
« on: August 29, 2017, 02:01:55 PM »
Is it possible to pop up a simple NetTalk web server on-the-fly, to handle a simple GET against localhost from a Windows app?  The trick is that it needs an ephemeral port, because we could theoretically have many such popup servers running at any given moment, so I want the system to issue the port number to me.
NetTalk: 12.26
Clarion: 9.1.11529
Brave: 1.31.88
Chrome: 95.0.4638.69
Edge: 95.0.1020.44
ExtJS: 7.0.0.156

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11244
    • View Profile
Re: Ephemeral ports with NetTalk?
« Reply #1 on: August 30, 2017, 06:21:05 AM »
Hi Flint,

>> Is it possible to pop up a simple NetTalk web server on-the-fly, to handle a simple GET against localhost from a Windows app?

I'm not 100% sure I understand what you mean by "pop up". If you want a web server, then yes, just start a procedure that contains a web server object, you can open that procedure, and close it whenever you like.

>> The trick is that it needs an ephemeral port, because we could theoretically have many such popup servers running at any given moment, so I want the system to issue the port number to me.

The port number is an expression, so you can put a variable in there, yes.

cheers
Bruce

Flint G

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • Email
Re: Ephemeral ports with NetTalk?
« Reply #2 on: August 30, 2017, 02:10:04 PM »
>> I'm not 100% sure I understand what you mean by "pop up".

What I mean is, we're trying to ensure that we have a local web server to respond to a simple GET (the reason we need one is a different topic for a different day), but we can't rely on the operating system to just have one laying around.  So the approach I'm thinking about would be to start a very basic web server for the task, and shut it down immediately after its purpose is fulfilled.  So the short answer here, is "yes."

>> The port number is an expression, so you can put a variable in there

But how do I get Windows to tell me what port to use?  Because Windows will know what ports are currently used, I assume, and my program will not.  But my program could be running many times (say: 100), with each instance needing to start a web server concurrently on a unique port number.

Alternately: can I test within a web server procedure whether a given port number is already in use, and randomly choose another port to use?  This would fulfill the same purpose, as far as I am concerned.
NetTalk: 12.26
Clarion: 9.1.11529
Brave: 1.31.88
Chrome: 95.0.4638.69
Edge: 95.0.1020.44
ExtJS: 7.0.0.156

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11244
    • View Profile
Re: Ephemeral ports with NetTalk?
« Reply #3 on: August 30, 2017, 11:51:31 PM »
>> But how do I get Windows to tell me what port to use?  Because Windows will know what ports are currently used, I assume, and my program will not.

maybe it does. I've not done it via an API, but the Command
netstat -an
will return what is listening on what.
So the caller can call this (ideally with OddJob) get the result, parse out the listening ports, and then pick some other random number that's not on that list.
Then pass that number to the new process on the command line.

>> Alternately: can I test within a web server procedure whether a given port number is already in use, and randomly choose another port to use? 

yes, you can do it this way - the only problem then is letting the client program know which port was chosen.

Cheers
Bruce