博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
能否不同udp socket绑定到同一IP地址和port
阅读量:6705 次
发布时间:2019-06-25

本文共 1948 字,大约阅读时间需要 6 分钟。

 

The main differences between BSD and Windows are conveniently described on . According to that, one would say that a solution:

1) For BSD "0" and for Windows "SO_EXCLUSIVEADDRUSE"
2) For BSD "SO_REUSEPORT | SO_REUSEADDR" and for Windows "SO_REUSEADDR"

Using SO_REUSEADDR

The SO_REUSEADDR socket option allows a socket to forcibly bind to a port in use by another socket. The second socket calls  with the optname parameter set to SO_REUSEADDR and the optval parameter set to a boolean value of TRUE before calling on the same port as the original socket. Once the second socket has successfully bound, the behavior for all sockets bound to that port is indeterminate. For example, if all of the sockets on the same port provide TCP service, any incoming TCP connection requests over the port cannot be guaranteed to be handled by the correct socket — the behavior is non-deterministic. A malicious program can use SO_REUSEADDR to forcibly bind sockets already in use for standard network protocol services in order to deny access to those service. No special privileges are required to use this option.

If a client application binds to a port before a server application is able to bind to the same port, then problems may result. If the server application forcibly binds using the SO_REUSEADDR socket option to the same port, then the behavior for all sockets bound to that port is indeterminate.

The exception to this non-deterministic behavior is multicast sockets. If two sockets are bound to the same interface and port and are members of the same multicast group, data will be delivered to both sockets, rather than an arbitrarily chosen one.

 

windows没有SO_REUSEPORT选项,只有SO_REUSEADDR,但

windows的SO_REUSEADDR 等同于BSD的SO_REUSEADDR和SO_REUSEPORT

但如果两个socket同时绑定到同一地址和同一port的话,这些socket的行为是不确定的,主要体现在接收数据上,数据会投递给随机选择的一个socket。

所以,两个socket绑定到同一地址和同一port,同时用udp接收数据的话,只有一个socket能够收到数据。

这样是不行的。只能用一个socket。

转载于:https://www.cnblogs.com/yanhc/p/10923505.html

你可能感兴趣的文章
ASP.NET 页生命周期概述
查看>>
Xen虚拟机克隆实战
查看>>
HttpContext.Current.Session ,出现未将对象引用设置到实例上
查看>>
所谓深度链接(Deep linking)
查看>>
C#中的数据格式转换 (未完待更新)
查看>>
基于 Python 官方 GitHub 构建 Python 文档
查看>>
ArcSDE:C#创建SDE要素数据集
查看>>
arulesSequences包做序列模式的关联分析
查看>>
下面以Button组件为例,开始FLEX皮肤制作的入门。
查看>>
CSS学习(一)
查看>>
SQL Server 数据库安全
查看>>
Android双机(网络和USB)调试及其完美ROOT
查看>>
Linux Suspend过程【转】
查看>>
算法-大整数加法
查看>>
变量命名那点小事
查看>>
程序员敏捷之路秘笈:降龙十八掌
查看>>
Java 基础【02】 Super 用法
查看>>
makefile初步制作,arm-linux- (gcc/ld/objcopy/objdump)详解【转】
查看>>
VS2005中建立解决方案及多项目
查看>>
C语言ASM汇编内嵌语法【转】
查看>>