Finding an available network port on the machine

Posted by Tomer Vromen on Stack Overflow See other posts from Stack Overflow or by Tomer Vromen
Published on 2010-06-02T19:17:23Z Indexed on 2010/06/02 19:24 UTC
Read the original article Hit count: 267

Filed under:
|
|
|

I'm trying to implement a simple FTP server (a variation of the EFTP protocol) in linux. When a client connects and sends the PASV command, the server should respond with a port number, so the client can connect to that port to transmit the file. How can the server choose a port number? Do I need to iterate through all the ports from 1024 to 65535 until I find a port that the process can bind to?
I know that calling bind() with 0 as the port automatically chooses the port to bind to, but then how can I know which port was chosen?

Many thanks.

© Stack Overflow or respective owner

Related posts about c

    Related posts about linux