views:

77

answers:

2

We are using a Delphi TServerSocket and want to assign a port automatically. How can we find an open port in Delphi 7?

A: 

If the server picks a port number randomly at run-time then how does the client know which one to connect to?

Usually, you pick an unused port number more or less randomly at design-time. There's a list on the internet of reserved port numbers that you probably want to stay away from, otherwise you should be fine.

If you're not sure what ports might already be in use on a particular setup, the netstat command is your friend.

500 - Internal Server Error
The server is embedded in our client application. It will "listen" to broadcast messages from our server application. So, our client will send a message to our server with its "listener" address/port. We would like to auto select an unused port and/or allow the user to select a port from a list of unused ports.
Randy Hosler
I see. It looks like Remy gave the answer you were after, then.
500 - Internal Server Error
+3  A: 

Set the TServerSocket.Port property to 0. The OS will assign the first available port it finds. You can then read the TServerSocket.Socket.LocalPort property after the server is active to find out which port was actually asssigned.

Remy Lebeau - TeamB