44 lines
803 B
C#
44 lines
803 B
C#
|
using System.Net;
|
|||
|
using System.Net.Sockets;
|
|||
|
|
|||
|
const ushort port = 4444;
|
|||
|
using var tcp = new TcpListener(IPAddress.Loopback, port);
|
|||
|
bool hasQuit = false;
|
|||
|
|
|||
|
var connectionListening = new Thread(() =>
|
|||
|
{
|
|||
|
using var liveConnections = new Connections();
|
|||
|
|
|||
|
while (!hasQuit)
|
|||
|
{
|
|||
|
const int retryDelayMilliseconds = 100;
|
|||
|
|
|||
|
liveConnections.Accept(tcp);
|
|||
|
Thread.Sleep(retryDelayMilliseconds);
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
tcp.Start();
|
|||
|
connectionListening.Start();
|
|||
|
Console.WriteLine($"Server listening on port {port}...");
|
|||
|
|
|||
|
while (!hasQuit)
|
|||
|
{
|
|||
|
var input = Console.ReadLine();
|
|||
|
|
|||
|
if (input == null)
|
|||
|
{
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
switch (input.ToLower())
|
|||
|
{
|
|||
|
case "quit": hasQuit = true; break;
|
|||
|
default: Console.WriteLine($"Unknown command `{input}.`"); break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
tcp.Stop();
|
|||
|
connectionListening.Join();
|
|||
|
Console.WriteLine("Server closed");
|