How does a socket know which network interface controller to use?

前端 未结 4 807
慢半拍i
慢半拍i 2020-12-04 17:40

If a computer has multiple network cards, all of them connected to different networks and functioning properly, when we open a socket, how does the OS determine which NIC to

4条回答
  •  渐次进展
    2020-12-04 18:19

    I dont know why im included in the edit suggestion when i was not even related to this question .I got similar edit suggestion before as well..might be some bug/issue.

    (If you feel inclined to up-vote, @Shtééf's answer deserves it more than mine.)

    That depends on whether you are connecting or binding.

    If you bind, you can bind to a specific IP address corresponding to one of the machine's interfaces, or you can bind to 0.0.0.0, in which case the socket will listen on all interfaces.

    If you connect an unbound socket, then the machine's routing tables, in conjunction with the destination IP adress, will determine which interface the connection request goes out on.

    It is possible to bind a socket then connect it. In this case, the socket will remain bound as per the bind call when it makes the connection. (Thanks to @RemyLebeau for pointing this out.)

提交回复
热议问题