Reputation: 23
We are using a Delphi TServerSocket and want to assign a port automatically. How can we find an open port in Delphi 7?
Upvotes: 2
Views: 1595
Reputation: 595320
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.
Upvotes: 3
Reputation: 28829
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.
Upvotes: 0