- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在旧版本的 Mac OS X 中,通配符绑定(bind)的工作方式如下所述:
Multicast Addresses
The meaning of SO_REUSEADDR changes for multicast addresses as it allows multiple sockets to be bound to exactly the same combination of source multicast address and port. In other words, for multicast addresses SO_REUSEADDR behaves exactly as SO_REUSEPORT for unicast addresses. Actually the code treats SO_REUSEADDR and SO_REUSEPORT identically for multicast addresses, that means you could say that SO_REUSEADDR implies SO_REUSEPORT for all multicast addresses and the other way round.
和
MacOS X
At its very core, MacOS X is simply a BSD-style UNIX, based on a rather late fork of the BSD code, which was even synchronized with FreeBSD 5 for the Mac OS 10.3 release. That's why MacOS X offers the same options as BSD and they also behave the same way as in BSD.
但是在 10.10.5 中,我在测试网络库时发现了一个变化。
即使设置了 SO_REUSEADDR,两个未绑定(bind)(通配符)UDP 套接字也不能再共享同一端口 (errno=EADDRINUSE)。两者都必须设置 SO_REUSEPORT,这对我来说是个谜。
它可以用这个简单的测试代码重现:
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
int main() {
for(int p = 0; p < 4; ++p) {
printf("Flags set: ");
if(p&1) printf("SO_REUSEADDR ");
if(p&2) printf("SO_REUSEPORT");
printf("\n");
int handles[2];
bool success = true;
for(int i = 0; i < sizeof(handles)/sizeof(int); ++i) {
handles[i] = socket(AF_INET, SOCK_DGRAM, 0);
int flag = 1;
if((p&1) && setsockopt(handles[i], SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)) == -1) {
printf("Setsockopt %d, SO_REUSEADDR failed with errno\n", i, errno);
success = false;
break;
}
if((p&2) && setsockopt(handles[i], SOL_SOCKET, SO_REUSEPORT, &flag, sizeof(flag)) == -1) {
printf("Setsockopt %d, SO_REUSEPORT failed with errno\n", i, errno);
success = false;
break;
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = 2000; // does not matter as long as it is currently free
addr.sin_addr.s_addr = 0; // wildcard address
if(bind(handles[i], (struct sockaddr*)&addr, sizeof(addr)) == -1) {
printf("Bind %d failed with errno %d\n", i, errno);
success = false;
break;
}
}
if(success)
printf("Alright\n");
for(int i = 0; i < sizeof(handles)/sizeof(int); ++i)
close(handles[i]);
printf("\n");
}
return 0;
}
哪些输出:
Flags set:
Bind 1 failed with errno 48
Flags set: SO_REUSEADDR
Bind 1 failed with errno 48
Flags set: SO_REUSEPORT
Alright
Flags set: SO_REUSEADDR SO_REUSEPORT
Alright
最佳答案
In older versions of Mac OS X, wildcard binding works like described here:
Multicast Addresses
…您引用的描述适用于多播地址。您的测试代码不使用 multicast address 。因此,适用不同的描述(来自同一来源):
SO_REUSEPORT
SO_REUSEPORT
is what most people would expectSO_REUSEADDR
to be. Basically,SO_REUSEPORT
allows you to bind an arbitrary number of sockets to exactly the same source address and port as long as all prior bound sockets also hadSO_REUSEPORT
set before they were bound. …您的测试代码完全证实了这一点。
关于macos - SO_REUSEADDR 和 SO_REUSEPORT 的行为改变了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32661091/
我遇到过设置了 SO_REUSEADDR 选项的 UDP 套接字的有趣行为... 如果我通过 IP/UDP 发送 1472 字节,我会在一帧中得到所有数据——这是预期的...... 但是 1473 碎
是否存在最大套接字数可以限制 REUSEADDR 套接字操作。我达到了文件描述符的最大数量,但我可以对所有套接字使用相同的地址和端口,重用地址操作的数量是否有限制???如果有,我怎样才能得到号码 最佳
我已经使用 SO_REUSEADDR 让我的服务器被终止重新启动,而不会提示套接字已在使用中。我在想:SO_REUSEADDR 还有其他用途吗?除了上述目的之外,是否有人使用套接字选项? 最佳答案 对
这个问题在这里已经有了答案: What are the use cases of SO_REUSEADDR? (2 个答案) 关闭 4 年前。 我正在尝试理解多播代码,但我不理解一小部分的实用程序:
我在一个 Java 客户端库中看到了这句话: socket.setReuseAddress(true); 以为这是用来提高性能的,因为 SO_REUSEADDR 选项可以指示套接字可以强制使用 TIM
我有几个基本问题: 1.一个socket由一个协议(protocol)、一个本地ip、本地端口、远程ip和远程端口来表示。假设客户端和服务器之间存在这样的连接。现在当我将另一个客户端绑定(bind
事实 在 POSIX 文档中,我看不到任何阻止将 SO_REUSEADDR 套接字选项与 AF_UNIX 一起用于 UNIX 域套接字的情况。 但是,如果套接字节点已经存在,它总是会在绑定(bind)
我一直在尝试学习使用 c 进行套接字编程,但遇到了一些让我感到困惑的事情。我写了这段代码: int main(int argc, char *argv[]) { int sockfd =
我在 SO_REUSEADDR 上看到的所有讨论都假定它是同一个程序在已知端口上创建和绑定(bind)到 TCP 套接字。 我有两个不同的程序使用同一个端口,我很好奇这个机制是如何工作的——为了让程序
在 unix 网络编程中,我总是在服务器使用的套接字上设置 SO_REUSEADDR 选项来监听连接。这基本上表示可以在机器上的同一端口上打开另一个套接字。这在从崩溃中恢复并且套接字未正确关闭时非常有
我在树莓派上运行自己的 http 服务器。问题是当我停止程序并重新启动它时,该端口不再可用。有时我在收到大量请求时会遇到同样的问题。 我想使用 SO_REUSEADDR 这样即使发生错误我也可以继续使
请帮忙, 起初,我创建了一个套接字并将其绑定(bind)到没有 SO_REUSEADDR 的地址。 然后,我的程序崩溃了(没有正确关闭套接字) 接下来,我再次运行它并从 bind() 返回“Addre
(在 VS2017、Win7 x64 上运行) 我对 SO_REUSEADDR 的观点感到困惑和 SO_EXCLUSIVEADDRUSE .是的,我读过 MSDN documentation ,但我显
在旧版本的 Mac OS X 中,通配符绑定(bind)的工作方式如下所述: Socket options SO_REUSEADDR and SO_REUSEPORT, how do they dif
我有一个应用程序,其中单个客户端与单个服务器通信。通常情况下,客户端只做一次connect,然后重复调用send,就没有问题。 但是,我需要做一个客户端为每个单独的 send 建立连接的版本(有点像带
据我所知,SO_REUSEADDR 允许服务器绑定(bind)到一个处于 TIME_WAIT 状态的地址。但是它如何影响服务器在接受新连接时的 Action 呢? 假设我们正在主机 A 和服务器 B
我遇到了一个奇怪的行为。 manual claimed :“当两个套接字绑定(bind)到相同的端口号但在不同的显式接口(interface)上时,没有冲突”。 为什么在第 3 步出现以下情况时系统会
我正在使用 SO_REUSEADDR 选项,但我不确定为什么会得到 资源临时不可用选项。 我正在 127.0.0.1 上测试客户端服务器代码 if ( (sockfd = socket(AF_INET
我正在构建一个应用程序,该应用程序使用 multiprocessing 包的 Listener/Client 类与另一个脚本进行通信。 我可以成功创建监听器和客户端并进行通信,但是我仍然收到 TCP
我正在运行两个 SCTP RPM 包。lksctp-工具-1.0.11lksctp-tools-devel-1.0.11 我的 sctp 头文件位于/usr/include/netinet/sctp.
我是一名优秀的程序员,十分优秀!