Skip to content

Instantly share code, notes, and snippets.

@amurzeau amurzeau/CMakeLists.txt Secret
Last active May 22, 2016

Embed
What would you like to do?
// Compile this to client.exe and execute after server.exe
#include <winsock2.h>
#include <ws2tcpip.h>
#include <mstcpip.h>
#include <stdio.h>
#ifndef SIO_LOOPBACK_FAST_PATH
# define SIO_LOOPBACK_FAST_PATH 0x98000010
#endif
#define assertp(_Expression) \
if(!(_Expression)) { \
printf("assertp failed at %s:%d: %s\n", __FILE__, __LINE__, #_Expression); \
abort(); \
}
int main() {
WSADATA wsa;
const int PING_LOOPS = 1000000;
SOCKET inputSocket;
struct sockaddr_in6 sin;
char buffer[1024] = "PING\n";
int flag;
int r;
int opt;
int i;
WSAStartup(MAKEWORD(2, 2), &wsa);
// Setup client
inputSocket = socket(AF_INET6, SOCK_STREAM, 0);
assertp(inputSocket != INVALID_SOCKET);
opt = 0;
setsockopt(inputSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&opt, sizeof(opt));
memset(&sin, 0, sizeof(sin));
sin.sin6_addr = in6addr_any;
sin.sin6_port = htons(0);
sin.sin6_family = AF_INET6;
assertp(bind(inputSocket, (struct sockaddr *) &sin, sizeof(sin)) == 0);
flag = 1;
assertp(WSAIoctl(inputSocket,
SIO_LOOPBACK_FAST_PATH,
&flag,
sizeof(flag),
NULL,
0,
&r,
NULL,
NULL) == 0);
memset(&sin, 0, sizeof(sin));
sin.sin6_addr = in6addr_loopback;
sin.sin6_port = htons(9123);
sin.sin6_family = AF_INET6;
assertp(connect(inputSocket, (struct sockaddr *) &sin, sizeof(sin)) == 0);
for(i = 0; i < PING_LOOPS; i++) {
send(inputSocket, buffer, sizeof(buffer), 0);
recv(inputSocket, buffer, sizeof(buffer), 0);
//printf("%s", buffer);
}
printf("Done\n");
closesocket(inputSocket);
WSACleanup();
return 0;
}
# To compile:
# mkdir build && cd build
# cmake ..
# cmake --build .
# (This will build server.c to server.exe and client.c to client.exe with ws2_32.lib and iphlpapi.lib)
#
# Then run server.exe then client.exe
#
cmake_minimum_required(VERSION 2.8.12)
project(test_fastpath)
add_executable(server server.c)
add_executable(client client.c)
target_link_libraries(server ws2_32.lib)
target_link_libraries(client ws2_32.lib)
target_link_libraries(server iphlpapi.lib)
target_link_libraries(client iphlpapi.lib)
// Compile this to server.exe and execute before client.exe
#include <winsock2.h>
#include <ws2tcpip.h>
#include <mstcpip.h>
#include <stdio.h>
#ifndef SIO_LOOPBACK_FAST_PATH
# define SIO_LOOPBACK_FAST_PATH 0x98000010
#endif
#define assertp(_Expression) \
if(!(_Expression)) { \
printf("assertp failed at %s:%d: %s\n", __FILE__, __LINE__, #_Expression); \
abort(); \
}
int main() {
WSADATA wsa;
const int PING_LOOPS = 1000;
SOCKET outputServerSocket, outputSocket;
struct sockaddr_in6 sin;
struct sockaddr_in6 csin;
char buffer[1024];
int flag;
int r;
WSAStartup(MAKEWORD(2, 2), &wsa);
// Setup socket server
outputServerSocket = socket(AF_INET6, SOCK_STREAM, 0);
assertp(outputServerSocket != INVALID_SOCKET);
memset(&sin, 0, sizeof(sin));
sin.sin6_addr = in6addr_any;
sin.sin6_port = htons(9123);
sin.sin6_family = AF_INET6;
assertp(bind(outputServerSocket, (struct sockaddr *) &sin, sizeof(sin)) == 0);
flag = 1;
assertp(WSAIoctl(outputServerSocket,
SIO_LOOPBACK_FAST_PATH,
&flag,
sizeof(flag),
NULL,
0,
&r,
NULL,
NULL) == 0);
assertp(listen(outputServerSocket, 1) == 0);
// Expect a connection on the server @ port 4800
while(1) {
socklen_t csinSize = sizeof(csin);
outputSocket = accept(outputServerSocket, (struct sockaddr *) &csin, &csinSize);
assertp(outputSocket != INVALID_SOCKET);
for(;;) {
r = recv(outputSocket, buffer, sizeof(buffer), 0);
if(r <= 0)
break;
send(outputSocket, buffer, r, 0);
}
printf("Done with last error: %d\n", WSAGetLastError());
closesocket(outputSocket);
}
closesocket(outputServerSocket);
WSACleanup();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.