private void RecieveCallback(IAsyncResult ar) { Socket socket = (Socket)ar.AsyncState; try { int recieved = socket.EndReceive(ar); if (recieved <= 0) { CloseClient(index); } else { byte[] dataBuffer = new byte[recieved]; Array.Copy(_buffer, dataBuffer, recieved); HandleNetworkData.HandleData(this, dataBuffer); socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(RecieveCallback), socket); } } catch { CloseClient(index); } }
public static void Init() { Console.WriteLine("Initializing server"); HandleNetworkData.Initialize(); _serverSocket.Bind(new IPEndPoint(IPAddress.Any, port)); _serverSocket.Listen(10); _serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null); for (int i = 0; i < MAX_PLAYERS; i++) { _clients[i] = new Client(); } }