diff --git a/contrib/win32/w32-posix-prototype/win32posix/SampleServer/ReadMe.txt b/contrib/win32/w32-posix-prototype/win32posix/SampleServer/ReadMe.txt new file mode 100644 index 0000000..a9fa7a2 --- /dev/null +++ b/contrib/win32/w32-posix-prototype/win32posix/SampleServer/ReadMe.txt @@ -0,0 +1,40 @@ +======================================================================== + CONSOLE APPLICATION : SampleServer Project Overview +======================================================================== + +AppWizard has created this SampleServer application for you. + +This file contains a summary of what you will find in each of the files that +make up your SampleServer application. + + +SampleServer.vcxproj + This is the main project file for VC++ projects generated using an Application Wizard. + It contains information about the version of Visual C++ that generated the file, and + information about the platforms, configurations, and project features selected with the + Application Wizard. + +SampleServer.vcxproj.filters + This is the filters file for VC++ projects generated using an Application Wizard. + It contains information about the association between the files in your project + and the filters. This association is used in the IDE to show grouping of files with + similar extensions under a specific node (for e.g. ".cpp" files are associated with the + "Source Files" filter). + +SampleServer.cpp + This is the main application source file. + +///////////////////////////////////////////////////////////////////////////// +Other standard files: + +StdAfx.h, StdAfx.cpp + These files are used to build a precompiled header (PCH) file + named SampleServer.pch and a precompiled types file named StdAfx.obj. + +///////////////////////////////////////////////////////////////////////////// +Other notes: + +AppWizard uses "TODO:" comments to indicate parts of the source code you +should add to or customize. + +///////////////////////////////////////////////////////////////////////////// diff --git a/contrib/win32/w32-posix-prototype/win32posix/SampleServer/SampleServer.cpp b/contrib/win32/w32-posix-prototype/win32posix/SampleServer/SampleServer.cpp new file mode 100644 index 0000000..bab74d6 --- /dev/null +++ b/contrib/win32/w32-posix-prototype/win32posix/SampleServer/SampleServer.cpp @@ -0,0 +1,462 @@ + +#undef UNICODE + +#define WIN32_LEAN_AND_MEAN + +#include +#include +extern "C" { +#include "..\win32posix\w32posix.h" +} +// Need to link with Ws2_32.lib +#pragma comment (lib, "Ws2_32.lib") +// #pragma comment (lib, "Mswsock.lib") + +#define DEFAULT_BUFLEN 512 +#define DEFAULT_PORT "27015" + +int regular() +{ + int iResult; + + int ListenSocket; + int ClientSocket; + + struct addrinfo *result = NULL; + struct addrinfo hints; + + int iSendResult; + char recvbuf[DEFAULT_BUFLEN]; + int recvbuflen = DEFAULT_BUFLEN; + + w32posix_initialize(); + + ZeroMemory(&hints, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + hints.ai_flags = AI_PASSIVE; + + // Resolve the server address and port + iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result); + if (iResult != 0) { + printf("getaddrinfo failed with error: %d\n", iResult); + w32posix_done(); + return 1; + } + + // Create a SOCKET for connecting to server + ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); + if (ListenSocket == -1) { + printf("socket failed with error: %ld\n", errno); + freeaddrinfo(result); + w32posix_done(); + return 1; + } + + // Setup the TCP listening socket + iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); + if (iResult == -1) { + printf("bind failed with error: %d\n", errno); + freeaddrinfo(result); + close(ListenSocket); + w32posix_done(); + return 1; + } + + freeaddrinfo(result); + + iResult = listen(ListenSocket, SOMAXCONN); + if (iResult == -1) { + printf("listen failed with error: %d\n", errno); + close(ListenSocket); + w32posix_done(); + return 1; + } + + // Accept a client socket + ClientSocket = accept(ListenSocket, NULL, NULL); + if (ClientSocket == -1) { + printf("accept failed with error: %d\n", errno); + close(ListenSocket); + w32posix_done(); + return 1; + } + + // No longer need server socket + close(ListenSocket); + + // Receive until the peer shuts down the connection + do { + + iResult = recv(ClientSocket, recvbuf, recvbuflen, 0); + if (iResult > 0) { + printf("Bytes received: %d\n", iResult); + recvbuf[iResult] = '\0'; + printf("%s\n", recvbuf); + + // Echo the buffer back to the sender + iSendResult = send(ClientSocket, recvbuf, iResult, 0); + if (iSendResult == -1) { + printf("send failed with error: %d\n", errno); + close(ClientSocket); + w32posix_done(); + return 1; + } + printf("Bytes sent: %d\n", iSendResult); + } + else if (iResult == 0) + printf("Connection closing...\n"); + else { + printf("recv failed with error: %d\n", errno); + close(ClientSocket); + w32posix_done(); + return 1; + } + + } while (iResult > 0); + + // shutdown the connection since we're done + iResult = shutdown(ClientSocket, SD_SEND); + if (iResult == -1) { + printf("shutdown failed with error: %d\n", errno); + iSendResult = send(ClientSocket, recvbuf, iResult, 0); + if (iSendResult == -1) + printf("send failed with error: %d\n", errno); + close(ClientSocket); + w32posix_done(); + return 1; + } + + // cleanup + close(ClientSocket); + w32posix_done(); + + return 0; +} + +int async() +{ + int iResult; + + int ListenSocket; + int ClientSocket; + + struct addrinfo *result = NULL; + struct addrinfo hints; + + int iSendResult; + char recvbuf[DEFAULT_BUFLEN]; + int recvbuflen = DEFAULT_BUFLEN; + + w32posix_initialize(); + + ZeroMemory(&hints, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + hints.ai_flags = AI_PASSIVE; + + // Resolve the server address and port + iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result); + if (iResult != 0) { + printf("getaddrinfo failed with error: %d\n", iResult); + w32posix_done(); + return 1; + } + + // Create a SOCKET for connecting to server + ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); + if (ListenSocket == -1) { + printf("socket failed with error: %ld\n", errno); + freeaddrinfo(result); + w32posix_done(); + return 1; + } + + // Setup the TCP listening socket + iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); + if (iResult == -1) { + printf("bind failed with error: %d\n", errno); + freeaddrinfo(result); + close(ListenSocket); + w32posix_done(); + return 1; + } + + freeaddrinfo(result); + + iResult = listen(ListenSocket, SOMAXCONN); + if (iResult == -1) { + printf("listen failed with error: %d\n", errno); + close(ListenSocket); + w32posix_done(); + return 1; + } + + fd_set readset; + memset(&readset, 0, sizeof(fd_set)); + FD_SET(ListenSocket, &readset); + + timeval time; + time.tv_sec = 60 * 60; + if (-1 == select(ListenSocket, &readset, NULL, NULL, &time)) + { + printf("select call failed"); + close(ListenSocket); + w32posix_done(); + return 1; + } + + if (!FD_ISSET(ListenSocket, &readset)) + printf("expected that fd is set"); + + // Accept a client socket + ClientSocket = accept(ListenSocket, NULL, NULL); + if (ClientSocket == -1) { + printf("accept failed with error: %d\n", errno); + close(ListenSocket); + w32posix_done(); + return 1; + } + + // No longer need server socket + close(ListenSocket); + + int fd_flags = fcntl(ClientSocket, F_GETFL); + fcntl(ClientSocket, F_SETFL, fd_flags | O_NONBLOCK); + + + // Receive until the peer shuts down the connection + do { + + memset(&readset, 0, sizeof(fd_set)); + FD_SET(ClientSocket, &readset); + if (-1 == select(ClientSocket, &readset, NULL, NULL, &time)) + { + printf("select call failed"); + close(ListenSocket); + w32posix_done(); + return 1; + } + + iResult = recv(ClientSocket, recvbuf, recvbuflen, 0); + if (iResult > 0) { + printf("Bytes received: %d\n", iResult); + recvbuf[iResult] = '\0'; + printf("%s\n", recvbuf); + + // Echo the buffer back to the sender + iSendResult = send(ClientSocket, recvbuf, iResult, 0); + if (iSendResult == -1) { + printf("send failed with error: %d\n", errno); + close(ClientSocket); + w32posix_done(); + return 1; + } + printf("Bytes sent: %d\n", iSendResult); + } + else if (iResult == 0) + printf("Connection closing...\n"); + else { + printf("recv failed with error: %d\n", errno); + close(ClientSocket); + w32posix_done(); + return 1; + } + + } while (iResult > 0); + + // shutdown the connection since we're done + iResult = shutdown(ClientSocket, SD_SEND); + if (iResult == -1) { + printf("shutdown failed with error: %d\n", errno); + close(ClientSocket); + w32posix_done(); + return 1; + } + + // cleanup + close(ClientSocket); + w32posix_done(); + + return 0; +} + + +#undef DEFAULT_BUFLEN +#define DEFAULT_BUFLEN 1024*1024 +BOOL writemode; +int throughput() +{ + int iResult; + + int ListenSocket; + int ClientSocket; + + struct addrinfo *result = NULL; + struct addrinfo hints; + + char *recvbuf = (char*)malloc(DEFAULT_BUFLEN); + int recvbuflen = DEFAULT_BUFLEN; + + w32posix_initialize(); + + ZeroMemory(&hints, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + hints.ai_flags = AI_PASSIVE; + + // Resolve the server address and port + iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result); + if (iResult != 0) { + printf("getaddrinfo failed with error: %d\n", iResult); + w32posix_done(); + return 1; + } + + // Create a SOCKET for connecting to server + ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); + if (ListenSocket == -1) { + printf("socket failed with error: %ld\n", errno); + freeaddrinfo(result); + w32posix_done(); + return 1; + } + + // Setup the TCP listening socket + iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); + if (iResult == -1) { + printf("bind failed with error: %d\n", errno); + freeaddrinfo(result); + close(ListenSocket); + w32posix_done(); + return 1; + } + + freeaddrinfo(result); + + iResult = listen(ListenSocket, SOMAXCONN); + if (iResult == -1) { + printf("listen failed with error: %d\n", errno); + close(ListenSocket); + w32posix_done(); + return 1; + } + + // Accept a client socket + ClientSocket = accept(ListenSocket, NULL, NULL); + if (ClientSocket == -1) { + printf("accept failed with error: %d\n", errno); + close(ListenSocket); + w32posix_done(); + return 1; + } + + // No longer need server socket + close(ListenSocket); + + double totalbytes = 0; + + // Receive until the peer shuts down the connection + if (writemode) + { + char *sendbuf = (char*)malloc(DEFAULT_BUFLEN); + int sendbuflen = DEFAULT_BUFLEN; + + while (totalbytes < 50000 * 1024 * 1024) + { + iResult = send(ClientSocket, sendbuf, sendbuflen, 0); + if (iResult == SOCKET_ERROR) { + printf("send failed with error: %d\n", WSAGetLastError()); + close(ClientSocket); + w32posix_done(); + return 1; + } + totalbytes += iResult; + } + + printf("send %f bytes\n", totalbytes); + // shutdown the connection since no more data will be sent + iResult = shutdown(ClientSocket, SD_SEND); + if (iResult == SOCKET_ERROR) { + printf("shutdown failed with error: %d\n", WSAGetLastError()); + close(ClientSocket); + w32posix_done(); + return 1; + } + } + else + { + do { + + iResult = recv(ClientSocket, recvbuf, recvbuflen, 0); + if (iResult > 0) { + totalbytes += iResult; + } + else if (iResult == 0) + printf("Connection closing...\n"); + else { + printf("recv failed with error: %d\n", errno); + close(ClientSocket); + w32posix_done(); + return 1; + } + + } while (iResult > 0); + + printf("Received total bytes %f\n", totalbytes); + } + + // cleanup + close(ClientSocket); + w32posix_done(); + + return 0; +} + +int pipetest() +{ + int pipefds[2]; + if (-1 == pipe(pipefds)) + { + printf("creating pipe failed %d\n", errno); + return -1; + } + + int readfd = pipefds[0]; + int writefd = pipefds[1]; + char* buf = "test characters to write"; + char readbuf[512]; + + int written = write(writefd, buf, strlen(buf)); + if (written == -1) { + printf("write to pipe failed %d \n", errno); + close(readfd); + close(writefd); + return -1; + } + + int rd = read(readfd, readbuf, 512); + if (rd == -1) { + printf("reading from pipe failed %d \n", errno); + close(readfd); + close(writefd); + return -1; + } + + close(readfd); + close(writefd); + return 0; +} + + +int __cdecl main(void) +{ + //return regular(); + //return async(); + writemode = TRUE; + //return throughput(); + return pipetest(); +} diff --git a/contrib/win32/w32-posix-prototype/win32posix/SampleServer/SampleServer.vcxproj b/contrib/win32/w32-posix-prototype/win32posix/SampleServer/SampleServer.vcxproj new file mode 100644 index 0000000..e693d7d --- /dev/null +++ b/contrib/win32/w32-posix-prototype/win32posix/SampleServer/SampleServer.vcxproj @@ -0,0 +1,93 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {D52F8255-C3A9-4416-A0A6-8CE63A4D7E43} + Win32Proj + SampleServer + + + + Application + true + v120 + Unicode + + + Application + false + v120 + true + Unicode + + + + + + + + + + + + + true + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + + + Console + true + C:\openssh\Win32-OpenSSH_\contrib\win32\w32-posix-prototype\win32posix\Debug\win32posix.lib;%(AdditionalDependencies) + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + + + Console + true + true + true + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/contrib/win32/w32-posix-prototype/win32posix/SampleServer/SampleServer.vcxproj.filters b/contrib/win32/w32-posix-prototype/win32posix/SampleServer/SampleServer.vcxproj.filters new file mode 100644 index 0000000..1ef5347 --- /dev/null +++ b/contrib/win32/w32-posix-prototype/win32posix/SampleServer/SampleServer.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + + + + Header Files + + + Header Files + + + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/contrib/win32/w32-posix-prototype/win32posix/SampleServer/stdafx.cpp b/contrib/win32/w32-posix-prototype/win32posix/SampleServer/stdafx.cpp new file mode 100644 index 0000000..79dba01 --- /dev/null +++ b/contrib/win32/w32-posix-prototype/win32posix/SampleServer/stdafx.cpp @@ -0,0 +1,8 @@ +// stdafx.cpp : source file that includes just the standard includes +// SampleServer.pch will be the pre-compiled header +// stdafx.obj will contain the pre-compiled type information + +#include "stdafx.h" + +// TODO: reference any additional headers you need in STDAFX.H +// and not in this file diff --git a/contrib/win32/w32-posix-prototype/win32posix/SampleServer/stdafx.h b/contrib/win32/w32-posix-prototype/win32posix/SampleServer/stdafx.h new file mode 100644 index 0000000..b005a83 --- /dev/null +++ b/contrib/win32/w32-posix-prototype/win32posix/SampleServer/stdafx.h @@ -0,0 +1,15 @@ +// stdafx.h : include file for standard system include files, +// or project specific include files that are used frequently, but +// are changed infrequently +// + +#pragma once + +#include "targetver.h" + +#include +#include + + + +// TODO: reference additional headers your program requires here diff --git a/contrib/win32/w32-posix-prototype/win32posix/SampleServer/targetver.h b/contrib/win32/w32-posix-prototype/win32posix/SampleServer/targetver.h new file mode 100644 index 0000000..87c0086 --- /dev/null +++ b/contrib/win32/w32-posix-prototype/win32posix/SampleServer/targetver.h @@ -0,0 +1,8 @@ +#pragma once + +// Including SDKDDKVer.h defines the highest available Windows platform. + +// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and +// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. + +#include