feat: finish normal binding socket example

This commit is contained in:
2026-05-20 12:25:50 -04:00
parent 3e6426c655
commit 9c957b0309
3 changed files with 18 additions and 3 deletions
+13 -2
View File
@@ -1,2 +1,13 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
using System.Net;
using System.Net.Sockets;
using var socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
await socket.ConnectAsync(new IPEndPoint(IPAddress.Loopback, 9000));
Console.WriteLine("Type into the console to echo the contents of your message.");
var ns = new NetworkStream(socket);
var readTask = Console.OpenStandardInput().CopyToAsync(ns);
var writeTask = ns.CopyToAsync(Console.OpenStandardOutput());
await Task.WhenAny(readTask, writeTask);
+4
View File
@@ -23,6 +23,10 @@ while (true)
int read = await connection.ReceiveAsync(buffer, SocketFlags.None);
if (read == 0) break;
var message = System.Text.Encoding.Default.GetString(buffer[..read]);
Console.WriteLine($"Received new message: {message}");
await connection.SendAsync(buffer[..read], SocketFlags.None);
}
}
+1 -1
View File
@@ -23,7 +23,7 @@
in
{
packages.default = pkgs.buildDotnetModule {
pname = "dotnet-systemd";
pname = "Server";
version = "0.1.0";
src = ./Server;