Exemple #1
0
 private void download_btn_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         ArrayList <string> fileData = new ArrayList <string>();
         Information        info     = Information.Instance;
         if (resourcesIp.Contains(info.IpAddress))
         {
             System.Windows.MessageBox.Show("file is allready exists!");
         }
         else
         {
             System.Windows.MessageBox.Show("file name to downlaod: " + fName);
             if (fName.Equals(""))
             {
                 System.Windows.MessageBox.Show("No file was selected!");
             }
             else
             {
                 int index = 0;
                 foreach (string ip in resourcesIp)
                 {
                     dt = DateTime.Now;
                     InitializeSender(ip);
                     string type  = "download";
                     byte[] typeB = Encoding.ASCII.GetBytes(type);
                     Thread.Sleep(4000);
                     strm.Write(typeB, 0, typeB.Length);
                     FileReqMessage fileReq = new FileReqMessage();
                     fileReq.fileName       = fName;
                     fileReq.myIP           = info.IpAddress;
                     fileReq.resourceIndex  = "" + index;
                     fileReq.numOfResources = "" + resourcesIp.Count;
                     string message      = MessageHendler.SerializeAnObject(fileReq);
                     byte[] contentBytes = Encoding.ASCII.GetBytes(message);
                     Thread.Sleep(4000);
                     strm.Write(contentBytes, 0, contentBytes.Length);
                     strm.Close();
                     sendingClient.Close();
                     index++;
                 }
             }
         }
     }
     catch (Exception ex)
     {
         System.Windows.MessageBox.Show("In download progress, please try later");
     }
 }
Exemple #2
0
        /** this is the thread function which recieves messages and listen to clients - server **/
        private void Receiver(Object obj)

        {
            Information info = (Information)obj;

            receivingClient.Start();                                   // strat tcpListener
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, port); // get messages from any ip

            event1 += new AddMessage(MessageReceived);                 // add action listener to delegete
            string message;

            while (true)
            {
                try
                {
                    Socket s = receivingClient.AcceptSocket();
                    /** recieving request type **/
                    byte[] bReq    = new byte[s.SendBufferSize];
                    int    reqSize = s.Receive(bReq);
                    message = "";
                    for (int i = 0; i < reqSize; i++) // convert from byte to string
                    {
                        message += Convert.ToChar(bReq[i]);
                    }

                    // check request type:
                    if (info.IpAddress.Equals(message)) // log out
                    {
                        // closing window request - if recieved ip is equal to my ip - stop listening.
                        break;
                    }

                    else
                    {
                        if (message.Equals("file"))                            // recieve file that i downloaded
                        {
                            byte[] recvFileInfoB = new byte[s.SendBufferSize]; // get file info in bytes
                            int    k             = s.Receive(recvFileInfoB);
                            string recvFileInfo  = "";
                            for (int i = 0; i < k; i++) // convert from byte to string
                            {
                                recvFileInfo += Convert.ToChar(recvFileInfoB[i]);
                            }
                            Array.Clear(recvFileInfoB, 0, recvFileInfoB.Length);
                            DownloadMessage DownloadMessage = new DownloadMessage();                                          // class of downloaded message details
                            Object          ob = MessageHendler.DeSerializeAnObject(recvFileInfo, DownloadMessage.GetType()); // convert from string to DownloadMessage object (2 rows)
                            DownloadMessage myDownloadMessage = (DownloadMessage)ob;

                            int resourceIndex  = int.Parse(myDownloadMessage.index);         // get idx from DownloadMessage obj
                            int countResources = int.Parse(myDownloadMessage.numOfResorces); // get num of resources from DownloadMessage obj
                            int fileSize       = int.Parse(myDownloadMessage.fileSize);      // get file size from DownloadMessage obj

                            string fileInfo = "";
                            fileInfo = info.Path + "\\" + myDownloadMessage.fileName;
                            if (resourceIndex == 0) // create new file only if this is the first source index
                            {
                                File.WriteAllBytes(fileInfo, myDownloadMessage.fileContent);
                            }
                            else // not first source index
                            {
                                using (var stream = new FileStream(fileInfo, FileMode.Append))
                                {
                                    // concat to last part of file
                                    stream.Write(myDownloadMessage.fileContent, 0, myDownloadMessage.fileContent.Length);
                                }
                            }

                            ts = DateTime.Now - dt; // calc dowmload time
                            string downloadedMsg = "";
                            int    idx           = resourceIndex + 1;
                            downloadedMsg += "" + idx + "  / " + countResources + " arrived! \n";
                            downloadedMsg += "total time: " + ts.Seconds + " seconds. \n";
                            downloadedMsg += "total length: " + myDownloadMessage.fileContent.Length + " kb. \n";
                            downloadedMsg += "bit rate: " + myDownloadMessage.fileContent.Length / ts.Seconds + " kb/s. \n";
                            System.Windows.MessageBox.Show(downloadedMsg);
                            if (resourceIndex == countResources - 1) // last resource
                            {
                                // create new MyFile and add it to DB
                                long   length = new System.IO.FileInfo(fileInfo).Length;
                                MyFile myFile = new MyFile();
                                myFile.fileName = myDownloadMessage.fileName;
                                myFile.size     = fileSize;
                                myFile.IP       = info.IpAddress;
                                myFile.port     = info.Port;
                                myFile.path     = info.Path;
                                MessageHendler.insertNewFile(myFile);
                            }
                        }
                        else // another client download file from me
                        {
                            byte[] recvFileInfoB = new byte[s.SendBufferSize]; // get wanted file info
                            int    k             = s.Receive(recvFileInfoB);
                            string recvFileInfo  = "";
                            for (int i = 0; i < k; i++)
                            {
                                recvFileInfo += Convert.ToChar(recvFileInfoB[i]);
                            }
                            Array.Clear(recvFileInfoB, 0, recvFileInfoB.Length);
                            FileReqMessage fileReq   = new FileReqMessage();
                            Object         ob        = MessageHendler.DeSerializeAnObject(recvFileInfo, fileReq.GetType());      // convert from string to FileReqMessage obj (2 rows)
                            FileReqMessage myfileReq = (FileReqMessage)ob;
                            event1(myfileReq.fileName, myfileReq.myIP, info, myfileReq.resourceIndex, myfileReq.numOfResources); // call delegete func
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show("In download progress, please try later");
                }
            }

            receivingClient.Stop(); // stop listening
            System.Windows.MessageBox.Show("bye bye");
            return;
        }