//uzyskanie ID od servera private static Protocol GetID() { int otrzymane = 0; Console.WriteLine("Wysyłam prośbę o przydzielenie ID"); Protocol paczkaID = new Protocol("ID", "przydzielID", DateTime.Now.ToLongTimeString(), "0"); SendString(paczkaID.Pakuj1()); Thread.Sleep(100); SendString(paczkaID.Pakuj2()); Boolean x = false; var buffer = new byte[2048]; int received = ClientSocket.Receive(buffer, SocketFlags.None); if (received != 0) { otrzymane = received; x = true; } var data = new byte[otrzymane]; Array.Copy(buffer, data, otrzymane); string text = Encoding.ASCII.GetString(data); if (paczka.GetNrS(text) == "1") { paczka.GetMessage1(text); } else if (paczka.GetNrS(text) == "2") { Console.WriteLine(paczka.GetMessage2(text)); } Boolean y = false; int received2 = ClientSocket.Receive(buffer, SocketFlags.None); if (received2 != 0) { otrzymane = received2; y = true; } var data2 = new byte[otrzymane]; Array.Copy(buffer, data2, otrzymane); string text2 = Encoding.ASCII.GetString(data2); if (paczka.GetNrS(text2) == "1") { paczka.GetMessage1(text2); } else if (paczka.GetNrS(text2) == "2") { string xyz = paczka.GetMessage2(text2); } Console.WriteLine("moje ID: " + paczka.ID); return(paczka); }
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); } }