数据报套接字中的bind 和connect 方法的主要用途是什么?它们有必要使用吗?这些方法的使用与路由器中传入和传出数据包的条目有任何关系吗?
最佳答案
What is the main purpose of bind and connect methods in a datagram socket?
bind()
将套接字绑定(bind)到本地接口(interface)和端口。 connect()
条件本地 UDP 堆栈仅向连接目标发送和接收,就该套接字而言。
Are they necessary to use?
只有当您需要使用特定的本地端口时才需要使用bind()
,以便期望发送到该端口的对等方能够成功。仅当您只对一个对等方感兴趣并希望自动过滤掉所有其他 UDP Activity 时,才需要使用 connect()
。
Does the usage of these methods have any relation with the entries in a router for incoming and outgoing packets?
如果您不调用 bind(),
第一次向任何地方发送任何数据时,将自动向指定的 IP 地址执行 bind()
通过单播 IP 路由表到达目标的最佳本地路由。
关于Java:绑定(bind)和连接方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21157621/