Exemplo n.º 1
0
        public void SendSharedFile(object Args)
        {
            object[] args     = (object[])Args;
            string   FileName = (string)args[0];
            long     FileSize = (long)args[1];
            int      NoParts  = (int)args[2];


            string filePath = FileSharing.sharedFiles.Select(P => P).Where(P => P.EndsWith(FileName)).ToList <string>()[0];

            /////Split//////
            List <BellaFile> Parts = new List <BellaFile>();

            if (NoParts != 1)
            {
                try
                {
                    FileStream _File        = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    int        noOfParts    = NoParts;
                    int        sizeOfPart   = (int)Math.Ceiling((double)_File.Length / noOfParts);
                    string     baseFileName = Path.GetFileNameWithoutExtension(filePath);
                    string     Extension    = Path.GetExtension(filePath);
                    for (int i = 0; i < noOfParts; i++)
                    {
                        string PartName = Path.GetDirectoryName(filePath) + @"\" + baseFileName + "." +
                                          i.ToString().PadLeft(5, Convert.ToChar("0")) + Extension + ".tmp";
                        FileStream outputFile = new FileStream(PartName, FileMode.Create, FileAccess.Write);
                        int        bytesRead  = 0;
                        byte[]     buffer     = new byte[sizeOfPart];
                        if ((bytesRead = _File.Read(buffer, 0, sizeOfPart)) > 0)
                        {
                            outputFile.Write(buffer, 0, bytesRead);
                            Parts.Add(new BellaFile()
                            {
                                Id = i, Size = bytesRead, Name = Path.GetFileName(PartName), Path = PartName
                            });
                        }
                        outputFile.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            /////////

            try
            {
                if (!isConnected)
                {
                    //_Client = new TcpClient();
                    //_Client.Connect(new IPEndPoint(IPAddress.Parse(_PeerAddr.Address.ToString()), _PeerPort));
                    //isConnected = true;
                    this.btnConnect(null, null);
                }
                Stream     stream   = _Client.GetStream();
                int        no_parts = NoParts;
                FileStream _file    = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                string Message = "take:" + no_parts;
                byte[] data    = new byte[Message.Length + 2];
                data = Encoding.ASCII.GetBytes(Message.ToCharArray());
                stream.Write(data, 0, data.Length);

                ////////////////
                {
                    byte[] msg = new byte[100];
                    stream.Read(msg, 0, msg.Length);
                    string ack = Encoding.ASCII.GetString(msg);
                }
                ////////////////


                if (NoParts > 1)
                {
                    List <string> tempFiles = new List <string>();
                    foreach (var item in Parts)
                    {
                        BellaFile _Part = (BellaFile)item;
                        _file = new FileStream(_Part.Path, FileMode.Open, FileAccess.Read);
                        byte[] buffer = new byte[_file.Length];
                        _file.Read(buffer, 0, (int)_file.Length);

                        string fileMsg = "file~" + _file.Length + "~" + Path.GetFileName(_file.Name);
                        byte[] msg     = new byte[fileMsg.Length];
                        msg = Encoding.ASCII.GetBytes(fileMsg.ToCharArray());
                        stream.Write(msg, 0, msg.Length);

                        ////////////////
                        {
                            byte[] m = new byte[100];
                            stream.Read(m, 0, m.Length);
                            string ack = Encoding.ASCII.GetString(m);
                        }
                        ////////////////

                        stream.Write(buffer, 0, (int)_file.Length);
                        tempFiles.Add(_file.Name);
                        _file.Close();
                    }
                    foreach (string tempFile in tempFiles)
                    {
                        File.Delete(tempFile);
                    }
                    Dispatcher.Invoke((Action) delegate
                    {
                        Parts.Clear();
                    });
                    MessageBox.Show("Bella :-) sent all the parts successfully!");
                }
                else
                {
                    string fileMsg = "file~" + _file.Length + "~" + Path.GetFileName(_file.Name);
                    byte[] msg     = new byte[fileMsg.Length];
                    msg = Encoding.ASCII.GetBytes(fileMsg.ToCharArray());
                    stream.Write(msg, 0, msg.Length);

                    ////////////////
                    {
                        byte[] m = new byte[100];
                        stream.Read(m, 0, m.Length);
                        string ack = Encoding.ASCII.GetString(m);
                    }
                    ////////////////

                    byte[] buffer = new byte[_file.Length];
                    _file.Read(buffer, 0, (int)_file.Length);
                    stream.Write(buffer, 0, (int)_file.Length);
                    MessageBox.Show("Bella =) sent something sucessfully!");
                    _file.Close();
                }
                stream.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Exemplo n.º 2
0
        public void SendFile(object FilePath)
        {
            string filePath = (string)FilePath;

            try
            {
                if (!isConnected)
                {
                    _Client = new TcpClient();
                    _Client.Connect(new IPEndPoint(IPAddress.Parse(_PeerAddr.Address.ToString()), _PeerPort));
                    isConnected = true;
                }
                Stream     stream   = _Client.GetStream();
                int        no_parts = (lstParts.Items.Count == 0) ? 1 : lstParts.Items.Count;
                FileStream _file    = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                string Message = "take:" + no_parts;
                byte[] data    = new byte[Message.Length + 2];
                data = Encoding.ASCII.GetBytes(Message.ToCharArray());
                stream.Write(data, 0, data.Length);

                ////////////////
                {
                    byte[] msg = new byte[100];
                    stream.Read(msg, 0, msg.Length);
                    string ack = Encoding.ASCII.GetString(msg);
                }
                ////////////////


                if (lstParts.Items.Count != 0)
                {// Multi Parts
                    List <string> tempFiles = new List <string>();
                    int           i         = 0;
                    foreach (var item in lstParts.Items)
                    {
                        BellaFile _Part = (BellaFile)item;
                        _file = new FileStream(_Part.Path, FileMode.Open, FileAccess.Read);
                        byte[] buffer = new byte[_file.Length];
                        _file.Read(buffer, 0, (int)_file.Length);

                        string fileMsg = "file~" + _file.Length + "~" + Path.GetFileName(_file.Name);
                        byte[] msg     = new byte[fileMsg.Length];
                        msg = Encoding.ASCII.GetBytes(fileMsg.ToCharArray());
                        stream.Write(msg, 0, msg.Length);

                        ////////////////
                        {
                            byte[] m = new byte[100];
                            stream.Read(m, 0, m.Length);
                            string ack = Encoding.ASCII.GetString(m);
                        }
                        ////////////////

                        stream.Write(buffer, 0, (int)_file.Length);
                        tempFiles.Add(_file.Name);
                        _file.Close();
                        App.Current.Dispatcher.Invoke((Action) delegate
                        {
                            progressBar.Value = (((double)++i) / no_parts) * 100;
                        }, System.Windows.Threading.DispatcherPriority.Send);
                    }
                    foreach (string tempFile in tempFiles)
                    {
                        File.Delete(tempFile);
                    }
                    Dispatcher.Invoke((Action) delegate
                    {
                        lstParts.Items.Clear();
                    });
                    MessageBox.Show("You sent all the parts successfully!");
                }
                else
                {
                    string fileMsg = "file~" + _file.Length + "~" + Path.GetFileName(_file.Name);
                    byte[] msg     = new byte[fileMsg.Length];
                    msg = Encoding.ASCII.GetBytes(fileMsg.ToCharArray());
                    stream.Write(msg, 0, msg.Length);

                    ////////////////
                    {
                        byte[] m = new byte[100];
                        stream.Read(m, 0, m.Length);
                        string ack = Encoding.ASCII.GetString(m);
                    }
                    ////////////////

                    byte[] buffer = new byte[_file.Length];
                    _file.Read(buffer, 0, (int)_file.Length);
                    stream.Write(buffer, 0, (int)_file.Length);
                    MessageBox.Show("You sent something sucessfully!");
                    _file.Close();
                    App.Current.Dispatcher.Invoke((Action) delegate
                    {
                        progressBar.Value = 100;
                    });
                }
                stream.Close();
                _Client.Close();
                isConnected = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }