错误“地址已在使用中”带 ipv6 udp 服务器套接字
您好,亲爱的同事,
我需要一些故障排除的提示/想法。
我的程序在两个实例中运行。每个实例都有自己的网络接口(eth0 和 eth1)。两个接口都使用 IPv6 互联网地址,因为这是由标准规定的(对于我们的应用程序)。
问题:如果实例 1 或实例 2 正在运行,我的应用程序将顺利运行。当两个应用程序同时启动时,两个实例之一都会崩溃。我捕捉到信号并收到消息
Address already in use
对于我来说,这没有意义。我打开两个不同的套接字,每个接口一个。但证明我的错误。也许我对 IPv6 参数 sin6_scope_id 的理解错误。此外,对于 IPv6 使用 in6addr_any - 它的行为不等于 IPv4 的 INADDR_ANY 吗?
在此程序中,我打开以下 udp 套接字:
socketDescriptor_ = socket(AF_INET6, SOCK_DGRAM, htons(PC_UNSPEC));
我还添加了一些套接字选项:
bool UDPSocket::AddSocketOptions2UDPSocket() {
int optval = 1;
if (setsockopt(socketDescriptor_, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
return false;
}
struct timeval sdpTimeout {};
sdpTimeout.tv_sec = 18;
sdpTimeout.tv_usec = 0;
if (setsockopt(socketDescriptor_, SOL_SOCKET, SO_RCVTIMEO, (void *) &sdpTimeout, sizeof(sdpTimeout)) < 0) {
return false;
}
return true;
}
之后,我在绑定套接字之前构造地址描述符。 对于地址描述符(方法返回对象描述符),我同时编写了两个解决方案:
1.)
descriptor.addressDescriptorIPv6.sin6_family = AF_INET6;
descriptor.addressDescriptorIPv6.sin6_addr = in6addr_any;
descriptor.addressDescriptorIPv6.sin6_port = htons(portNumber);
descriptor.addressDescriptorIPv6.sin6_scope_id = if_nametoindex(interfaceName_.c_str());
2.)
descriptor.addressDescriptorIPv6.sin6_family = AF_INET6;
inet_pton(AF_INET6,IPAddress.c_str(AF_INET6, &descriptor.addressDescriptorIPv6.sin6_addr);
descriptor.addressDescriptorIPv6.sin6_port = htons(portNumber);
descriptor.addressDescriptorIPv6.sin6_scope_id =
if_nametoindex(interfaceName_.c_str());
绑定如下所示:
if (bind(socketDescriptor_, (struct sockaddr *) &localSocketAddressDescriptor_, sizeof(localSocketAddressDescriptor_)) < 0) {
connected_ = false;
return false;
} else {
connected_ = true;
return true;
}
此外,我尝试使用更多套接字选项将套接字绑定到特定网络设备:
setsockopt(socketDescriptor_,
SOL_SOCKET,
SO_BINDTODEVICE,
interfaceName_.c_str(),
sizeof(interfaceName_.c_str()) < 0);
感谢您的帮助!
亲切的问候, 马蒂亚斯
Hello dear colleagues,
I need some hints/ideas for troubleshooting.
My programme runs in two instances. Each instance has its own network interface (eth0 and eth1). Both interfaces are using IPv6 internet addresses due to this is prescribed by a standard (for our application).
The problem: if instance 1 or instance 2 is running, my application runs through without any issues. When both applications are started at the same time, one of both instances crashes. I catch the signal and get the message
Address already in use
For my, it doesn't make sense. I open two different sockets, one for each interface. But proof my wrong. Maybe I understood the IPv6 parameter sin6_scope_id wrong. In addition, the usage of in6addr_any for IPv6 - doesn't it equal behave to INADDR_ANY of IPv4?
In this programme I open the following udp socket:
socketDescriptor_ = socket(AF_INET6, SOCK_DGRAM, htons(PC_UNSPEC));
As well I added some socket options:
bool UDPSocket::AddSocketOptions2UDPSocket() {
int optval = 1;
if (setsockopt(socketDescriptor_, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
return false;
}
struct timeval sdpTimeout {};
sdpTimeout.tv_sec = 18;
sdpTimeout.tv_usec = 0;
if (setsockopt(socketDescriptor_, SOL_SOCKET, SO_RCVTIMEO, (void *) &sdpTimeout, sizeof(sdpTimeout)) < 0) {
return false;
}
return true;
}
After that, I construct the address descriptor before I bind the socket.
For the address descriptor (method returns object descriptor) I wrote two solutions meanwhile:
1.)
descriptor.addressDescriptorIPv6.sin6_family = AF_INET6;
descriptor.addressDescriptorIPv6.sin6_addr = in6addr_any;
descriptor.addressDescriptorIPv6.sin6_port = htons(portNumber);
descriptor.addressDescriptorIPv6.sin6_scope_id = if_nametoindex(interfaceName_.c_str());
2.)
descriptor.addressDescriptorIPv6.sin6_family = AF_INET6;
inet_pton(AF_INET6,IPAddress.c_str(AF_INET6, &descriptor.addressDescriptorIPv6.sin6_addr);
descriptor.addressDescriptorIPv6.sin6_port = htons(portNumber);
descriptor.addressDescriptorIPv6.sin6_scope_id =
if_nametoindex(interfaceName_.c_str());
The binding looks as follows:
if (bind(socketDescriptor_, (struct sockaddr *) &localSocketAddressDescriptor_, sizeof(localSocketAddressDescriptor_)) < 0) {
connected_ = false;
return false;
} else {
connected_ = true;
return true;
}
Additionally, I tried to bind the socket to a specific network device with further socket options:
setsockopt(socketDescriptor_,
SOL_SOCKET,
SO_BINDTODEVICE,
interfaceName_.c_str(),
sizeof(interfaceName_.c_str()) < 0);
Thanks for any help!
Kind regards,
Matthias
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论