WEB开发网
开发学院软件开发C语言 C#网络编程(异步传输字符串) - Part.3 阅读

C#网络编程(异步传输字符串) - Part.3

 2009-03-26 08:20:01 来源:WEB开发网   
核心提示: public class RemoteClient { private TcpClient client; private NetworkStream streamToClient; private const int BufferSize = 8192; private byte[] b

public class RemoteClient {
    private TcpClient client;
    private NetworkStream streamToClient;
    private const int BufferSize = 8192;
    private byte[] buffer;
    private RequestHandler handler;
   
    public RemoteClient(TcpClient client) {
        this.client = client;

        // 打印连接到的客户端信息
        Console.WriteLine("nClient Connected!{0} <-- {1}",
            client.Client.LocalEndPoint, client.Client.RemoteEndPoint);

        // 获得流
        streamToClient = client.GetStream();
        buffer = new byte[BufferSize];

        // 设置RequestHandler
        handler = new RequestHandler();

        // 在构造函数中就开始准备读取
        AsyncCallback callBack = new AsyncCallback(ReadComplete);
        streamToClient.BeginRead(buffer, 0, BufferSize, callBack, null);
    }

    // 再读取完成时进行回调
    private void ReadComplete(IAsyncResult ar) {
        int bytesRead = 0;
        try {
            lock (streamToClient) {
                bytesRead = streamToClient.EndRead(ar);
                Console.WriteLine("
Reading data, {0} bytes ...", bytesRead);
            }
            if (bytesRead == 0) throw new Exception("读取到0字节");

            string msg = Encoding.Unicode.GetString(buffer, 0, bytesRead);
            Array.Clear(buffer,0,buffer.Length);        // 清空缓存,避免脏读
       
            string[] msgArray = handler.GetActualString(msg);   // 获取实际的字符串

            // 遍历获得到的字符串
            foreach (string m in msgArray) {
                Console.WriteLine("Received: {0}", m);
                string back = m.ToUpper();

                // 将得到的字符串改为大写并重新发送
                byte[] temp = Encoding.Unicode.GetBytes(back);
                streamToClient.Write(temp, 0, temp.Length);
                streamToClient.Flush();
                Console.WriteLine("Sent: {0}", back);
            }              

            // 再次调用BeginRead(),完成时调用自身,形成无限循环
            lock (streamToClient) {
                AsyncCallback callBack = new AsyncCallback(ReadComplete);
                streamToClient.BeginRead(buffer, 0, BufferSize, callBack, null);
            }
        } catch(Exception ex) {
            if(streamToClient!=null)
                streamToClient.Dispose();
            client.Close();
            Console.WriteLine(ex.Message);      // 捕获异常时退出程序             
        }
    }
}

上一页  1 2 3 4 5 6 7 8  下一页

Tags:网络编程 异步 传输

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接