Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit 78f7ce5

Browse files
author
Oleksandr
committed
Add project files.
1 parent 68be9cb commit 78f7ce5

14 files changed

+742
-0
lines changed

Client/Client.vcxproj

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
</ItemGroup>
13+
<PropertyGroup Label="Globals">
14+
<ProjectGuid>{60BA9FBE-D2BA-4422-AF9A-652FF009EA46}</ProjectGuid>
15+
<Keyword>Win32Proj</Keyword>
16+
<RootNamespace>Client</RootNamespace>
17+
</PropertyGroup>
18+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
19+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
20+
<ConfigurationType>Application</ConfigurationType>
21+
<UseDebugLibraries>true</UseDebugLibraries>
22+
<PlatformToolset>v120</PlatformToolset>
23+
<CharacterSet>Unicode</CharacterSet>
24+
</PropertyGroup>
25+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
26+
<ConfigurationType>Application</ConfigurationType>
27+
<UseDebugLibraries>false</UseDebugLibraries>
28+
<PlatformToolset>v120</PlatformToolset>
29+
<WholeProgramOptimization>true</WholeProgramOptimization>
30+
<CharacterSet>Unicode</CharacterSet>
31+
</PropertyGroup>
32+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
33+
<ImportGroup Label="ExtensionSettings">
34+
</ImportGroup>
35+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
36+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
37+
</ImportGroup>
38+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
39+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
40+
</ImportGroup>
41+
<PropertyGroup Label="UserMacros" />
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
43+
<LinkIncremental>true</LinkIncremental>
44+
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(BOOST_ROOT);</IncludePath>
45+
<LibraryPath>$(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86);$(BOOST_ROOT)\lib32-msvc-12.0;</LibraryPath>
46+
</PropertyGroup>
47+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
48+
<LinkIncremental>false</LinkIncremental>
49+
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(BOOST_ROOT);</IncludePath>
50+
<LibraryPath>$(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86);$(BOOST_ROOT)\lib32-msvc-12.0;</LibraryPath>
51+
</PropertyGroup>
52+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
53+
<ClCompile>
54+
<PrecompiledHeader>
55+
</PrecompiledHeader>
56+
<WarningLevel>Level3</WarningLevel>
57+
<Optimization>Disabled</Optimization>
58+
<PreprocessorDefinitions>WIN32;_WIN32_WINNT=0x0601;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
59+
</ClCompile>
60+
<Link>
61+
<SubSystem>Console</SubSystem>
62+
<GenerateDebugInformation>true</GenerateDebugInformation>
63+
</Link>
64+
</ItemDefinitionGroup>
65+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
66+
<ClCompile>
67+
<WarningLevel>Level3</WarningLevel>
68+
<PrecompiledHeader>
69+
</PrecompiledHeader>
70+
<Optimization>MaxSpeed</Optimization>
71+
<FunctionLevelLinking>true</FunctionLevelLinking>
72+
<IntrinsicFunctions>true</IntrinsicFunctions>
73+
<PreprocessorDefinitions>WIN32;_WIN32_WINNT=0x0601;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
74+
</ClCompile>
75+
<Link>
76+
<SubSystem>Console</SubSystem>
77+
<GenerateDebugInformation>true</GenerateDebugInformation>
78+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
79+
<OptimizeReferences>true</OptimizeReferences>
80+
</Link>
81+
</ItemDefinitionGroup>
82+
<ItemGroup>
83+
<ClCompile Include="client.cpp" />
84+
<ClCompile Include="..\Common\logger.cpp" />
85+
<ClCompile Include="main.cpp" />
86+
</ItemGroup>
87+
<ItemGroup>
88+
<ClInclude Include="client.h" />
89+
<ClInclude Include="..\Common\logger.h" />
90+
</ItemGroup>
91+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
92+
<ImportGroup Label="ExtensionTargets">
93+
</ImportGroup>
94+
</Project>

Client/Client.vcxproj.filters

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClCompile Include="main.cpp">
19+
<Filter>Source Files</Filter>
20+
</ClCompile>
21+
<ClCompile Include="client.cpp">
22+
<Filter>Source Files</Filter>
23+
</ClCompile>
24+
<ClCompile Include="..\Common\logger.cpp">
25+
<Filter>Source Files</Filter>
26+
</ClCompile>
27+
</ItemGroup>
28+
<ItemGroup>
29+
<ClInclude Include="client.h">
30+
<Filter>Header Files</Filter>
31+
</ClInclude>
32+
<ClInclude Include="..\Common\logger.h">
33+
<Filter>Header Files</Filter>
34+
</ClInclude>
35+
</ItemGroup>
36+
</Project>

Client/client.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <string>
2+
#include <iostream>
3+
4+
#include <boost/filesystem/path.hpp>
5+
#include <boost/log/trivial.hpp>
6+
7+
#include "client.h"
8+
9+
10+
Client::Client(IoService& t_ioService, TcpResolverIterator t_endpointIterator,
11+
std::string const& t_path)
12+
: m_ioService(t_ioService), m_socket(t_ioService),
13+
m_endpointIterator(t_endpointIterator), m_path(t_path)
14+
{
15+
doConnect();
16+
openFile(m_path);
17+
}
18+
19+
20+
void Client::openFile(std::string const& t_path)
21+
{
22+
m_sourceFile.open(t_path, std::ios_base::binary | std::ios_base::ate);
23+
if (m_sourceFile.fail())
24+
throw std::fstream::failure("Failed while opening file " + t_path);
25+
26+
m_sourceFile.seekg(0, m_sourceFile.end);
27+
auto fileSize = m_sourceFile.tellg();
28+
m_sourceFile.seekg(0, m_sourceFile.beg);
29+
30+
std::ostream requestStream(&m_request);
31+
boost::filesystem::path p(t_path);
32+
requestStream << p.filename().string() << "\n" << fileSize << "\n\n";
33+
BOOST_LOG_TRIVIAL(trace) << "Request size: " << m_request.size();
34+
}
35+
36+
37+
void Client::doConnect()
38+
{
39+
boost::asio::async_connect(m_socket, m_endpointIterator,
40+
[this](boost::system::error_code ec, TcpResolverIterator)
41+
{
42+
if (!ec) {
43+
writeBuffer(m_request);
44+
} else {
45+
std::cout << "Coudn't connect to host. Please run server "
46+
"or check network connection.\n";
47+
BOOST_LOG_TRIVIAL(error) << "Error: " << ec.message();
48+
}
49+
});
50+
}
51+
52+
53+
void Client::doWriteFile(const boost::system::error_code& t_ec)
54+
{
55+
if (!t_ec) {
56+
if (m_sourceFile) {
57+
m_sourceFile.read(m_buf.data(), m_buf.size());
58+
if (m_sourceFile.fail() && !m_sourceFile.eof()) {
59+
auto msg = "Failed while reading file";
60+
BOOST_LOG_TRIVIAL(error) << msg;
61+
throw std::fstream::failure(msg);
62+
}
63+
std::stringstream ss;
64+
ss << "Send " << m_sourceFile.gcount() << " bytes, total: "
65+
<< m_sourceFile.tellg() << " bytes";
66+
BOOST_LOG_TRIVIAL(trace) << ss.str();
67+
std::cout << ss.str() << std::endl;
68+
69+
auto buf = boost::asio::buffer(m_buf.data(), static_cast<size_t>(m_sourceFile.gcount()));
70+
writeBuffer(buf);
71+
}
72+
} else {
73+
BOOST_LOG_TRIVIAL(error) << "Error: " << t_ec.message();
74+
}
75+
}

Client/client.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#pragma once
2+
3+
#include <array>
4+
#include <fstream>
5+
6+
#include <boost/asio.hpp>
7+
8+
9+
class Client
10+
{
11+
public:
12+
using IoService = boost::asio::io_service;
13+
using TcpResolver = boost::asio::ip::tcp::resolver;
14+
using TcpResolverIterator = TcpResolver::iterator;
15+
using TcpSocket = boost::asio::ip::tcp::socket;
16+
17+
Client(IoService& t_ioService, TcpResolverIterator t_endpointIterator,
18+
std::string const& t_path);
19+
20+
private:
21+
void openFile(std::string const& t_path);
22+
void doConnect();
23+
void doWriteFile(const boost::system::error_code& t_ec);
24+
template<class Buffer>
25+
void writeBuffer(Buffer& t_buffer);
26+
27+
28+
TcpResolver m_ioService;
29+
TcpSocket m_socket;
30+
TcpResolverIterator m_endpointIterator;
31+
enum { MessageSize = 1024 };
32+
std::array<char, MessageSize> m_buf;
33+
boost::asio::streambuf m_request;
34+
std::ifstream m_sourceFile;
35+
std::string m_path;
36+
};
37+
38+
39+
template<class Buffer>
40+
void Client::writeBuffer(Buffer& t_buffer)
41+
{
42+
boost::asio::async_write(m_socket,
43+
t_buffer,
44+
[this](boost::system::error_code ec, size_t /*length*/)
45+
{
46+
doWriteFile(ec);
47+
});
48+
}

Client/main.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <iostream>
2+
3+
#include <boost/asio/io_service.hpp>
4+
5+
#include "client.h"
6+
#include "../Common/logger.h"
7+
8+
9+
int main(int argc, char* argv[])
10+
{
11+
if (argc != 4) {
12+
std::cerr << "Usage: client <address> <port> <filePath>\n";
13+
return 1;
14+
}
15+
16+
Logger::instance().setOptions("client_%3N.log", 1 * 1024 * 1024, 10 * 1024 * 1024);
17+
18+
auto address = argv[1];
19+
auto port = argv[2];
20+
auto filePath = argv[3];
21+
22+
try {
23+
boost::asio::io_service ioService;
24+
25+
boost::asio::ip::tcp::resolver resolver(ioService);
26+
auto endpointIterator = resolver.resolve({ address, port });
27+
Client client(ioService, endpointIterator, filePath);
28+
29+
ioService.run();
30+
} catch (std::fstream::failure& e) {
31+
std::cerr << e.what() << "\n";
32+
} catch (std::exception& e) {
33+
std::cerr << "Exception: " << e.what() << "\n";
34+
}
35+
36+
return 0;
37+
}

Common/logger.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <boost/log/utility/setup.hpp>
2+
#include <boost/log/utility/setup/file.hpp>
3+
4+
#include "logger.h"
5+
6+
7+
Logger& Logger::instance()
8+
{
9+
static Logger logger;
10+
return logger;
11+
}
12+
13+
14+
void Logger::setOptions(std::string const& t_fileName, unsigned t_rotationSize,
15+
unsigned t_maxSize)
16+
{
17+
boost::log::add_file_log(
18+
boost::log::keywords::file_name = t_fileName,
19+
boost::log::keywords::rotation_size = t_rotationSize,
20+
boost::log::keywords::max_size = t_maxSize,
21+
boost::log::keywords::time_based_rotation
22+
= boost::log::sinks::file::rotation_at_time_point(0, 0, 0),
23+
boost::log::keywords::format = "[%TimeStamp%]: %Message%",
24+
boost::log::keywords::auto_flush = true
25+
);
26+
27+
boost::log::add_common_attributes();
28+
}

Common/logger.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
6+
class Logger
7+
{
8+
Logger() {}
9+
public:
10+
static Logger& instance();
11+
static void setOptions(std::string const& t_fileName, unsigned t_rotationSize,
12+
unsigned t_maxSize);
13+
};

FileTransfer.sln

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 2013
4+
VisualStudioVersion = 12.0.40418.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Client", "Client\Client.vcxproj", "{60BA9FBE-D2BA-4422-AF9A-652FF009EA46}"
7+
EndProject
8+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Server", "Server\Server.vcxproj", "{49207E11-2770-4624-ACBC-CB9D1CD4A6A9}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Win32 = Debug|Win32
13+
Release|Win32 = Release|Win32
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{60BA9FBE-D2BA-4422-AF9A-652FF009EA46}.Debug|Win32.ActiveCfg = Debug|Win32
17+
{60BA9FBE-D2BA-4422-AF9A-652FF009EA46}.Debug|Win32.Build.0 = Debug|Win32
18+
{60BA9FBE-D2BA-4422-AF9A-652FF009EA46}.Release|Win32.ActiveCfg = Release|Win32
19+
{60BA9FBE-D2BA-4422-AF9A-652FF009EA46}.Release|Win32.Build.0 = Release|Win32
20+
{49207E11-2770-4624-ACBC-CB9D1CD4A6A9}.Debug|Win32.ActiveCfg = Debug|Win32
21+
{49207E11-2770-4624-ACBC-CB9D1CD4A6A9}.Debug|Win32.Build.0 = Debug|Win32
22+
{49207E11-2770-4624-ACBC-CB9D1CD4A6A9}.Release|Win32.ActiveCfg = Release|Win32
23+
{49207E11-2770-4624-ACBC-CB9D1CD4A6A9}.Release|Win32.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
EndGlobal

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Requirements
2+
- MS Visual Studio 2015 with update 5
3+
- boost 1.58.0
4+
5+
# Building for Windows
6+
- Install boost.asio
7+
- Add `BOOST_ROOT` with boost's directory path to environment variables
8+
- Open solution and build
9+
10+
# Running
11+
- start server to accept files
12+
```
13+
server <port> <workDirectory>
14+
```
15+
- start client to transfer file
16+
```
17+
client <address> <port> <filePath>
18+
```
19+

0 commit comments

Comments
 (0)