Пример #1
0
        public void Sending(SingleFileTransfer sft)
        {
            var buffer    = new byte[256 * 1024]; // good enough
            int bytesRead = sft.CurrentFileStream.Read(buffer, 0, buffer.Length);

            if (bytesRead > 0 && sft.FileLength > 0)
            {
                sft.CurrentNetworkStream.Write(buffer, 0, bytesRead);
                sft.FileLength -= bytesRead;
            }
        }
Пример #2
0
        // Receiving a chunk of data (to be in a while loop)
        public void Receive(SingleFileTransfer sft)
        {
            var buffer    = new byte[256 * 1024]; // my default size of buffer to receive
            int bytesRead = sft.CurrentNetworkStream.Read(buffer, 0, buffer.Length);

            if (bytesRead > 0 && sft.FileLength > 0)
            {
                sft.CurrentFileStream.Write(buffer, 0, bytesRead);
                sft.FileLength -= bytesRead;
            }
            else
            {
                throw new SocketException(1);
            }
        }
Пример #3
0
        public SingleFileTransfer StartSending(string FilePath, MyEndpoint WantedEndpoint)
        {
            SingleFileTransfer sft = new SingleFileTransfer();

            sft.HostName = WantedEndpoint.Name;
            TcpClient tcpClient = new TcpClient();

            tcpClient.Connect(WantedEndpoint.Endpoint); // Connecting to the client specified (throws an exception if not available)

            // checking if it's a directory: if so, then compressing
            if (Directory.Exists(FilePath))
            {
                string tempPath = Path.GetTempPath() + new DirectoryInfo(FilePath).Name + ".zip";
                if (File.Exists(tempPath)) // Check if the file already exists so that ZipFile doesn't throw an exception
                {
                    File.Delete(tempPath);
                }
                ZipFile.CreateFromDirectory(FilePath, tempPath);
                FilePath = tempPath;
            }
            // New Stream opening
            FileInfo fi = new FileInfo(FilePath); // Obtaining the infos of the specified file

            sft.Name                 = fi.Name;
            sft.FileLength           = fi.Length;
            sft.CurrentNetworkStream = tcpClient.GetStream();
            sft.CurrentNetworkStream.WriteTimeout = 20000; // seems good enough 20 seconds
            // Sending UserName, FileName and FileLength
            byte[] hostNameLengthBuffer = BitConverter.GetBytes(Encoding.Unicode.GetByteCount(Environment.UserName));
            sft.CurrentNetworkStream.Write(hostNameLengthBuffer, 0, hostNameLengthBuffer.Length);
            // Sending
            byte[] hostNameBuffer = Encoding.Unicode.GetBytes(Environment.UserName);
            sft.CurrentNetworkStream.Write(hostNameBuffer, 0, hostNameBuffer.Length);
            // Sending
            byte[] fileNameLengthBuffer = BitConverter.GetBytes(Encoding.Unicode.GetByteCount(sft.Name));
            sft.CurrentNetworkStream.Write(fileNameLengthBuffer, 0, fileNameLengthBuffer.Length);
            // Sending
            byte[] fileNameBuffer = Encoding.Unicode.GetBytes(sft.Name);
            sft.CurrentNetworkStream.Write(fileNameBuffer, 0, fileNameBuffer.Length);
            // Sending
            byte[] fileLengthBuffer = BitConverter.GetBytes(sft.FileLength);
            sft.CurrentNetworkStream.Write(fileLengthBuffer, 0, fileLengthBuffer.Length);
            // Opening FileStream
            sft.CurrentFileStream = File.OpenRead(FilePath);
            return(sft);
        }
Пример #4
0
        // Starting receiving the first metadata of the file (host name, file name and file size).
        public SingleFileTransfer StartReceiving(TcpClient client)
        {
            SingleFileTransfer sft       = new SingleFileTransfer();
            NetworkStream      netStream = client.GetStream();

            // Pattern of receiving as MyServer:
            // Receiving UserName, FileName and FileLength
            byte[] hostNameLengthBuffer = new byte[sizeof(int)];
            netStream.Read(hostNameLengthBuffer, 0, hostNameLengthBuffer.Length);
            int hostNameLength = BitConverter.ToInt32(hostNameLengthBuffer, 0);

            // Receiving
            byte[] hostNameBuffer = new byte[hostNameLength];
            netStream.Read(hostNameBuffer, 0, hostNameBuffer.Length);
            // Receiving
            byte[] fileNameLengthBuffer = new byte[sizeof(int)];
            netStream.Read(fileNameLengthBuffer, 0, fileNameLengthBuffer.Length);
            int fileNameLength = BitConverter.ToInt32(fileNameLengthBuffer, 0);

            // Receiving
            byte[] fileNameBuffer = new byte[fileNameLength];
            netStream.Read(fileNameBuffer, 0, fileNameBuffer.Length);
            // Receiving
            byte[] fileLengthBuffer = new byte[sizeof(long)];
            netStream.Read(fileLengthBuffer, 0, fileLengthBuffer.Length);

            string hostName   = Encoding.Unicode.GetString(hostNameBuffer);
            string fileName   = Encoding.Unicode.GetString(fileNameBuffer);
            long   fileLength = BitConverter.ToInt64(fileLengthBuffer, 0);

            sft.HostName             = hostName;
            sft.Name                 = fileName;
            sft.Path                 = Path + "//" + fileName;
            sft.FileLength           = fileLength;
            sft.CurrentNetworkStream = netStream;
            sft.CurrentFileStream    = null; // N.B. management is done on the GUI
            return(sft);
        }
Пример #5
0
 // stopping gracefully
 public void CancelSending(SingleFileTransfer sft)
 {
     sft.CurrentNetworkStream.Dispose();
     sft.CurrentFileStream.Dispose();
 }
Пример #6
0
 // stopping gracefully
 public void StopSending(SingleFileTransfer sft)
 {
     sft.CurrentNetworkStream.Flush();
     sft.CurrentNetworkStream.Dispose();
     sft.CurrentFileStream.Dispose();
 }
Пример #7
0
 // Stopping the file reception and deletes the file stored.
 public void CancelReceiving(SingleFileTransfer sft)
 {
     sft.CurrentNetworkStream.Dispose();
     sft.CurrentFileStream?.Dispose();
     File.Delete(sft.Path);
 }
Пример #8
0
 // Ending gracefully the file reception.
 public void EndReceiving(SingleFileTransfer sft)
 {
     sft.CurrentFileStream.Flush();
     sft.CurrentNetworkStream.Dispose();
     sft.CurrentFileStream.Dispose();
 }