From 2e6492457d7e02390f8639e173744f9737b03e82 Mon Sep 17 00:00:00 2001 From: Andrey Tkachenko Date: Thu, 1 May 2014 15:24:58 +0300 Subject: [PATCH] Very first, but working commit. --- .gitignore | 3 + SendFileToSocket.sln | 20 ++++ SendFileToSocket/Program.cs | 139 ++++++++++++++++++++++++++++ SendFileToSocket/Properties/AssemblyInfo.cs | 36 +++++++ SendFileToSocket/SendFileToSocket.csproj | 55 +++++++++++ 5 files changed, 253 insertions(+) create mode 100644 .gitignore create mode 100644 SendFileToSocket.sln create mode 100644 SendFileToSocket/Program.cs create mode 100644 SendFileToSocket/Properties/AssemblyInfo.cs create mode 100644 SendFileToSocket/SendFileToSocket.csproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3d8df41 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +bin +obj +*.manifest diff --git a/SendFileToSocket.sln b/SendFileToSocket.sln new file mode 100644 index 0000000..e671b07 --- /dev/null +++ b/SendFileToSocket.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SendFileToSocket", "SendFileToSocket\SendFileToSocket.csproj", "{E2845014-C43A-469C-BDC9-8D288DA15D86}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E2845014-C43A-469C-BDC9-8D288DA15D86}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E2845014-C43A-469C-BDC9-8D288DA15D86}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E2845014-C43A-469C-BDC9-8D288DA15D86}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E2845014-C43A-469C-BDC9-8D288DA15D86}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/SendFileToSocket/Program.cs b/SendFileToSocket/Program.cs new file mode 100644 index 0000000..204be94 --- /dev/null +++ b/SendFileToSocket/Program.cs @@ -0,0 +1,139 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading; + +namespace SendFileToSocket +{ + class Program + { + class ReadState + { + public int BufferSize; + public byte[] Buffer; + public Stream Stream; + } + + static int MaxBufferSize = 1024 * 512; + + static void Main(string[] args) + { + if (args.Length != 3) + { + _ShowUsage(); + return; + } + + if (!File.Exists(args[2])) + { + _ShowUsage(); + return; + } + + try + { + var ipAddress = default(IPAddress); + try + { + var addresses = Dns.GetHostAddresses(args[0]); + ipAddress = addresses[0]; + } + catch + { + } + + if (ipAddress == default(IPAddress)) + ipAddress = IPAddress.Parse(args[0]); + + var port = int.Parse(args[1]); + + var remoteEP = new IPEndPoint(ipAddress, port); + + using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) + { + socket.Connect(ipAddress, port); + + using (var stream = new NetworkStream(socket, true)) + { + var readState = new ReadState + { + BufferSize = MaxBufferSize, + Buffer = new byte[MaxBufferSize], + Stream = stream + }; + + stream.BeginRead(readState.Buffer, 0, readState.BufferSize, _OnReadCompleted, readState); + + using (var streamWriter = new StreamWriter(stream)) + { + using (var file = File.OpenText(args[2])) + { + while (true) + { + var line = file.ReadLine(); + if (line == null) + break; + streamWriter.WriteLine(line); + break; + } + + streamWriter.Flush(); + } + + while (!_ReadCompleted) + { + Thread.Sleep(100); + _ReadTimeout--; + if (_ReadTimeout < 0) + break; + } + } + } + } + } + catch(Exception ex) + { + Console.WriteLine(ex.Message); + _ShowUsage(); + } + } + + + static volatile int _ReadTimeout = 10; + static volatile bool _ReadCompleted = false; + static void _OnReadCompleted(IAsyncResult ar) + { + _ReadTimeout = 10; + + var readState = ar.AsyncState as ReadState; + if (readState == null) + return; + try + { + var readed = readState.Stream.EndRead(ar); + if (readed == 0) + { + _ReadCompleted = true; + return; + } + + Console.WriteLine(Encoding.Default.GetString(readState.Buffer, 0, readed)); + + readState.Stream.BeginRead(readState.Buffer, 0, readState.BufferSize, _OnReadCompleted, readState); + } + catch + { + _ReadCompleted = true; + } + } + + static void _ShowUsage() + { + Console.WriteLine("SendFileToSocket.exe "); + } + } +} diff --git a/SendFileToSocket/Properties/AssemblyInfo.cs b/SendFileToSocket/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..5a84443 --- /dev/null +++ b/SendFileToSocket/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("SendFileToSocket")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("SendFileToSocket")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("1b677d21-2b3f-4783-94e3-afbb25a56037")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/SendFileToSocket/SendFileToSocket.csproj b/SendFileToSocket/SendFileToSocket.csproj new file mode 100644 index 0000000..8883705 --- /dev/null +++ b/SendFileToSocket/SendFileToSocket.csproj @@ -0,0 +1,55 @@ + + + + + Debug + AnyCPU + {E2845014-C43A-469C-BDC9-8D288DA15D86} + Exe + Properties + SendFileToSocket + SendFileToSocket + v4.0 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + \ No newline at end of file -- 2.1.0