C#:一个增强的TcpClient(三)读取
来源:互联网 发布:win10点网络图标没反应 编辑:程序博客网 时间:2024/05/22 08:24
using System;using System.IO;using System.Net.Sockets;using System.Text;using System.Threading;namespace Splash.Net.Sockets{ /// <summary> /// 异步读状态对象 /// </summary> internal class AsyncReadStateObject { public ManualResetEvent eventDone; public NetworkStream stream; public Exception exception; public Int32 numberOfBytesRead; } /// <summary> /// 实现TcpClient的异步接收 /// </summary> public partial class TcpClientPlus { /// <summary> /// 设置读超时等待值 /// </summary> public Int32 ReadTimeout = Timeout.Infinite; /// <summary> /// 异步接收 /// </summary> /// <param name="data">接收到的字节数组</param> public void Read(out Byte[] data) { // 获取网络数据流 NetworkStream netStream = GetStream(); // 用户定义对象 AsyncReadStateObject State = new AsyncReadStateObject { // 将事件状态设置为非终止状态,导致线程阻止 eventDone = new ManualResetEvent(false), stream = netStream, exception = null, numberOfBytesRead = -1 }; Byte[] Buffer = new Byte[ReceiveBufferSize]; using (MemoryStream memStream = new MemoryStream(ReceiveBufferSize)) { Int32 TotalBytes = 0; // 总共需要接收的字节数 Int32 ReceivedBytes = 0; // 当前已接收的字节数 while (true) { // 将事件状态设置为非终止状态,导致线程阻止 State.eventDone.Reset(); // 异步读取网络数据流 netStream.BeginRead(Buffer, 0, Buffer.Length, new AsyncCallback(AsyncReadCallback), State); // 等待操作完成信号 if (State.eventDone.WaitOne(ReadTimeout, false)) { // 接收到信号 if (State.exception != null) throw State.exception; if (State.numberOfBytesRead == 0) { // 连接已经断开 throw new SocketException(); } else if (State.numberOfBytesRead > 0) { if (TotalBytes == 0) { // 提取流头部字节长度信息 TotalBytes = BitConverter.ToInt32(Buffer, 0); // 保存剩余信息 memStream.Write(Buffer, 4, State.numberOfBytesRead - 4); } else { memStream.Write(Buffer, 0, State.numberOfBytesRead); } ReceivedBytes += State.numberOfBytesRead; if (ReceivedBytes >= TotalBytes) break; } } else { // 超时异常 throw new TimeoutException(); } } // 将流内容写入字节数组 if (String.IsNullOrEmpty(_secretKey)) { data = (memStream.Length > 0) ? memStream.ToArray() : null; } else { // 解密数据 data = (memStream.Length > 0) ? Decrypt(memStream.ToArray(), 0, TotalBytes - 4) : null; } } } /// <summary> /// 异步接收 /// </summary> /// <param name="answer">接收到的字符串</param> /// <param name="codePage">代码页</param> /// <remarks> /// 代码页: /// 936:简体中文GB2312 /// 54936:简体中文GB18030 /// 950:繁体中文BIG5 /// 1252:西欧字符CP1252 /// 65001:UTF-8编码 /// </remarks> public void Read(out String answer, Int32 codePage = 65001) { Byte[] data; Read(out data); answer = (data != null) ? Encoding.GetEncoding(codePage).GetString(data) : null; } /// <summary> /// 异步读取回调函数 /// </summary> /// <param name="ar">异步操作结果</param> private static void AsyncReadCallback(IAsyncResult ar) { AsyncReadStateObject State = ar.AsyncState as AsyncReadStateObject; try { // 异步写入结束 State.numberOfBytesRead = State.stream.EndRead(ar); } catch (Exception e) { // 异步连接异常 State.exception = e; } finally { // 将事件状态设置为终止状态,线程继续 State.eventDone.Set(); } } }}
- C#:一个增强的TcpClient(三)读取
- C#:一个增强的TcpClient(一)连接
- C#:一个增强的TcpClient(二)写入
- C#:一个增强的TcpClient(四)查询
- C#:一个增强的TcpClient(五)构造函数
- C#:一个增强的TcpClient(六)数据加密
- C#:一个增强的TcpClient(七)独立通信线程
- C#:一个增强的NetworkStream(三)读取
- C#:一个增强的TcpClient(*)客户端演示程序及源代码下载
- C#TCPClient应用-一个简单的消息发送和接收
- tcpclient.c
- C#:一个增强的NetworkStream(一)构造函数
- C#:一个增强的NetworkStream(二)写入
- C#:一个增强的NetworkStream(四)数据加密
- C#:一个增强的TcpListener(一)构造函数
- C#:一个增强的TcpListener(二)线程池
- C#:一个增强的TcpListener(二)线程池
- C#:一个增强的TcpListener(一)构造函数
- Outlook 2007技巧两则
- 事件详解
- ubuntu gedit中文乱码解决办法
- Excel 内取出数字和文字 标注重复项(用颜色区分重复的数据)有图
- iPhone开发笔记 (6) [UIColor colorWithRed:(CGFloat) green:(CGFloat) blue:(CGFloat) alpha:(CGFloat)]
- C#:一个增强的TcpClient(三)读取
- window.location详解
- C#:一个增强的TcpClient(四)查询
- qt 静态编译
- Java英文面试测试
- POJ 2774 Long Long Message
- linux下repair filesystem模式修复方法实践
- 关于C#[StructLayout(LayoutKind.Sequential)]的问题
- VC 获取汉字拼音首字母(unicode编码)下