Jump to content
Mark-

THttpServer port number...

Recommended Posts

Hello

 

Is there a method/way/solution to configure an THttpServer instance to use a random free port number when "Start" is called?

 

Cheers,

 

Mark

Edited by Mark-

Share this post


Link to post

The component doesn't do that by itself but has a property to select the port to listen to. So just assign it to the value you need before calling start.

 

Please note that the client must know the port to connect so I don't understand why you want a random port! Unless you used the term "random" instead of "custom".

Share this post


Link to post

This is certainly a thing that is routinely done. 

 

https://en.m.wikipedia.org/wiki/Ephemeral_port

 

The system has support for generating such ports, I've used it with an Indy server listening on loopback for a client in the same process. I do this to implement an application help browser using an embedded Web browser control. I don't want to use a fixed port number because I don't want to risk clashing with other services. And since I control both server and client an ephemeral port is perfect. 

  • Thanks 1

Share this post


Link to post

TWSocket allows the port to set to zero and listen called with a random port allocated by Windows, this is used by the ICS FTP client for the PORT command.  

 

Not sure about TWSocketServer, never tried it but it's rather more complex listening on multiple ports so ignoring zero is quite possible, TWSocketServer is certainly not designed to listen on a random port.   Again the FTP client has code to allocated ports sequentially from a specific range, trying the next if in use, so you could borrow that.

 

Angus

Share this post


Link to post
7 hours ago, FPiette said:

The component doesn't do that by itself but has a property to select the port to listen to. So just assign it to the value you need before calling start.

 

Please note that the client must know the port to connect so I don't understand why you want a random port! Unless you used the term "random" instead of "custom".

I am the client (program) and server (a service) on the same computer. I do not want to use a fixed port, to avoid conflicts. When the service starts it would open the port (wanting any free port number) and save the port number to a file the client reads when it starts.

Edited by Mark-

Share this post


Link to post
4 hours ago, Angus Robertson said:

Not sure about TWSocketServer, never tried it...

When I set the port to 0 and call "Start" the port number does not change.

Perhaps I am doing something wrong.

Share this post


Link to post
4 hours ago, Angus Robertson said:

...the FTP client has code to allocated ports sequentially from a specific range, trying the next if in use, so you could borrow that.

Thanks

Share this post


Link to post

The port number property does not change if you specify zero, it remains zero.  You need to find the port number allocated by windows:

 

    saddr        : TSockAddrIn6;
    saddrlen     : Integer;
 

           { Get the port number as assigned by Windows }
            saddrLen  := SizeOf(saddr);
            ListenSocket.GetSockName(PSockAddrIn(@saddr)^, saddrLen);
            DataPort  := WSocket_ntohs(saddr.sin6_port);
 

This should work with the listen socket in SocketServer, but other things that need the port won't know about it. And not tested for zero.

 

Angus

Share this post


Link to post

Thanks.

That would not compile for THttpServer and my messing with it to get it to compile, did not return the correct port number when I set the port to 8080 for example.

 

Share this post


Link to post

It was concept code, how to get the port of an open socket. and can be simplified somewhat to:

 

BindIpPortStr := Socket.GetXPort;

 

since it's a built function that the FTP client seems not to use.  In this case Socket is your TSocketServer component, provided you are not using IcsHosts. 

 

The IcsHosts implementation up to V8.63 does not allow a zero port, since that means there should be an SSL port specified instead, each IcsHost is designed to listen on two ports at once.  But this was a bad design, so I'll change it for the next release so that both ports being zero uses a non-SSL random port.  I'll also return the random port allocated, somewhere. 

 

Angus

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×