Пример #1
0
        /// <summary>
        /// To send GZip file through tcp client
        /// </summary>
        /// <param name="objTrasportData">Data to be send</param>
        public void SendGZipFile(TrasportData objTrasportData)
        {
            Socket socket = tcpClient.Client;

            socket.Send(objTrasportData.GetHeaderBytes());
            //Send data
            if (objTrasportData.FilePath == string.Empty)
            {
                socket.Send(objTrasportData.DataStream.ToArray());
            }
            //Read GZipFile content
            else
            {
                byte[] buffer    = new byte[8192];
                int    readCount = 0;
                int    sum       = 0;

                //Read GZipFile content
                using (Stream file = new GZipStream(new FileStream(objTrasportData.FilePath, FileMode.Open, FileAccess.Read), CompressionMode.Compress))
                {
                    while ((readCount = file.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        socket.Send(buffer, 0, readCount, SocketFlags.None);
                        sum += readCount;
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        ///  Write data to tcp client stream and notify totalByte, totalByteUploaded,success information.
        /// </summary>
        /// <param name="objTrasportData">Data to be send</param>
        /// <param name="totalByte">Total byte to be send</param>
        /// <param name="totalByteUploaded">Total byte sent</param>
        /// <param name="success">Whether data uploaded successfully.</param>
        public void Send(TrasportData objTrasportData, ref long totalByte, ref long totalByteUploaded, ref bool success)
        {
            Socket socket = tcpClient.Client;

            socket.Send(objTrasportData.GetHeaderBytes());

            //Send data
            if (objTrasportData.FilePath == string.Empty)
            {
                totalByte         = objTrasportData.DataStream.Length;
                totalByteUploaded = socket.Send(objTrasportData.DataStream.ToArray());
            }
            //Send file data
            else
            {
                byte[] buffer    = new byte[8192];
                int    readCount = 0;

                //Read file content
                using (FileStream file = new FileStream(objTrasportData.FilePath, FileMode.Open, FileAccess.Read))
                {
                    totalByte = file.Length;
                    while ((readCount = file.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        //If user break
                        if (success == false)
                        {
                            break;
                        }
                        totalByteUploaded += socket.Send(buffer, 0, readCount, SocketFlags.None);
                    }
                }
            }
        }