-
-
Save Deathspike/bb1260215e6b165b463e872d89417210 to your computer and use it in GitHub Desktop.
This file contains 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.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