套接字如何知道要使用哪个网络接口控制器? [英] How does a socket know which network interface controller to use?

查看:83
本文介绍了套接字如何知道要使用哪个网络接口控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一台计算机有多个网卡,它们都连接到不同的网络并且可以正常工作,那么当我们打开一个插槽时,操作系统如何确定与此插槽一起使用哪个NIC?套接字API是否允许我们显式指定要使用的NIC?

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 use with this socket? Does the socket API allow us to explicitly specify the NIC that is to be used?

推荐答案

我什至与这个问题都不相关时,我不知道为什么im包含在编辑建议中.我之前也有类似的编辑建议.可能是一些错误/问题.

(如果您倾向于投票,@Shtééf的答案比我的更有价值.)

(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.

如果绑定,则可以绑定到与计算机接口之一相对应的特定IP地址,也可以绑定到0.0.0.0,在这种情况下,套接字将在所有接口上侦听.

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.

如果连接未绑定的套接字,则计算机的路由表以及目标IP地址将确定连接请求从哪个接口发出.

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.

可以绑定一个套接字然后再连接它.在这种情况下,套接字在建立连接时将按照bind调用保持绑定状态. (感谢@RemyLebeau指出这一点.)

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.)

这篇关于套接字如何知道要使用哪个网络接口控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆