Guest User

Dual Stack IPv6 test Server

a guest
May 26th, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <WinSock2.h>
  2. #include <WS2tcpip.h>
  3. #include <iostream>
  4. #pragma comment( lib, "WS2_32.lib" )
  5.  
  6. using namespace std;
  7.  
  8. int status;
  9.  
  10. int main(int argc, char* argv[])
  11. {
  12.     WSADATA WsaData;
  13.     if(WSAStartup( MAKEWORD(2,2), &WsaData ) != NO_ERROR)
  14.     {
  15.         cout << "Could not initialize sockets!\n";
  16.     }
  17.  
  18.     int sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  19.  
  20.     char option = '0';
  21.     status = setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &option, sizeof option);
  22.  
  23.     if(status != 0)
  24.     {
  25.         printf("Could not set socket to dual-stack mode: %d\n", status);
  26.     }
  27.  
  28.     struct sockaddr_in6 localAddr;
  29.  
  30.     memset(&localAddr, 0, sizeof(localAddr));
  31.     localAddr.sin6_family = AF_INET6;
  32.     localAddr.sin6_port   = htons(28960);
  33.     localAddr.sin6_addr   = in6addr_any;
  34.  
  35.     status = bind(sock, (struct sockaddr *)&localAddr, sizeof(localAddr));
  36.  
  37.     if(status != 0)
  38.     {
  39.         printf("Could not bind socket on address 28960: %d\n", status);
  40.     }
  41.  
  42.     unsigned char packetData[256];
  43.     unsigned int maxPacketSize = sizeof( packetData );
  44.     struct sockaddr_in6 fromAddr;
  45.     int fromAddrLength = sizeof( fromAddr );
  46.     while(true)
  47.     {
  48.         int received_bytes = recvfrom( sock, (char*)packetData, maxPacketSize,
  49.                                        0, (sockaddr*)&fromAddr, &fromAddrLength );
  50.         char ip[40];
  51.         inet_ntop(fromAddr.sin6_family, &fromAddr.sin6_addr, ip, 40);
  52.         cout << ip << endl;
  53.         cout << packetData << endl;
  54.     }
  55.     WSACleanup();
  56.  
  57.     cin.get();
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment