I need a c# socket server that handles multiple connections at once, each socket handler…
- User
I need a c# socket server that handles multiple connections at once, each socket handler should be async and output the current date and time then disconnect.
Assistant
Here is a simple asynchronous C# socket server that handles multiple connections concurrently and outputs the current date and time before disconnecting each client:
using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; class SocketServer { public static async Task Main() { var listener = new TcpListener(IPAddress.Any, 8080); listener.Start(); while (true) { TcpClient client = await listener.AcceptTcpClientAsync(); _ = HandleClientAsync(client); } } private static async Task HandleClientAsync(TcpClient client) { try { NetworkStream stream = client.GetStream(); byte[] date = Encoding.UTF8.GetBytes(DateTime.Now.ToString("r")); await stream.WriteAsync(date, 0, date.Length); client.Close(); } catch { // ignore exceptions on close } } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.