コード例 #1
0
        /// <summary>
        /// 传送文件到服务器.
        /// </summary>
        /// <param name="client">
        /// The client.
        /// </param>
        /// <param name="compressFile">
        /// The compress file.
        /// </param>
        /// <param name="offset">
        /// The offset.
        /// </param>
        /// <param name="ask">
        /// The ask.
        /// </param>
        /// <param name="act">
        /// The act.
        /// </param>
        /// <param name="onprogress">
        /// The onprogress.
        /// </param>
        /// <returns>
        /// 成功返回NULL, 失败返回错误信息.
        /// </returns>
        private static string TransferFile(Socket client, string compressFile, int offset, out string ask, RequestHeaderAction act, EventHandler onprogress)
        {
            ask = null;
            string error;
            try
            {
                // 文件长度
                var fileLength = (int)new FileInfo(compressFile).Length;

                // 发送报头
                new RequestHeader { Action = act, BodyLength = (uint)(fileLength - offset) }.SendToServer(client);

                // 接收, 解析应答
                ResponseMessage repMsg;
                error = ParseResponse(client.Receive(ReceiveBuffer), ReceiveBuffer, out repMsg);
                if (error != null)
                    return error;
                if (repMsg.Action == ResponseMessageAction.Stop)
                    return repMsg.Message;

                // 服务器询问文件覆盖模式
                if (repMsg.Action == ResponseMessageAction.Ask)
                {
                    ask = repMsg.Message;
                }
                else
                {
                    // 成功, 继续发送报身
                    using (var fs = File.Open(compressFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        // fs指针移动到指定偏移量
                        fs.Seek(offset, SeekOrigin.Begin);

                        int readLength;
                        var sendAllLength = 0;

                        while ((readLength = fs.Read(SendBuffer, 0, SendBuffer.Length)) > 0)
                        {
                            sendAllLength += client.Send(SendBuffer, 0, readLength, SocketFlags.None);
                            // 进度事件
                            onprogress(new[] { sendAllLength + offset, fileLength }, null);
                        }
                    }

                    // 接收, 解析应答
                    error = ParseResponse(client.Receive(ReceiveBuffer), ReceiveBuffer, out repMsg);
                    if (error != null)
                        return error;
                    if (repMsg.Action == ResponseMessageAction.Stop)
                        return repMsg.Message;
                }
            }
            catch (Exception exp)
            {
                error = exp.Message;
            }

            return error;
        }
コード例 #2
0
        /// <summary>
        /// 发送消息头(没有正文的情况)
        /// </summary>
        /// <param name="client">
        /// The client.
        /// </param>
        /// <param name="act">
        /// The act.
        /// </param>
        /// <returns>
        /// 成功返回NULL, 失败返回错误信息.
        /// </returns>
        private static string SendHeaderSimple(Socket client, RequestHeaderAction act)
        {
            string error;
            try
            {
                // 发送报头
                new RequestHeader { Action = act, BodyLength = 0 }.SendToServer(client);

                // 接收, 解析应答
                ResponseMessage repMsg;
                error = ParseResponse(client.Receive(ReceiveBuffer), ReceiveBuffer, out repMsg);
                if (error != null)
                    return error;
                if (repMsg.Action == ResponseMessageAction.Stop)
                    return repMsg.Message;
            }
            catch (Exception exp)
            {
                error = exp.Message;
            }

            return error;
        }