How to support both IPv4 and IPv6 connections(如何同时支持 IPv4 和 IPv6 连接)
问题描述
我目前正在开发一个 UDP 套接字应用程序,我需要构建支持,以便 IPV4 和 IPV6 连接可以将数据包发送到服务器.
I'm currently working on a UDP socket application and I need to build in support so that IPV4 and IPV6 connections can send packets to a server.
我希望有人能帮助我并指出正确的方向;我发现的大部分文档都不完整.如果您能指出 Winsock 和 BSD 套接字之间的任何区别,也会很有帮助.
I was hoping that someone could help me out and point me in the right direction; the majority of the documentation that I found was not complete. It'd also be helpful if you could point out any differences between Winsock and BSD sockets.
提前致谢!
推荐答案
最好的方法是创建一个也可以接受 IPv4 连接的 IPv6 服务器套接字.为此,创建一个常规的 IPv6 套接字,关闭关闭套接字选项IPV6_V6ONLY
,将其绑定到任何"地址,然后开始接收.IPv4 地址将显示为 IPv6 地址,采用 IPv4-mapped 格式.
The best approach is to create an IPv6 server socket that can also accept IPv4 connections. To do so, create a regular IPv6 socket, turn off the socket option IPV6_V6ONLY
, bind it to the "any" address, and start receiving. IPv4 addresses will be presented as IPv6 addresses, in the IPv4-mapped format.
不同系统的主要区别在于 IPV6_V6ONLY
是否 a) 可用,以及 b) 默认打开或关闭.它在 Linux 上默认关闭(即允许没有 setsockopt 的双栈套接字),并在大多数其他系统上打开.
The major difference across systems is whether IPV6_V6ONLY
is a) available, and b) turned on or off by default. It is turned off by default on Linux (i.e. allowing dual-stack sockets without setsockopt), and is turned on on most other systems.
此外,Windows XP 上的 IPv6 堆栈不支持该选项.在这些情况下,您需要创建两个单独的服务器套接字,并将它们放入 select 或多个线程中.
In addition, the IPv6 stack on Windows XP doesn't support that option. In these cases, you will need to create two separate server sockets, and place them into select or into multiple threads.
这篇关于如何同时支持 IPv4 和 IPv6 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何同时支持 IPv4 和 IPv6 连接
基础教程推荐
- 设计字符串本地化的最佳方法 2022-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++,'if' 表达式中的变量声明 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01