how do I find in C that a port is free to use?

后端 未结 3 1761
长情又很酷
长情又很酷 2021-02-14 02:19

The OS is Linux. I have a server process that can change its port realtime. However I would like to know in advance if a port is free before binding.

Scenario: Server bi

3条回答
  •  别跟我提以往
    2021-02-14 03:17

    This is an obvious race condition, since other processes on your system might be binding to ports in parallel. So, any solution you find will be imperfect, and you will still need to just write it according to the "try to bind(), if it fails pick a new port number and try again" approach.

提交回复
热议问题