問題描述
我目前正在開發(fā)一個 UDP 套接字應(yīng)用程序,我需要構(gòu)建支持,以便 IPV4 和 IPV6 連接可以將數(shù)據(jù)包發(fā)送到服務(wù)器.
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.
我希望有人能幫助我并指出正確的方向;我發(fā)現(xiàn)的大部分文檔都不完整.如果您能指出 Winsock 和 BSD 套接字之間的任何區(qū)別,也會很有幫助.
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.
提前致謝!
推薦答案
最好的方法是創(chuàng)建一個也可以接受 IPv4 連接的 IPv6 服務(wù)器套接字.為此,創(chuàng)建一個常規(guī)的 IPv6 套接字,關(guān)閉關(guān)閉套接字選項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.
不同系統(tǒng)的主要區(qū)別在于 IPV6_V6ONLY
是否 a) 可用,以及 b) 默認(rèn)打開或關(guān)閉.它在 Linux 上默認(rèn)關(guān)閉(即允許沒有 setsockopt 的雙棧套接字),并在大多數(shù)其他系統(tǒng)上打開.
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 堆棧不支持該選項.在這些情況下,您需要創(chuàng)建兩個單獨的服務(wù)器套接字,并將它們放入 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.
這篇關(guān)于如何同時支持 IPv4 和 IPv6 連接的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!