bind socket to network interface

前端 未结 3 737
天涯浪人
天涯浪人 2020-12-02 17:49

How can I bind a socket to a particular network interface? I tried using setsockopt on server side, but the clients can still access the service through both et

3条回答
  •  温柔的废话
    2020-12-02 18:15

    The only way you can do it is as you mention -

    by setting the particular IP address using serv_addr.sin_addr.s_addr

    You can't do it without knowing the address to bind to.

    You can use ioctls to determine the current IP address if you need, though there may be a cleverer way to do this these days - I've not done much in modern Linux distros lately.

提交回复
热议问题