Пример #1
0
        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            lock (locker)
            {
                if (client != null)
                {
                    if (client.Connected)
                    {
                        client?.GetStream()?.Close();
                    }
                    client?.Close();
                    client = null;
                }

                connectToMainServerThread?.Abort();
                connectToMainServerThread = null;

                ListHolder.DownloadableFiles.Clear();
                ListHolder.UpdateList();

                MessageBox.Show("Client close", "Close");
            }
        }
Пример #2
0
        private void UdpDownload(UdpClient udpClient, IPEndPoint udpListenerIP, DownloadableFile file)
        {
            byte[] hashBuffer;
            byte[] dataBuffer;
            byte[] messageBuffer;
            long   filesize = file.FileSize;
            long   received = 0;

            DownloadedFile newFileInfo = new DownloadedFile(file.FileName, "Downloading...");

            ListHolder.DownloadedFiles.Add(newFileInfo);
            ListHolder.UpdateList();

            udpClient.Client.ReceiveTimeout = 7000;
            udpClient.Client.SendTimeout    = 7000;
            try
            {
                byte[] relyBuffer = udpClient.Receive(ref udpListenerIP);

                messageBuffer = Encoding.UTF8.GetBytes("<ok>");
                udpClient.Send(messageBuffer, messageBuffer.Length, udpListenerIP);

                using (FileStream newFile = File.OpenWrite(file.ShortenFileName))
                {
                    while (received < filesize)
                    {
                        hashBuffer = udpClient.Receive(ref udpListenerIP);
                        dataBuffer = udpClient.Receive(ref udpListenerIP);

                        string hash = Encoding.UTF8.GetString(hashBuffer);
                        string data = Encoding.UTF8.GetString(dataBuffer);

                        if (MyMD5Hash.VerifyMd5Hash(dataBuffer, hash) == false)
                        {
                            messageBuffer = Encoding.UTF8.GetBytes("<error>");
                            udpClient.Send(messageBuffer, messageBuffer.Length, udpListenerIP);
                            continue;
                        }

                        messageBuffer = Encoding.UTF8.GetBytes("<ok>");
                        udpClient.Send(messageBuffer, messageBuffer.Length, udpListenerIP);

                        newFile.Write(dataBuffer, 0, dataBuffer.Length);
                        received += dataBuffer.Length;
                    }
                }

                udpClient.Close();
                newFileInfo.Status = "Finish";
                ListHolder.UpdateList();

                MyDispatcher.Dispatcher.Invoke(() =>
                {
                    MessageBox.Show("finish download file: " + file.FileName);
                });
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Client error: when download file from file server");
                newFileInfo.Status = "Download error";
                ListHolder.UpdateList();
            }
        }
Пример #3
0
        private void connectToMainServer(IPEndPoint serverIPEndPoint)
        {
            MyStreamIO myStream = null;

            try
            {
                client = new TcpClient(AddressFamily.InterNetworkV6);
                client.Connect(serverIPEndPoint);
                myStream = new MyStreamIO(client.GetStream());
                myStream.Write("<isClient>");
                myStream.GetNEXT();


                client.ReceiveTimeout = 7000;
                client.SendTimeout    = 7000;
                while (true)
                {
                    lock (locker)
                    {
                        myStream.Write("<getAllFileInfo>");
                        int numberOfFileServer = myStream.ReadInt();
                        myStream.SendNEXT();
                        ListHolder.DownloadableFiles.Clear();

                        for (int i = 0; i < numberOfFileServer; i++)
                        {
                            int numberOfFile = myStream.ReadInt();
                            myStream.SendNEXT();

                            for (int j = 0; j < numberOfFile; j++)
                            {
                                string fileName = myStream.ReadString();
                                myStream.SendNEXT();

                                long fileSize = myStream.ReadLong();
                                myStream.SendNEXT();

                                string ip = myStream.ReadString();
                                myStream.SendNEXT();

                                int port = myStream.ReadInt();
                                myStream.SendNEXT();

                                ListHolder.DownloadableFiles.Add(new DownloadableFile(fileName, fileSize, ip, port));
                            }
                        }

                        ListHolder.UpdateList();
                    }

                    Thread.Sleep(5000);
                }
            }
            catch (ThreadAbortException)
            {
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Client error: when getting file info from main server");
            }
        }