Commit 9cf6c0606521c3c230d6caa4199f0f2260e93215
1 parent
2e6492457d
Exists in
master
Little issue with send lines fixed.
Showing 1 changed file with 0 additions and 1 deletions Inline Diff
SendFileToSocket/Program.cs
| 1 | using System; | 1 | using System; |
| 2 | using System.Collections.Generic; | 2 | using System.Collections.Generic; |
| 3 | using System.IO; | 3 | using System.IO; |
| 4 | using System.Linq; | 4 | using System.Linq; |
| 5 | using System.Net; | 5 | using System.Net; |
| 6 | using System.Net.Sockets; | 6 | using System.Net.Sockets; |
| 7 | using System.Text; | 7 | using System.Text; |
| 8 | using System.Threading; | 8 | using System.Threading; |
| 9 | 9 | ||
| 10 | namespace SendFileToSocket | 10 | namespace SendFileToSocket |
| 11 | { | 11 | { |
| 12 | class Program | 12 | class Program |
| 13 | { | 13 | { |
| 14 | class ReadState | 14 | class ReadState |
| 15 | { | 15 | { |
| 16 | public int BufferSize; | 16 | public int BufferSize; |
| 17 | public byte[] Buffer; | 17 | public byte[] Buffer; |
| 18 | public Stream Stream; | 18 | public Stream Stream; |
| 19 | } | 19 | } |
| 20 | 20 | ||
| 21 | static int MaxBufferSize = 1024 * 512; | 21 | static int MaxBufferSize = 1024 * 512; |
| 22 | 22 | ||
| 23 | static void Main(string[] args) | 23 | static void Main(string[] args) |
| 24 | { | 24 | { |
| 25 | if (args.Length != 3) | 25 | if (args.Length != 3) |
| 26 | { | 26 | { |
| 27 | _ShowUsage(); | 27 | _ShowUsage(); |
| 28 | return; | 28 | return; |
| 29 | } | 29 | } |
| 30 | 30 | ||
| 31 | if (!File.Exists(args[2])) | 31 | if (!File.Exists(args[2])) |
| 32 | { | 32 | { |
| 33 | _ShowUsage(); | 33 | _ShowUsage(); |
| 34 | return; | 34 | return; |
| 35 | } | 35 | } |
| 36 | 36 | ||
| 37 | try | 37 | try |
| 38 | { | 38 | { |
| 39 | var ipAddress = default(IPAddress); | 39 | var ipAddress = default(IPAddress); |
| 40 | try | 40 | try |
| 41 | { | 41 | { |
| 42 | var addresses = Dns.GetHostAddresses(args[0]); | 42 | var addresses = Dns.GetHostAddresses(args[0]); |
| 43 | ipAddress = addresses[0]; | 43 | ipAddress = addresses[0]; |
| 44 | } | 44 | } |
| 45 | catch | 45 | catch |
| 46 | { | 46 | { |
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | if (ipAddress == default(IPAddress)) | 49 | if (ipAddress == default(IPAddress)) |
| 50 | ipAddress = IPAddress.Parse(args[0]); | 50 | ipAddress = IPAddress.Parse(args[0]); |
| 51 | 51 | ||
| 52 | var port = int.Parse(args[1]); | 52 | var port = int.Parse(args[1]); |
| 53 | 53 | ||
| 54 | var remoteEP = new IPEndPoint(ipAddress, port); | 54 | var remoteEP = new IPEndPoint(ipAddress, port); |
| 55 | 55 | ||
| 56 | using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) | 56 | using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) |
| 57 | { | 57 | { |
| 58 | socket.Connect(ipAddress, port); | 58 | socket.Connect(ipAddress, port); |
| 59 | 59 | ||
| 60 | using (var stream = new NetworkStream(socket, true)) | 60 | using (var stream = new NetworkStream(socket, true)) |
| 61 | { | 61 | { |
| 62 | var readState = new ReadState | 62 | var readState = new ReadState |
| 63 | { | 63 | { |
| 64 | BufferSize = MaxBufferSize, | 64 | BufferSize = MaxBufferSize, |
| 65 | Buffer = new byte[MaxBufferSize], | 65 | Buffer = new byte[MaxBufferSize], |
| 66 | Stream = stream | 66 | Stream = stream |
| 67 | }; | 67 | }; |
| 68 | 68 | ||
| 69 | stream.BeginRead(readState.Buffer, 0, readState.BufferSize, _OnReadCompleted, readState); | 69 | stream.BeginRead(readState.Buffer, 0, readState.BufferSize, _OnReadCompleted, readState); |
| 70 | 70 | ||
| 71 | using (var streamWriter = new StreamWriter(stream)) | 71 | using (var streamWriter = new StreamWriter(stream)) |
| 72 | { | 72 | { |
| 73 | using (var file = File.OpenText(args[2])) | 73 | using (var file = File.OpenText(args[2])) |
| 74 | { | 74 | { |
| 75 | while (true) | 75 | while (true) |
| 76 | { | 76 | { |
| 77 | var line = file.ReadLine(); | 77 | var line = file.ReadLine(); |
| 78 | if (line == null) | 78 | if (line == null) |
| 79 | break; | 79 | break; |
| 80 | streamWriter.WriteLine(line); | 80 | streamWriter.WriteLine(line); |
| 81 | break; | ||
| 82 | } | 81 | } |
| 83 | 82 | ||
| 84 | streamWriter.Flush(); | 83 | streamWriter.Flush(); |
| 85 | } | 84 | } |
| 86 | 85 | ||
| 87 | while (!_ReadCompleted) | 86 | while (!_ReadCompleted) |
| 88 | { | 87 | { |
| 89 | Thread.Sleep(100); | 88 | Thread.Sleep(100); |
| 90 | _ReadTimeout--; | 89 | _ReadTimeout--; |
| 91 | if (_ReadTimeout < 0) | 90 | if (_ReadTimeout < 0) |
| 92 | break; | 91 | break; |
| 93 | } | 92 | } |
| 94 | } | 93 | } |
| 95 | } | 94 | } |
| 96 | } | 95 | } |
| 97 | } | 96 | } |
| 98 | catch(Exception ex) | 97 | catch(Exception ex) |
| 99 | { | 98 | { |
| 100 | Console.WriteLine(ex.Message); | 99 | Console.WriteLine(ex.Message); |
| 101 | _ShowUsage(); | 100 | _ShowUsage(); |
| 102 | } | 101 | } |
| 103 | } | 102 | } |
| 104 | 103 | ||
| 105 | 104 | ||
| 106 | static volatile int _ReadTimeout = 10; | 105 | static volatile int _ReadTimeout = 10; |
| 107 | static volatile bool _ReadCompleted = false; | 106 | static volatile bool _ReadCompleted = false; |
| 108 | static void _OnReadCompleted(IAsyncResult ar) | 107 | static void _OnReadCompleted(IAsyncResult ar) |
| 109 | { | 108 | { |
| 110 | _ReadTimeout = 10; | 109 | _ReadTimeout = 10; |
| 111 | 110 | ||
| 112 | var readState = ar.AsyncState as ReadState; | 111 | var readState = ar.AsyncState as ReadState; |
| 113 | if (readState == null) | 112 | if (readState == null) |
| 114 | return; | 113 | return; |
| 115 | try | 114 | try |
| 116 | { | 115 | { |
| 117 | var readed = readState.Stream.EndRead(ar); | 116 | var readed = readState.Stream.EndRead(ar); |
| 118 | if (readed == 0) | 117 | if (readed == 0) |
| 119 | { | 118 | { |
| 120 | _ReadCompleted = true; | 119 | _ReadCompleted = true; |
| 121 | return; | 120 | return; |
| 122 | } | 121 | } |
| 123 | 122 | ||
| 124 | Console.WriteLine(Encoding.Default.GetString(readState.Buffer, 0, readed)); | 123 | Console.WriteLine(Encoding.Default.GetString(readState.Buffer, 0, readed)); |
| 125 | 124 | ||
| 126 | readState.Stream.BeginRead(readState.Buffer, 0, readState.BufferSize, _OnReadCompleted, readState); | 125 | readState.Stream.BeginRead(readState.Buffer, 0, readState.BufferSize, _OnReadCompleted, readState); |
| 127 | } | 126 | } |
| 128 | catch | 127 | catch |
| 129 | { | 128 | { |
| 130 | _ReadCompleted = true; | 129 | _ReadCompleted = true; |
| 131 | } | 130 | } |
| 132 | } | 131 | } |
| 133 | 132 | ||
| 134 | static void _ShowUsage() | 133 | static void _ShowUsage() |
| 135 | { | 134 | { |
| 136 | Console.WriteLine("SendFileToSocket.exe <HOST> <PORT> <PATH-TO-FILE>"); | 135 | Console.WriteLine("SendFileToSocket.exe <HOST> <PORT> <PATH-TO-FILE>"); |
| 137 | } | 136 | } |
| 138 | } | 137 | } |
| 139 | } | 138 | } |
| 140 | 139 |