Exemplo n.º 1
0
        private void Download(DownloadableFile file)
        {
            try
            {
                TcpClient  client   = new TcpClient(IPBuilder.GetIP());
                IPEndPoint serverIP = IPBuilder.GetIP(file.IP, file.Port);

                client.Connect(serverIP);
                client.Client.ReceiveTimeout = 7000;
                client.Client.SendTimeout    = 7000;

                MyStreamIO myStream = new MyStreamIO(client.GetStream());

                myStream.Write("<isClient>");
                myStream.GetNEXT();

                myStream.Write(file.FileName);
                string rely = myStream.ReadString();

                if (rely != "<fileFound>")
                {
                    client.GetStream().Close();
                    client.Close();
                    return;
                }

                IPEndPoint udpClientIP = IPBuilder.GetIP();
                UdpClient  udpClient   = new UdpClient(udpClientIP);


                myStream.Write(udpClientIP.Address.ToString());
                string udpListenerIP = myStream.ReadString();

                myStream.Write(udpClientIP.Port);
                int udpListenerPort = myStream.ReadInt();

                client.GetStream().Close();
                client.Close();

                UdpDownload(udpClient, IPBuilder.GetIP(udpListenerIP, udpListenerPort), file);

                udpClient.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Client error: when getting file info for download from file server");
            }
        }
Exemplo n.º 2
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");
            }
        }