using System.Net.Sockets; using System.Net; using System.Text; using static System.Runtime.InteropServices.JavaScript.JSType; namespace LobbyClientTest { internal class FakeGameHost : IDisposable { public bool isHost = false; UdpClient? udpClient; bool running = true; public int Server(int port) { udpClient = new UdpClient(port); _ = Task.Run(() => { Receive(); }); return ((IPEndPoint)udpClient.Client.LocalEndPoint!).Port; } public void Send(IPEndPoint ep, string text) { byte[] data = Encoding.ASCII.GetBytes(text); udpClient?.Send(data, data.Length, ep); } public void Send(IPEndPoint ep, byte[] message, int length) { udpClient?.Send(message, length, ep); } private void Receive() { try { IPEndPoint remoteEp = new IPEndPoint(IPAddress.Any, 0); while (running) { var data = udpClient!.Receive(ref remoteEp); Console.WriteLine($"Game {(isHost ? "host" : "client")} received: {remoteEp.ToString()}: {data.Length}, {Encoding.ASCII.GetString(data, 0, data.Length)}"); if (isHost) Send(remoteEp, "Hello from Game Server!"); } } catch { } finally { } } public void Dispose() { running = false; udpClient?.Dispose(); } } }