Пример #1
0
 private bool DoHandshake()
 {
     try
     {
         client = new TcpClient(server.Address.ToString(), server.Port);
         stream = client.GetStream();
         Handshake handshake = new Handshake();
         return(handshake.Authorize(ref stream));
     }
     catch (SocketException ex)
     {
         Global.Log("SocketException in ServerStream message: " + ex.Message);
         stream = null;
         return(false);
     }
     catch (Exception ex)
     {
         Global.Log("Unnknown error in ServerStream handshake. Message: " + ex.Message);
         return(false);
     }
 }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="command"></param>
        /// <param name="buffer"></param>
        /// <param name="total"></param>
        /// <param name="maxsize">0: unlimited</param>
        /// <returns>return true if connected suceffull, false if any exception or authorization faild</returns>
        private static bool SendRequest(Commands command, ref byte[] buffer, ref int total)
        {
            if (server == null)
            {
                server = LocalDatabase.GetServerEndpoint(LocalDatabase.ServerType.Offer);
            }

            List <byte[]> receivedData = new List <byte[]>();

            try
            {
                using (TcpClient client = new TcpClient(server.Address.ToString(), server.Port))
                {
                    total = 0;

                    byte[]        buffor        = new byte[1024];
                    NetworkStream networkStream = client.GetStream();

                    //Autoryzacja
                    Handshake handshake = new Handshake();
                    if (handshake.Authorize(ref networkStream) == false)
                    {
                        return(false);
                    }

                    System.Threading.Thread.Sleep(500);

                    //Wysylanie komendy
                    if (SendByteArray(ref networkStream, GetBytes(command.ToString())) == true)
                    {
                        //pobieranie danych
                        int readed = 0;

                        do
                        {
                            readed = networkStream.Read(buffor, 0, buffor.Length);
                            if (readed > 0)
                            {
                                byte[] newBuffor = new byte[readed];
                                System.Buffer.BlockCopy(buffor, 0, newBuffor, 0, readed);
                                receivedData.Add(newBuffor);
                            }
                            total += readed;
                        } while (readed > 0);
                    }
                    else
                    {
                        Global.Log("Nie udalo się wysłać komendy");
                    }

                    networkStream.Close(); //Zamykanie połączenia
                    client.Close();
                }
            }
            catch (SocketException se)
            {
                Debug.WriteLine(@"Nie udało się połączyć z serwerem. Socket Exception message: " + se.Message);
                return(false);
            }

            buffer = CombineArrays(receivedData); //Łączenie odebranych danych.

            return(true);
        }