Beispiel #1
0
 public static void Start()
 {
     Console.WriteLine("Starting server, please wait...");
     DataReceiver.InitializePacketsMethods();
     socket.Start();
     socket.BeginAcceptTcpClient(new AsyncCallback(OnClientConnect), null);
     Console.WriteLine("Ready to receive packets");
     Console.ForegroundColor = ConsoleColor.Green;
     Console.WriteLine($"Server has started on {ip}:{port}");
     Console.ForegroundColor = ConsoleColor.White;
 }
Beispiel #2
0
        private void OnReceiveData(IAsyncResult result)
        {
            try
            {
                int length = stream.EndRead(result);
                if (length <= 0)
                {
                    CloseConnection();
                    return;
                }

                byte[] newBytes = new byte[length];
                Array.Copy(receiveBuffer, newBytes, length);
                DataReceiver.HandleData(connexionID, newBytes);

                stream.BeginRead(receiveBuffer, 0, socket.ReceiveBufferSize, OnReceiveData, null);
                buffer.Clear();
            }
            catch (Exception)
            {
                CloseConnection();
                return;
            }
        }