Пример #1
0
        /// <summary>
        /// Sends a file over the network.
        /// </summary>
        /// <param name="pathToFile">The path to the file to be transferred over the network.</param>
        public void SendFile(string pathToFile)
        {
            var transmittedFile = new TransmittedFile(pathToFile);

            SendFileDetails(transmittedFile);

            byte[] fileContentBuffer = new byte[BufferSize];

            long fileContentLength   = transmittedFile.GetFileContentLength();
            long fileContentByteSent = 0;

            while (fileContentByteSent != fileContentLength)
            {
                int numberOfBytesToSend = ((fileContentLength - fileContentByteSent) / BufferSize > 0) ?
                                          BufferSize : (int)(fileContentLength - fileContentByteSent);

                const int bufferOffset = 0;
                transmittedFile.ReadBytes(fileContentBuffer, bufferOffset,
                                          numberOfBytesToSend, fileContentByteSent);

                _sender.BeginSend(fileContentBuffer, bufferOffset,
                                  fileContentBuffer.Length, SocketFlags.None,
                                  new AsyncCallback(SendFileCallback), _sender);

                fileContentByteSent += numberOfBytesToSend;
                Array.Clear(fileContentBuffer, bufferOffset, fileContentBuffer.Length);
            }
        }
Пример #2
0
        /// <summary>
        /// Sends a file details over the network.
        /// </summary>
        /// <param name="transmittedFile">The file to be transferred over the network.</param>
        private void SendFileDetails(TransmittedFile transmittedFile)
        {
            byte[] fileDetails       = transmittedFile.GetByteArrayFileDetails();
            byte[] fileDetailsLength = BitConverter.GetBytes((long)fileDetails.Length);
            byte[] fileContentLength = BitConverter.GetBytes(transmittedFile.GetFileContentLength());

            byte[] fileDetailsBuffer = new byte[fileDetailsLength.Length
                                                + fileDetails.Length + fileContentLength.Length];

            const int stertIndex = 0;

            fileDetailsLength.CopyTo(fileDetailsBuffer, stertIndex);
            fileDetails.CopyTo(fileDetailsBuffer, fileDetailsLength.Length);
            fileContentLength.CopyTo(fileDetailsBuffer,
                                     fileDetailsLength.Length + fileDetails.Length);

            const int offset = 0;

            _sender.BeginSend(fileDetailsBuffer, offset,
                              fileDetailsBuffer.Length, SocketFlags.None,
                              new AsyncCallback(SendFileCallback), _sender);
        }