Last active
November 21, 2019 21:52
-
-
Save lodejard/386ff1fbf1ef4514c9ee712fe75bd84d to your computer and use it in GitHub Desktop.
ConsoleApp1 echo request octets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp3.0</TargetFramework> | |
</PropertyGroup> | |
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using System.Net.Http; | |
using System.Net.Sockets; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ConsoleApp2 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
MainAsync(args).GetAwaiter().GetResult(); | |
} | |
static async Task MainAsync(string[] args) | |
{ | |
var listener = new TcpListener(4321); | |
listener.Start(); | |
Tangent(); | |
while (true) | |
{ | |
var client = await listener.AcceptTcpClientAsync(); | |
HandleAsync(client); | |
} | |
} | |
private static async void HandleAsync(TcpClient client) | |
{ | |
var stream = client.GetStream(); | |
var buffer = new byte[50 << 10]; | |
var offset = 0; | |
var sb = new StringBuilder(); | |
sb.Append($"HTTP/1.1 200 OK\r\n"); | |
sb.Append($"Server: ConsoleApp2\r\n"); | |
sb.Append($"Connection: close\r\n"); | |
sb.Append($"Content-Type: text/plain\r\n"); | |
sb.Append("\r\n"); | |
stream.Write(Encoding.ASCII.GetBytes(sb.ToString())); | |
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(500)); | |
try | |
{ | |
while (true) | |
{ | |
var bytes = await stream.ReadAsync(buffer, offset, buffer.Length - offset, cts.Token); | |
var part1 = ""; | |
var part2 = ""; | |
for (var i = 0; i != bytes; ++i) | |
{ | |
var ch = buffer[offset + i]; | |
if (ch >= 0x20 && ch < 0x80) | |
{ | |
part2 += (char)ch; | |
} | |
else | |
{ | |
part2 += $"[{ch:x2}]"; | |
} | |
part1 += ($" {buffer[offset + i]:x2}"); | |
if ((i % 32) == 31) | |
{ | |
var line = $"{part1.PadRight(100)}{part2}\r\n"; | |
Console.Write(line); | |
stream.Write(Encoding.ASCII.GetBytes(line)); | |
part1 = ""; | |
part2 = ""; | |
} | |
} | |
offset += bytes; | |
var partial = $"{part1.PadRight(100)}{part2}\r\n"; | |
Console.Write(partial); | |
stream.Write(Encoding.ASCII.GetBytes(partial)); | |
if (bytes == 0) | |
{ | |
break; | |
} | |
} | |
} | |
catch | |
{ | |
stream.Close(); | |
client.Close(); | |
} | |
} | |
private static async void Tangent() | |
{ | |
var client = new HttpClient(); | |
var x = await client.GetAsync("http://localhost:4321/q=robé"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment