What client-side situations need bind()?

后端 未结 7 1716
长情又很酷
长情又很酷 2021-01-01 16:21

I\'m learning C socket programming. When would you use bind() on the client-side? What types of program will need it and why? Where can I find an example?

7条回答
  •  天涯浪人
    2021-01-01 16:33

    A good situation would be in a p2p case, you’re communicating with a STUN Server with a bound socket, and the STUN Server tells you the port on which he is receiving messages from your socket (that can be different from the one you specified when you bound your socket depending on your network and more specifically on your NAT type). This will allow you to be aware of the real port translation that your NAT is doing, and you’ll be able to give this information to potential peers that want to connect to you. Binding the socket is useful as some NATs are dynamically giving you ports (binding on port x twice might not give you the same “real” port). So you’ll be able to directly use the socket you bound to listen on the port.

提交回复
热议问题