Commit 2e6492457d7e02390f8639e173744f9737b03e82

Authored by Andrey Tkachenko
0 parents
Exists in master

Very first, but working commit.

Showing 5 changed files with 253 additions and 0 deletions Side-by-side Diff

... ... @@ -0,0 +1,3 @@
  1 +bin
  2 +obj
  3 +*.manifest
SendFileToSocket.sln
... ... @@ -0,0 +1,20 @@
  1 +
  2 +Microsoft Visual Studio Solution File, Format Version 12.00
  3 +# Visual Studio 2012
  4 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SendFileToSocket", "SendFileToSocket\SendFileToSocket.csproj", "{E2845014-C43A-469C-BDC9-8D288DA15D86}"
  5 +EndProject
  6 +Global
  7 + GlobalSection(SolutionConfigurationPlatforms) = preSolution
  8 + Debug|Any CPU = Debug|Any CPU
  9 + Release|Any CPU = Release|Any CPU
  10 + EndGlobalSection
  11 + GlobalSection(ProjectConfigurationPlatforms) = postSolution
  12 + {E2845014-C43A-469C-BDC9-8D288DA15D86}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
  13 + {E2845014-C43A-469C-BDC9-8D288DA15D86}.Debug|Any CPU.Build.0 = Debug|Any CPU
  14 + {E2845014-C43A-469C-BDC9-8D288DA15D86}.Release|Any CPU.ActiveCfg = Release|Any CPU
  15 + {E2845014-C43A-469C-BDC9-8D288DA15D86}.Release|Any CPU.Build.0 = Release|Any CPU
  16 + EndGlobalSection
  17 + GlobalSection(SolutionProperties) = preSolution
  18 + HideSolutionNode = FALSE
  19 + EndGlobalSection
  20 +EndGlobal
SendFileToSocket/Program.cs
... ... @@ -0,0 +1,139 @@
  1 +using System;
  2 +using System.Collections.Generic;
  3 +using System.IO;
  4 +using System.Linq;
  5 +using System.Net;
  6 +using System.Net.Sockets;
  7 +using System.Text;
  8 +using System.Threading;
  9 +
  10 +namespace SendFileToSocket
  11 +{
  12 + class Program
  13 + {
  14 + class ReadState
  15 + {
  16 + public int BufferSize;
  17 + public byte[] Buffer;
  18 + public Stream Stream;
  19 + }
  20 +
  21 + static int MaxBufferSize = 1024 * 512;
  22 +
  23 + static void Main(string[] args)
  24 + {
  25 + if (args.Length != 3)
  26 + {
  27 + _ShowUsage();
  28 + return;
  29 + }
  30 +
  31 + if (!File.Exists(args[2]))
  32 + {
  33 + _ShowUsage();
  34 + return;
  35 + }
  36 +
  37 + try
  38 + {
  39 + var ipAddress = default(IPAddress);
  40 + try
  41 + {
  42 + var addresses = Dns.GetHostAddresses(args[0]);
  43 + ipAddress = addresses[0];
  44 + }
  45 + catch
  46 + {
  47 + }
  48 +
  49 + if (ipAddress == default(IPAddress))
  50 + ipAddress = IPAddress.Parse(args[0]);
  51 +
  52 + var port = int.Parse(args[1]);
  53 +
  54 + var remoteEP = new IPEndPoint(ipAddress, port);
  55 +
  56 + using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
  57 + {
  58 + socket.Connect(ipAddress, port);
  59 +
  60 + using (var stream = new NetworkStream(socket, true))
  61 + {
  62 + var readState = new ReadState
  63 + {
  64 + BufferSize = MaxBufferSize,
  65 + Buffer = new byte[MaxBufferSize],
  66 + Stream = stream
  67 + };
  68 +
  69 + stream.BeginRead(readState.Buffer, 0, readState.BufferSize, _OnReadCompleted, readState);
  70 +
  71 + using (var streamWriter = new StreamWriter(stream))
  72 + {
  73 + using (var file = File.OpenText(args[2]))
  74 + {
  75 + while (true)
  76 + {
  77 + var line = file.ReadLine();
  78 + if (line == null)
  79 + break;
  80 + streamWriter.WriteLine(line);
  81 + break;
  82 + }
  83 +
  84 + streamWriter.Flush();
  85 + }
  86 +
  87 + while (!_ReadCompleted)
  88 + {
  89 + Thread.Sleep(100);
  90 + _ReadTimeout--;
  91 + if (_ReadTimeout < 0)
  92 + break;
  93 + }
  94 + }
  95 + }
  96 + }
  97 + }
  98 + catch(Exception ex)
  99 + {
  100 + Console.WriteLine(ex.Message);
  101 + _ShowUsage();
  102 + }
  103 + }
  104 +
  105 +
  106 + static volatile int _ReadTimeout = 10;
  107 + static volatile bool _ReadCompleted = false;
  108 + static void _OnReadCompleted(IAsyncResult ar)
  109 + {
  110 + _ReadTimeout = 10;
  111 +
  112 + var readState = ar.AsyncState as ReadState;
  113 + if (readState == null)
  114 + return;
  115 + try
  116 + {
  117 + var readed = readState.Stream.EndRead(ar);
  118 + if (readed == 0)
  119 + {
  120 + _ReadCompleted = true;
  121 + return;
  122 + }
  123 +
  124 + Console.WriteLine(Encoding.Default.GetString(readState.Buffer, 0, readed));
  125 +
  126 + readState.Stream.BeginRead(readState.Buffer, 0, readState.BufferSize, _OnReadCompleted, readState);
  127 + }
  128 + catch
  129 + {
  130 + _ReadCompleted = true;
  131 + }
  132 + }
  133 +
  134 + static void _ShowUsage()
  135 + {
  136 + Console.WriteLine("SendFileToSocket.exe <HOST> <PORT> <PATH-TO-FILE>");
  137 + }
  138 + }
  139 +}
SendFileToSocket/Properties/AssemblyInfo.cs
... ... @@ -0,0 +1,36 @@
  1 +using System.Reflection;
  2 +using System.Runtime.CompilerServices;
  3 +using System.Runtime.InteropServices;
  4 +
  5 +// General Information about an assembly is controlled through the following
  6 +// set of attributes. Change these attribute values to modify the information
  7 +// associated with an assembly.
  8 +[assembly: AssemblyTitle("SendFileToSocket")]
  9 +[assembly: AssemblyDescription("")]
  10 +[assembly: AssemblyConfiguration("")]
  11 +[assembly: AssemblyCompany("")]
  12 +[assembly: AssemblyProduct("SendFileToSocket")]
  13 +[assembly: AssemblyCopyright("Copyright © 2014")]
  14 +[assembly: AssemblyTrademark("")]
  15 +[assembly: AssemblyCulture("")]
  16 +
  17 +// Setting ComVisible to false makes the types in this assembly not visible
  18 +// to COM components. If you need to access a type in this assembly from
  19 +// COM, set the ComVisible attribute to true on that type.
  20 +[assembly: ComVisible(false)]
  21 +
  22 +// The following GUID is for the ID of the typelib if this project is exposed to COM
  23 +[assembly: Guid("1b677d21-2b3f-4783-94e3-afbb25a56037")]
  24 +
  25 +// Version information for an assembly consists of the following four values:
  26 +//
  27 +// Major Version
  28 +// Minor Version
  29 +// Build Number
  30 +// Revision
  31 +//
  32 +// You can specify all the values or you can default the Build and Revision Numbers
  33 +// by using the '*' as shown below:
  34 +// [assembly: AssemblyVersion("1.0.*")]
  35 +[assembly: AssemblyVersion("1.0.0.0")]
  36 +[assembly: AssemblyFileVersion("1.0.0.0")]
SendFileToSocket/SendFileToSocket.csproj
... ... @@ -0,0 +1,55 @@
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  3 + <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  4 + <PropertyGroup>
  5 + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
  6 + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
  7 + <ProjectGuid>{E2845014-C43A-469C-BDC9-8D288DA15D86}</ProjectGuid>
  8 + <OutputType>Exe</OutputType>
  9 + <AppDesignerFolder>Properties</AppDesignerFolder>
  10 + <RootNamespace>SendFileToSocket</RootNamespace>
  11 + <AssemblyName>SendFileToSocket</AssemblyName>
  12 + <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
  13 + <FileAlignment>512</FileAlignment>
  14 + </PropertyGroup>
  15 + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  16 + <PlatformTarget>AnyCPU</PlatformTarget>
  17 + <DebugSymbols>true</DebugSymbols>
  18 + <DebugType>full</DebugType>
  19 + <Optimize>false</Optimize>
  20 + <OutputPath>bin\Debug\</OutputPath>
  21 + <DefineConstants>DEBUG;TRACE</DefineConstants>
  22 + <ErrorReport>prompt</ErrorReport>
  23 + <WarningLevel>4</WarningLevel>
  24 + </PropertyGroup>
  25 + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
  26 + <PlatformTarget>AnyCPU</PlatformTarget>
  27 + <DebugType>pdbonly</DebugType>
  28 + <Optimize>true</Optimize>
  29 + <OutputPath>bin\Release\</OutputPath>
  30 + <DefineConstants>TRACE</DefineConstants>
  31 + <ErrorReport>prompt</ErrorReport>
  32 + <WarningLevel>4</WarningLevel>
  33 + </PropertyGroup>
  34 + <ItemGroup>
  35 + <Reference Include="System" />
  36 + <Reference Include="System.Core" />
  37 + <Reference Include="System.Xml.Linq" />
  38 + <Reference Include="System.Data.DataSetExtensions" />
  39 + <Reference Include="Microsoft.CSharp" />
  40 + <Reference Include="System.Data" />
  41 + <Reference Include="System.Xml" />
  42 + </ItemGroup>
  43 + <ItemGroup>
  44 + <Compile Include="Program.cs" />
  45 + <Compile Include="Properties\AssemblyInfo.cs" />
  46 + </ItemGroup>
  47 + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  48 + <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
  49 + Other similar extension points exist, see Microsoft.Common.targets.
  50 + <Target Name="BeforeBuild">
  51 + </Target>
  52 + <Target Name="AfterBuild">
  53 + </Target>
  54 + -->
  55 +</Project>
0 56 \ No newline at end of file