Example #1
0
        private static void ReceiveCallback(IAsyncResult AR)
        {
            Socket current  = (Socket)AR.AsyncState;
            int    received = 0;

            try
            {
                received = current.EndReceive(AR);
            }
            catch (SocketException)
            {
                Console.WriteLine("Klient zerwal polaczenie");
                current.Close();
                clientSockets.Remove(current);
                return;
            }
            byte[] recBuf1 = new byte[received];
            Array.Copy(buffer, recBuf1, received);
            string   text1 = Encoding.ASCII.GetString(recBuf1);
            Protocol aa    = new Protocol();
            string   msg   = "";

            if (aa.GetNrS(text1) == "1")
            {
                aa.GetMessage1(text1);
            }
            else if (aa.GetNrS(text1) == "2")
            {
                msg = aa.GetMessage2(text1);
            }

            int     otrzymane = 0;
            var     buffer2   = new byte[2048];
            Boolean y         = false;

            do
            {
                int received2 = current.Receive(buffer2, SocketFlags.None);
                if (received2 != 0)
                {
                    otrzymane = received2;
                    y         = true;
                }
            } while (!y);

            byte[] recBuf2 = new byte[otrzymane];
            Array.Copy(buffer2, recBuf2, otrzymane);
            string text2 = Encoding.ASCII.GetString(recBuf2);

            if (aa.GetNrS(text2) == "1")
            {
                aa.GetMessage1(text2);
            }
            else if (aa.GetNrS(text2) == "2")
            {
                msg = aa.GetMessage2(text2);
            }
            if (koniec)
            {
                Protocol a     = new Protocol("odpowiedz", "Ktos inny zgadl liczby, przegrales", DateTime.Now.ToLongTimeString(), aa.ID);
                byte[]   data1 = Encoding.ASCII.GetBytes(a.Pakuj1());
                current.Send(data1);
                Thread.Sleep(100);
                byte[] data2 = Encoding.ASCII.GetBytes(a.Pakuj2());
                current.Send(data2);
                return;
            }
            Console.WriteLine("Klient: " + msg);


            if (msg == "przydzielID")
            {
                PrzydzielID(current);
            }
            else if (msg.ToLower() == guess_num)
            {
                Console.WriteLine("Gracz trafil liczbe");
                koniec = true;
                wygrana(current);
            }
            else if (msg.ToLower() == "wyjscie")
            {
                current.Shutdown(SocketShutdown.Both);
                current.Close();
                clientSockets.Remove(current);
                Console.WriteLine("Klient sie rozlaczyl");
                return;
            }
            else
            {
                Console.WriteLine("Gracz nie trafil liczby");
                DateTime tempTime = DateTime.Now;
                Protocol a        = new Protocol("odpowiedz", "Bledna liczba", DateTime.Now.ToLongTimeString(), aa.ID);
                byte[]   data1    = Encoding.ASCII.GetBytes(a.Pakuj1());
                current.Send(data1);
                Thread.Sleep(100);
                byte[] data2 = Encoding.ASCII.GetBytes(a.Pakuj2());
                current.Send(data2);
                Console.WriteLine("Wyslano powiadomienie, " + DateTime.Now.ToLongTimeString());
            }
            if (!koniec)
            {
                current.BeginReceive(buffer, 0, BUFFER_SIZE, SocketFlags.None, ReceiveCallback, current);
            }
        }