public class PacketListener{ private bool _isRunning; public PacketListener(ServerClient client) { _isRunning = true; NetworkStream stream = client.Stream; while (_isRunning) { byte[] buffer = new byte[1024]; int bytesRead = stream.Read(buffer, 0, buffer.Length); if (bytesRead > 0) { stream.Write(buffer, 0, bytesRead); } else { break; } } client.Client.Close(); Console.WriteLine("Client has disconnected."); } public void Stop() { _isRunning = false; }}
public class ServerConnection{ private static bool _IsRunning; private readonly TcpListener _listener; public ServerConnection(IPAddress address, int port) { if (_IsRunning) { throw new Exception("Server is already running."); } _listener = new TcpListener(address, port); _IsRunning = true; } public void Init() { try { _listener.Start(); Console.WriteLine("Server started. Waiting for connections..."); TcpClient client = _listener.AcceptTcpClient(); Console.WriteLine("Client connected."); new ServerClient(client); } catch (Exception e) { Console.WriteLine("Server Error: " + e.Message); } }}
Stream.Write(packetBytes, 0, packetBytes.Length);