問題描述
我目前正在開發一個 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 連接的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!