Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            ClientHandleNetworkPackets.InitializeNetworkPackages();
            ClientTCP.ConnectToServer();

            Console.ReadLine();
        }
Ejemplo n.º 2
0
        private static void OnReceive()
        {
            byte[] _sizeInfo       = new byte[4];
            byte[] _receivedBuffer = new byte[1024];

            int totalRead = 0, currentRead = 0;

            try
            {
                currentRead = totalRead = _clientSocket.Receive(_sizeInfo);
                if (totalRead <= 0)
                {
                    Console.WriteLine("You are not connected to the server.");
                }
                else
                {
                    while (totalRead < _sizeInfo.Length && currentRead > 0)
                    {
                        currentRead = _clientSocket.Receive(_sizeInfo, totalRead, _sizeInfo.Length - totalRead, SocketFlags.None);
                        totalRead  += currentRead;
                    }

                    int messageSize = 0;
                    messageSize |= _sizeInfo[0];
                    messageSize |= (_sizeInfo[1] << 8);
                    messageSize |= (_sizeInfo[2] << 16);
                    messageSize |= (_sizeInfo[3] << 24);

                    byte[] data = new byte[messageSize];

                    totalRead   = 0;
                    currentRead = totalRead = _clientSocket.Receive(data, totalRead, data.Length - totalRead, SocketFlags.None);

                    while (totalRead < messageSize && currentRead > 0)
                    {
                        currentRead = _clientSocket.Receive(data, totalRead, data.Length - totalRead, SocketFlags.None);
                        totalRead  += currentRead;
                    }

                    ClientHandleNetworkPackets.HandleNetworkInformation(data);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("You are not connected to the server.");
            }
        }