This commit is contained in:
Manoj Ampalam 2016-01-18 22:29:15 -08:00
parent 118d457d39
commit 733d09e249
7 changed files with 662 additions and 0 deletions

View File

@ -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.
/////////////////////////////////////////////////////////////////////////////

View File

@ -0,0 +1,462 @@
#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdlib.h>
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();
}

View File

@ -0,0 +1,93 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{D52F8255-C3A9-4416-A0A6-8CE63A4D7E43}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>SampleServer</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>C:\openssh\Win32-OpenSSH_\contrib\win32\w32-posix-prototype\win32posix\Debug\win32posix.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="SampleServer.cpp" />
<ClCompile Include="stdafx.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="stdafx.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="targetver.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="SampleServer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -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

View File

@ -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 <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here

View File

@ -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 <SDKDDKVer.h>