get udp example working
This commit is contained in:
parent
d2621e2aec
commit
4420b4e061
3 changed files with 56 additions and 5 deletions
|
|
@ -1,12 +1,52 @@
|
|||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Love2DBackend
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello from LÖVE2D Backend!");
|
||||
Console.WriteLine("LÖVE2D Backend - UDP Server");
|
||||
Console.WriteLine("Starting server on port 7777...");
|
||||
|
||||
using (var udpServer = new UdpClient(7777))
|
||||
{
|
||||
Console.WriteLine("Server listening on 0.0.0.0:7777");
|
||||
|
||||
var cts = new CancellationTokenSource();
|
||||
|
||||
// Handle Ctrl+C gracefully
|
||||
Console.CancelKeyPress += (s, e) =>
|
||||
{
|
||||
e.Cancel = true;
|
||||
cts.Cancel();
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
while (!cts.Token.IsCancellationRequested)
|
||||
{
|
||||
var result = await udpServer.ReceiveAsync(cts.Token);
|
||||
var message = Encoding.UTF8.GetString(result.Buffer);
|
||||
var clientEndpoint = result.RemoteEndPoint;
|
||||
|
||||
Console.WriteLine($"[{clientEndpoint}] {message}");
|
||||
|
||||
// Echo back
|
||||
var response = Encoding.UTF8.GetBytes($"Echo: {message}");
|
||||
await udpServer.SendAsync(response, response.Length, clientEndpoint);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
Console.WriteLine("Server stopped.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue