示例#1
0
        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);
            }
        }
示例#2
0
        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();
            }
        }