int m_nSocket6;
sockaddr_in6 m_address_6;
/*created the socket*/
m_nSocket6 = socket(AF_INET6, SOCK_DGRAM, 0);
int ret;
/* Set socket buffer size */
int buffer_size;
ret = setsockopt(m_nSocket, SOL_SOCKET, SO_RCVBUF, (char*) &buffer_size, sizeof(buffer_size));
/* Set socket timeout */
int timeout = m_nTimeout;
ret = setsockopt(m_nSocket, SOL_SOCKET, SO_RCVTIMEO, (char*) &timeout, sizeof(timeout));
/*Multicast*/
int yes = m_nMulticast; //1 = enabled
ret = setsockopt(m_nSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof(yes));
/*bind*/
memset((char *)&abc, 0, sizeof(m_address6));
m_address6.ss_family = AF_UNSPEC;
m_address6.sin6_family = AF_INET6;
m_address_6.sin6_addr = in6addr_any;
m_address6.sin6_port = htons((u_short)m_nPort);
ret = bind(m_nSocket6, (struct sockaddr*) &abc, sizeof(abc));
/*receiving the packets*/
recvfrom(m_nSocket, m_sBuffer, UPD_DATAGRAM_BUFFER_SIZE, 0, (struct sockaddr*) &m_address, &server_length);
答案 0 :(得分:2)
为了使用单个套接字处理两者 IPv4和IPv6流量,您必须创建一个双栈套接字,这是一个具有{ {1}}选项已停用。
有关详细信息,请参阅MSDN文档:
Dual-Stack Sockets for IPv6 Winsock Applications
为了在带有Service Pack 1(SP1)的Windows XP和Windows Server 2003上同时支持IPv4和IPv6,应用程序必须创建两个套接字,一个用于IPv4的套接字和一个用于IPv6的套接字。这两个插座必须由应用程序单独处理。
Windows Vista及更高版本提供了创建可以处理IPv6和IPv4流量的单个IPv6套接字的功能。例如,创建用于IPv6的TCP侦听套接字,将其置于双堆栈模式,并绑定到端口5001.此双栈套接字可以接受来自连接到端口5001的IPv6 TCP客户端和连接到端口5001的IPv4 TCP客户端的连接。此功能可以大大简化应用程序设计,并减少在两个单独的套接字上发布操作所需的资源开销。