如何支持IPv4和IPv6连接 [英] How to support both IPv4 and IPv6 connections

查看:956
本文介绍了如何支持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地址将以 IPv4映射格式显示为IPv6地址。

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连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆