Ejemplo n.º 1
0
            private async void sendFileNameAndPart(NetworkStream ns, string fileName, long fromByte, long toByte)
            {
                ClientUploadDownload cup = new ClientUploadDownload(fileName, fromByte, toByte);

                string jasonStriing = JsonConvert.SerializeObject(cup);

                byte[] jsonFile       = ASCIIEncoding.ASCII.GetBytes(jasonStriing);
                byte[] jsonFileLength = BitConverter.GetBytes(jsonFile.Length);

                await ns.WriteAsync(jsonFileLength, 0, jsonFileLength.Length);

                await ns.WriteAsync(jsonFile, 0, jsonFile.Length);
            }
Ejemplo n.º 2
0
            public async void getFileToDownload(NetworkStream ns)
            {
                string jsonFile;

                byte[] jsonBytes;
                byte[] jsonLengthBytes = new byte[4];      //int32

                await ns.ReadAsync(jsonLengthBytes, 0, 4); // int32

                jsonBytes = new byte[BitConverter.ToInt32(jsonLengthBytes, 0)];
                await ns.ReadAsync(jsonBytes, 0, jsonBytes.Length);

                jsonFile = ASCIIEncoding.ASCII.GetString(jsonBytes);

                ClientUploadDownload cup = JsonConvert.DeserializeObject <ClientUploadDownload>(jsonFile);

                startSendFile(cup, ns);
            }
Ejemplo n.º 3
0
            private async void startSendFile(ClientUploadDownload cup, NetworkStream ns)
            {
                FileStream     fileStream = null;
                FilesAndStatus fas        = null;

                foreach (FilesAndStatus tempFas in uploadFiles)
                {
                    if (tempFas.FileName.Equals(cup.FileName))
                    {
                        fas = tempFas;
                    }
                }

                try
                {
                    string   path = currentUser.UpLoc + "\\" + cup.FileName;
                    FileInfo file = new FileInfo(path);
                    fileStream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read);

                    long   total     = cup.ToByte - cup.FromByte;
                    long   ToatlSent = 0;
                    long   totalLeft = total;
                    int    len       = 0;
                    byte[] buffer    = new byte[50000];

                    fileStream.Seek(cup.FromByte, 0);
                    while (ToatlSent < total && ns.CanWrite && activeFlag)
                    {
                        //Read from the File (len contains the number of bytes read)
                        if (totalLeft > 50000)
                        {
                            len = fileStream.Read(buffer, 0, buffer.Length);
                        }
                        else
                        {
                            len = fileStream.Read(buffer, 0, (int)totalLeft);
                        }
                        //Write the Bytes on the Socket
                        await ns.WriteAsync(buffer, 0, len);

                        //Increase the bytes Read counter
                        ToatlSent += len;

                        double pctread = ((double)ToatlSent / total) * 100;
                        fas.PbValue = Convert.ToInt32(pctread);
                        fas.Status  = "Uploading";

                        while (bw.IsBusy)
                        {
                            ;
                        }
                        bw.RunWorkerAsync();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("A Exception occured in transfer" + e.ToString());
                    fas.Status = "Error";
                    while (bw.IsBusy)
                    {
                        ;
                    }
                    bw.RunWorkerAsync();
                    MessageBoxResult result = MessageBox.Show("There was a problem with " + cup.FileName + "transfer");
                }
                finally
                {
                    fas.Status = "Completed";
                    while (bw.IsBusy)
                    {
                        ;
                    }
                    bw.RunWorkerAsync();

                    ns.Close();
                    if (fileStream != null)
                    {
                        fileStream.Dispose();
                        fileStream.Close();
                    }
                }
            }