c++c 判断ipv6 ipv4网络是ipv4还是ipv6

【求助】怎么编程实现ipv4与ipv6的快速切换_c++吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:268,964贴子:
【求助】怎么编程实现ipv4与ipv6的快速切换收藏
就是怎么改协议版本?
不懂啊 只知道建立socket的时候可以在第一个参数里指定协议socket(PF_INET,SOCK_STREAM,0);这样就是V4socket(PF_INET4,SOCK_STREAM,0);这样就是V6
这个可以通过修改注册表么?如果可以,具体是注册表里的哪儿键?求指导
来人挽尊啊
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或c++ - ipv4 and ipv6 from any valid address - Stack Overflow
to customize your list.
Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
J it only takes a minute:
I'm trying to get both the ipv4 and ipv6 address from any string address, be it by ipv4, ipv6, or DNS address.
I can create my own function to do so, but I'm trying to take
and utilize built-in capabilities.
Is there a way to input an address string of any format, and have both
boost addresses returned?
user1382306
Getting an address from the DNS name involves... querying a naming server (DNS!). If you want to enumerate the results, use a resolver in asio:
Simple example:
#include &boost/asio.hpp&
#include &boost/function_output_iterator.hpp&
#include &set&
using boost::asio::ip::
std::set&address& unique_endpoints(std::string const& ip)
using resolver = boost::asio::ip::tcp::
boost::asio::io_ // TODO use existing service / resolver
resolver r(ios);
std::set&address&
for (auto it = r.resolve({ip, ""}); it != resolver::iterator {}; ++it)
//std::cout && "Resolved: " && it-&host_name() && " -& " && it-&endpoint() && " " && it-&service_name() && "\n";
address a = it-&endpoint().address();
if (a.is_v4())
unique.insert(boost::asio::ip::address_v6::v4_mapped(a.to_v4()));
unique.insert(a);
template &typename S&
bool endpoints_overlap(S const& a, S const& b)
bool matching_found =
std::set_intersection(
a.begin(), a.end(), b.begin(), b.end(),
boost::make_function_output_iterator([&](address const&) { matching_found = }));
return matching_
int main()
auto h = unique_endpoints("bbs2.fritz.box");
auto a = unique_endpoints("192.168.2.111");
auto b = unique_endpoints("::ffff:192.168.2.111");
auto c = unique_endpoints("::ffff:c0a8:026f");
assert(endpoints_overlap(a, b));
assert(endpoints_overlap(a, c));
assert(endpoints_overlap(b, c));
assert(endpoints_overlap(h, a));
assert(endpoints_overlap(h, b));
assert(endpoints_overlap(h, c));
Note that this test will decide that endpoints overlap when one of the DNS responses matches. This might not always be true in a cluster setting (? no expert there) and you might also want to detect broadcast addresses before using this algorithm (not tested).
Note also, I don't think there's a way to detect equivalence of actual hosts (meaning, if a host has several physical/logical NICs, they will appear as separate servers on the transport level).
Finally, in a real-world application you will want to do the resolving asynchronously (use async_resolve)
213k26262357
boost::ip::address provides this type of functionality.
You can construct an address from a string of either format(decimal for ipv4 or hexadecimal for ipv6) using the from_string function:
boost::ip::address address( boost::ip::address::from_string( myIpAddress ) );
Then you should be able to return either the v4 or v6 ip address:
boost::asio::ip::address_v4 ipv4 = address.to_v4();
boost::asio::ip::address_v6 ipv6 = address.to_v6();
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Stack Overflow works best with JavaScript enabled新手园地& & & 硬件问题Linux系统管理Linux网络问题Linux环境编程Linux桌面系统国产LinuxBSD& & & BSD文档中心AIX& & & 新手入门& & & AIX文档中心& & & 资源下载& & & Power高级应用& & & IBM存储AS400Solaris& & & Solaris文档中心HP-UX& & & HP文档中心SCO UNIX& & & SCO文档中心互操作专区IRIXTru64 UNIXMac OS X门户网站运维集群和高可用服务器应用监控和防护虚拟化技术架构设计行业应用和管理服务器及硬件技术& & & 服务器资源下载云计算& & & 云计算文档中心& & & 云计算业界& & & 云计算资源下载存储备份& & & 存储文档中心& & & 存储业界& & & 存储资源下载& & & Symantec技术交流区安全技术网络技术& & & 网络技术文档中心C/C++& & & GUI编程& & & Functional编程内核源码& & & 内核问题移动开发& & & 移动开发技术资料ShellPerlJava& & & Java文档中心PHP& & & php文档中心Python& & & Python文档中心RubyCPU与编译器嵌入式开发驱动开发Web开发VoIP开发技术MySQL& & & MySQL文档中心SybaseOraclePostgreSQLDB2Informix数据仓库与数据挖掘NoSQL技术IT业界新闻与评论IT职业生涯& & & 猎头招聘IT图书与评论& & & CU技术图书大系& & & Linux书友会二手交易下载共享Linux文档专区IT培训与认证& & & 培训交流& & & 认证培训清茶斋投资理财运动地带快乐数码摄影& & & 摄影器材& & & 摄影比赛专区IT爱车族旅游天下站务交流版主会议室博客SNS站务交流区CU活动专区& & & Power活动专区& & & 拍卖交流区频道交流区
稍有积蓄, 积分 413, 距离下一级还需 87 积分
论坛徽章:0
怎样编写ip无关的应用程序呢?书上只讲了用getaddrinfo(),gethostinfo(),小弟初学,求高人给详个较为清楚详细的办法吧,谢谢各位了!
[ 本帖最后由 Jass 于
15:38 编辑 ]
&&nbsp|&&nbsp&&nbsp|&&nbsp&&nbsp|&&nbsp&&nbsp|&&nbsp
稍有积蓄, 积分 413, 距离下一级还需 87 积分
论坛徽章:0
求助:编写Ipv4、Ipv6兼容的网络程序方法
版上大牛肯定有做过Ipv4和IPv6兼容的网络程序的吧。
小弟初学,牛看了一两本书,不甚了了,求牛人指点迷津啊
家境小康, 积分 1262, 距离下一级还需 738 积分
论坛徽章:0
稍有积蓄, 积分 413, 距离下一级还需 87 积分
论坛徽章:0
能不能劳驾说得详细一些呢,呵呵。
不好意思初学者,而且对ACE只是听说却不了解。
另外最好能用C语言实现一个简单的思路
谢谢啦!!
论坛徽章:0
回复 4楼 Jass 的帖子
&UNIX Network Programming&这本书上讲得有. 好书啊好书
稍有积蓄, 积分 413, 距离下一级还需 87 积分
论坛徽章:0
嗯 我读了他的第十二章 IPv4与IPv6的互操作性
但是呢 呵呵 恕我能力不高 只是有了一个使用getaddrinfo和getnameinfo的印象
并不清楚要怎么编写出ip无关的程序来
楼上的大哥能不能指点一下,提示一个框架出来
比方说要作哪些哪些工作,编写哪些哪些函数,就可以了
呵呵 谢谢!!
论坛徽章:0
那么请问何为 “IP 无关”,或者 “IP independent”?
稍有积蓄, 积分 413, 距离下一级还需 87 积分
论坛徽章:0
回复 7楼 langue 的帖子
就是写好的程序,拿到ipv4的机器和ipv6的机器上都不影响C++在windows下获取本地主机ipv4地址和ipv6地址
0人收藏此代码,
C++在windows下获取本地主机ipv4地址和ipv6地址
#include &Winsock2.h&
#include &stdio.h&
#include &iostream&
#include &cstring&
#include&ws2tcpip.h&
#pragma comment(lib, &ws2_32.lib &)
//linking to the library
int get_ip()
struct addrinfo *ailist, *
struct sockaddr_in6 *sinp6;
char hostname[255] = {0};
char *port = &3294&;
const char *
gethostname(hostname, sizeof(hostname));
if((hostinfo = gethostbyname(hostname)) == NULL)
//获得本地ipv4地址
errno = GetLastError();
fprintf(stderr,&gethostbyname Error:%d\n&, errno);
while(*(hostinfo-&h_addr_list) != NULL)
//输出ipv4地址
ip = inet_ntoa(*(struct in_addr *) *hostinfo-&h_addr_list);
printf(&ipv4 addr = %s\n\n&, ip);
hostinfo-&h_addr_list++;
hint.ai_family = AF_INET6;
hint 的限定设置
hint.ai_socktype = SOCK_STREAM;
这里可是设置 socket type
SOCK——DGRAM */
hint.ai_flags = AI_PASSIVE;
// flags 的标志很多
。常用的有AI_CANONNAME;
hint.ai_protocol = 0;
一般为0,默认 */
hint.ai_addrlen = 0;
下面不可以设置,为0,或者为NULL
hint.ai_canonname = NULL;
hint.ai_addr = NULL;
hint.ai_next = NULL;
ilRc = getaddrinfo(hostname, port, &hint, &ailist);
/*通过主机名获得地址信息*/
if (ilRc & 0)
char str_error[100];
strcpy(str_error, (char *)gai_strerror(errno));
printf(&str_error = %s&, str_error);
if(ailist == NULL)
printf(&sorry not find the IP address,please try again \n&);
for (aip = aip != NULL; aip = aip-&ai_next)
/* 显示获取的信息
aip-&ai_family == AF_INET6;
sinp6 = (struct sockaddr_in6 *)aip-&ai_
/* 为什么是for 循环 ,先向下看 */
printf(&ipv6 addr = &);
for(i = 0; i & 16; i++)
if(((i-1)%2) && (i&0))
printf(&:&);
printf(&%02x&,sinp6-&sin6_addr.u.Byte[i]);
printf(& \n&);
printf(& \n&);
int main(){
WORD wVersionR
WSADATA wsaD
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );//initiate the ws2_32.dll and match the version
if ( err != 0 )
if ( LOBYTE( wsaData.wVersion ) != 1 ||
//if the version is not matched ,then quit and terminate the ws3_32.dll
HIBYTE( wsaData.wVersion ) != 1 )
WSACleanup( );
//该代码片段来自于: /codes/cpp/5958
相关代码片段:
最新C/C++代码片段
合作网站:

我要回帖

更多关于 c 判断ipv6 ipv4 的文章

 

随机推荐