Get random port for UDP socket

前端 未结 3 893
旧时难觅i
旧时难觅i 2021-01-02 00:49

I need to create a program that will communicate with other programs on the same computer via UDP sockets. It will read commands from stdin, and some of this commands will m

3条回答
  •  青春惊慌失措
    2021-01-02 01:26

    Call bind() specifying port 0. That will allow the OS to pick an unused port. You can then use getsockname() to retreive the chosen port.

提交回复
热议问题