/// <summary>
        /// 从指定的网络流中获取数据并写入到输出流中。
        /// </summary>
        /// <param name="networkStream">与客户端连接的网络流。</param>
        /// <param name="dataSize">指定要读取的数据最大大小。</param>
        /// <param name="outputStream">数据输出流。</param>
        /// <param name="bufferSize">设置允许使用的缓冲区最大大小。</param>
        /// <param name="transferSizeChanged">已经传输的数据大小更改时引发此事件。设置为 null 则忽略此参数。</param>
        /// <returns>实际接收到的数据大小。</returns>
        public static long ReadDataFromNetwork(System.Net.Sockets.NetworkStream networkStream, long dataSize, System.IO.Stream outputStream, int bufferSize, TransferPositionChangedHandler transferSizeChanged)
        {
            long tLen = 0;

            byte[] buff = new byte[dataSize < bufferSize ? dataSize : bufferSize];
            int    len;
            int    tRead;
            long   rightSize;       //剩余待传输的数据大小。

            while (tLen < dataSize) // && networkStream.CanRead
            {
                rightSize = dataSize - tLen;
                tRead     = System.Convert.ToInt32(rightSize > buff.Length ? buff.Length : rightSize);
                len       = networkStream.Read(buff, 0, tRead);
                if (len == 0)
                {
                    break;
                }
                outputStream.Write(buff, 0, len);
                tLen += len;
                if (transferSizeChanged != null)
                {
                    TransferPositionChangedEventArgs e = new TransferPositionChangedEventArgs(outputStream.Position);
                    transferSizeChanged(e);
                    if (e.AbortTransfer)
                    {
                        break;
                    }
                }
            }
            return(tLen);
        }
        /// <summary>
        /// 以指定的发送规则发送数据到指定的网络流,数据来自输入流“inputStream”。
        /// </summary>
        /// <param name="networkStream">与客户端连接的网络流。</param>
        /// <param name="inputStream">数据输入流。</param>
        /// <param name="sleep">发送数据间隔时间。</param>
        /// <param name="dataSize">每次发送数据包大小。</param>
        /// <param name="firstDataSize">首次发送数据包大小。</param>
        /// <param name="transferSizeChanged">已经传输的数据大小更改时引发此事件。设置为 null 则忽略此参数。</param>
        /// <returns>实际发送的数据大小。</returns>
        public static long WriteDataToNetwork(System.Net.Sockets.NetworkStream networkStream, System.IO.Stream inputStream, System.TimeSpan sleep, int dataSize, int firstDataSize, TransferPositionChangedHandler transferSizeChanged)
        {
            //if (!NetworkStream.CanWrite)
            //{
            //    throw new System.Exception("网络文件流不支持写入操作。");
            //}
            //this.OutputLog("[IP=" + RemoteClient.RemoteIP.ToString() + ":" + RemoteClient.RemotePort.ToString() + "] 开始传送文件:" + FileName);
            //if (!networkStream.CanWrite)
            //{
            //    return;
            //}
            if (inputStream.Length < firstDataSize)
            {
                firstDataSize = System.Convert.ToInt32(inputStream.Length);
            }
            if (inputStream.Length < dataSize)
            {
                dataSize = System.Convert.ToInt32(inputStream.Length);
            }
            int bufferLength = dataSize > firstDataSize ? dataSize : firstDataSize;

            byte[] buffer = new byte[bufferLength];
            //if (!networkStream.CanWrite)
            //{
            //    return;
            //}
            //try
            //{
            #region 开始写入数据。
            long tLen = 0;
            //long currentSize = inputStream.Position;
            int len = inputStream.Read(buffer, 0, firstDataSize);
            while (len != 0)
            {
                //if (!networkStream.CanWrite)
                //{
                //    break;
                //}
                networkStream.Write(buffer, 0, len);
                tLen += len;
                //currentSize += len;
                if (transferSizeChanged != null)
                {
                    TransferPositionChangedEventArgs e = new TransferPositionChangedEventArgs(inputStream.Position);
                    transferSizeChanged(e);
                    if (e.AbortTransfer)
                    {
                        break;
                    }
                }
                if (sleep > System.TimeSpan.Zero)
                {
                    //if (!networkStream.CanWrite)
                    //{
                    //    break;
                    //}
                    System.Threading.Thread.Sleep(sleep);//控制每隔多长时间发送一次数据。
                }
                //if (!networkStream.CanWrite)
                //{
                //    break;
                //}
                len = inputStream.Read(buffer, 0, bufferLength);
            }
            #endregion

            //}
            //catch (System.Threading.ThreadAbortException taex)
            //{
            //    //this.OutputLog(taex.ToString());
            //}
            //catch (System.IO.IOException ioex)
            //{
            //    /*此处 catch 用于截获下面的因属于正常的下载文件完成 IE 客户端先断开连接引发的异常:
            //    System.IO.IOException: 无法将数据写入传输连接: 远程主机强迫关闭了一个现有的连接。。 ---> System.Net.Sockets.SocketException: 远程主机强迫关闭了一个现有的连接。
            //       在 System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
            //       在 System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
            //       --- 内部异常堆栈跟踪的结尾 ---
            //       在 System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
            //       在 IEFileServer.Form1.SendFileToNetwork(String FileName, NetworkStream NetworkStream) 位置 D:\Documents and Settings\桌面\IEFileServer\IEFileServer\Form1.cs:行号 157
            //     */
            //    //this.OutputLog(ioex.ToString());
            //}
            //catch (System.ObjectDisposedException odex)
            //{
            //    //this.OutputLog(odex.ToString());
            //}

            return(tLen);
        }