Randy Hosler
Randy Hosler

Reputation: 23

In Delphi How can we find an open port?

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

Answers (2)

Remy Lebeau
Remy Lebeau

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

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

Related Questions