Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WinSock2.h>
- #include <WS2tcpip.h>
- #include <iostream>
- #pragma comment( lib, "WS2_32.lib" )
- using namespace std;
- int status;
- int main(int argc, char* argv[])
- {
- WSADATA WsaData;
- if(WSAStartup( MAKEWORD(2,2), &WsaData ) != NO_ERROR)
- {
- cout << "Could not initialize sockets!\n";
- }
- int sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
- char option = '0';
- status = setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &option, sizeof option);
- if(status != 0)
- {
- printf("Could not set socket to dual-stack mode: %d\n", status);
- }
- struct sockaddr_in6 localAddr;
- memset(&localAddr, 0, sizeof(localAddr));
- localAddr.sin6_family = AF_INET6;
- localAddr.sin6_port = htons(28960);
- localAddr.sin6_addr = in6addr_any;
- status = bind(sock, (struct sockaddr *)&localAddr, sizeof(localAddr));
- if(status != 0)
- {
- printf("Could not bind socket on address 28960: %d\n", status);
- }
- unsigned char packetData[256];
- unsigned int maxPacketSize = sizeof( packetData );
- struct sockaddr_in6 fromAddr;
- int fromAddrLength = sizeof( fromAddr );
- while(true)
- {
- int received_bytes = recvfrom( sock, (char*)packetData, maxPacketSize,
- 0, (sockaddr*)&fromAddr, &fromAddrLength );
- char ip[40];
- inet_ntop(fromAddr.sin6_family, &fromAddr.sin6_addr, ip, 40);
- cout << ip << endl;
- cout << packetData << endl;
- }
- WSACleanup();
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment