Skip to content

Instantly share code, notes, and snippets.

@Deathspike
Created April 9, 2020 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Deathspike/bb1260215e6b165b463e872d89417210 to your computer and use it in GitHub Desktop.
Save Deathspike/bb1260215e6b165b463e872d89417210 to your computer and use it in GitHub Desktop.
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace PlayGround
{
class Program
{
private static async Task ClientAsync(Socket socket)
{
using (socket)
{
await using var ns = new NetworkStream(socket);
await using var sw = new StreamWriter(ns);
using var sr = new StreamReader(ns);
while (true)
{
var line = await sr.ReadLineAsync();
if (!string.IsNullOrEmpty(line)) continue;
await sw.WriteLineAsync("HTTP/1.1 200 OK\r\n\r\nHello world!");
await sw.FlushAsync();
break;
}
}
}
private static async Task ServerAsync()
{
var listener = new TcpListener(IPAddress.Any, 3000);
listener.AllowNatTraversal(true);
listener.Start();
while (true)
{
var client = await listener.AcceptSocketAsync();
var concurrentTask = ClientAsync(client);
}
}
public static void Main()
{
ServerAsync().Wait();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment