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

后端 未结 3 1381
隐瞒了意图╮
隐瞒了意图╮ 2021-02-14 02:44

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 02:54

    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.

提交回复
热议问题