Created
February 21, 2018 19:59
-
-
Save pps83/3ccadae391d7e612de13605125253d1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _WS2DEF_ | |
#include "inaddr.h" | |
#include "MSTcpIP.h" | |
void startServer(); | |
void startClient_6_to_4(short xxport); | |
void startClient_4_to_4(short xxport); | |
void startClient_6_to_6(short xxport); | |
short xxport = 8112; | |
bool useV4 = false; | |
void startServer() | |
{ | |
WSADATA wsaData; | |
SOCKET lsock = INVALID_SOCKET; | |
SOCKET csock = INVALID_SOCKET; | |
SOCKADDR_STORAGE serverAddr = { 0 }; | |
SOCKADDR_STORAGE clientAddr = { 0 }; | |
int off = 0; | |
int port = xxport; | |
int clientAddrLen = sizeof(clientAddr); | |
WSAStartup(MAKEWORD(2, 2), &wsaData); | |
serverAddr.ss_family = AF_INET6; | |
INETADDR_SETANY((SOCKADDR *)&serverAddr); | |
SS_PORT(&serverAddr) = htons((unsigned short)port); | |
lsock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); | |
//make the socket a dual mode socket | |
setsockopt(lsock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&off, sizeof(off)); | |
bind(lsock, (SOCKADDR *)&serverAddr, (int)INET_SOCKADDR_LENGTH(serverAddr.ss_family)); | |
listen(lsock, 5); | |
csock = accept(lsock, (SOCKADDR *)&clientAddr, &clientAddrLen); | |
char str[128]; | |
if (clientAddr.ss_family = AF_INET6) | |
inet_ntop(clientAddr.ss_family, &((sockaddr_in6*)&clientAddr)->sin6_addr, str, 128); | |
else | |
inet_ntop(clientAddr.ss_family, &((sockaddr_in*)&clientAddr)->sin_addr, str, 128); | |
printf("accepted connection from \"%s\"\n", str); | |
WSACleanup(); | |
} | |
void ConvertToV4MappedAddressIfNeeded(PSOCKADDR pAddr) | |
{ | |
// if v4 address, convert to v4 mapped v6 address | |
if (AF_INET == pAddr->sa_family) | |
{ | |
IN_ADDR In4addr; | |
SCOPE_ID scope = INETADDR_SCOPE_ID(pAddr); | |
USHORT port = INETADDR_PORT(pAddr); | |
In4addr = *(IN_ADDR*)INETADDR_ADDRESS(pAddr); | |
ZeroMemory(pAddr, sizeof(SOCKADDR_STORAGE)); | |
IN6ADDR_SETV4MAPPED( | |
(PSOCKADDR_IN6)pAddr, | |
&In4addr, | |
scope, | |
port | |
); | |
} | |
} | |
void startClient_6_to_4(short xxport) | |
{ | |
WSADATA wsaData; | |
SOCKET csock = INVALID_SOCKET; | |
SOCKADDR_STORAGE addrLoopback4 = { 0 }; | |
int port = xxport; | |
int off = 0; | |
addrLoopback4.ss_family = AF_INET; | |
INETADDR_SETLOOPBACK((SOCKADDR*)&addrLoopback4); | |
SS_PORT(&addrLoopback4) = htons((short)port); | |
WSAStartup(MAKEWORD(2, 2), &wsaData); | |
if ((csock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) | |
{ | |
//error creating the listening socket | |
WSACleanup(); | |
return; | |
} | |
//make the socket a dual mode socket | |
setsockopt(csock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&off, sizeof(off)); | |
// format the address as a v4 mapped address if needed | |
ConvertToV4MappedAddressIfNeeded((SOCKADDR *)&addrLoopback4); | |
// connect | |
if (0 == connect(csock, (SOCKADDR *)&addrLoopback4, sizeof(addrLoopback4))) | |
{ | |
printf("Connect complete from v6\n"); | |
} | |
else | |
{ | |
printf("Connect from v6 failed err:%d\n", WSAGetLastError()); | |
} | |
closesocket(csock); | |
WSACleanup(); | |
} | |
void startClient_4_to_4(short xxport) | |
{ | |
WSADATA wsaData; | |
SOCKET csock = INVALID_SOCKET; | |
SOCKADDR_STORAGE addrLoopback4 = { 0 }; | |
int port = xxport; | |
int off = 0; | |
addrLoopback4.ss_family = AF_INET; | |
INETADDR_SETLOOPBACK((SOCKADDR*)&addrLoopback4); | |
SS_PORT(&addrLoopback4) = htons((short)port); | |
WSAStartup(MAKEWORD(2, 2), &wsaData); | |
if ((csock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) | |
{ | |
//error creating the listening socket | |
WSACleanup(); | |
return; | |
} | |
//make the socket a dual mode socket | |
//setsockopt(csock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&off, sizeof(off)); | |
// format the address as a v4 mapped address if needed | |
//ConvertToV4MappedAddressIfNeeded((SOCKADDR *)&addrLoopback4); | |
// connect | |
if (0 == connect(csock, (SOCKADDR *)&addrLoopback4, sizeof(addrLoopback4))) | |
{ | |
printf("Connect complete from v4\n"); | |
} | |
else | |
{ | |
printf("Connect from v4 failed err:%d\n", WSAGetLastError()); | |
} | |
closesocket(csock); | |
WSACleanup(); | |
} | |
void startClient_6_to_6(short xxport) | |
{ | |
WSADATA wsaData; | |
SOCKET csock = INVALID_SOCKET; | |
SOCKADDR_STORAGE addrLoopback4 = { 0 }; | |
int port = xxport; | |
int off = 0; | |
addrLoopback4.ss_family = AF_INET6; | |
INETADDR_SETLOOPBACK((SOCKADDR*)&addrLoopback4); | |
SS_PORT(&addrLoopback4) = htons((short)port); | |
WSAStartup(MAKEWORD(2, 2), &wsaData); | |
if ((csock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) | |
{ | |
//error creating the listening socket | |
WSACleanup(); | |
return; | |
} | |
//make the socket a dual mode socket | |
//setsockopt(csock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&off, sizeof(off)); | |
// format the address as a v4 mapped address if needed | |
//ConvertToV4MappedAddressIfNeeded((SOCKADDR *)&addrLoopback4); | |
// connect | |
if (0 == connect(csock, (SOCKADDR *)&addrLoopback4, sizeof(addrLoopback4))) | |
{ | |
printf("Connect complete from v6 to v6\n"); | |
} | |
else | |
{ | |
printf("Connect from v6 to v6 failed err:%d\n", WSAGetLastError()); | |
} | |
closesocket(csock); | |
WSACleanup(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment