标题:有关异步监听的问题
只看楼主
dlyltm
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2011-7-28
 问题点数:0 回复次数:0 
有关异步监听的问题
我做了一个服务器异步监听、异步读取数据的程序,主要代码如下:
 TcpListener^ RemotePM;
 static ManualResetEvent ^TcpClientConnected=gcnew ManualResetEvent(false);
 static int numberOfBytesRead=0;
 RemotePM=gcnew TcpListener(IPAddress::Any,7010);
 RemotePM->Start();
 RemotePM->BeginAcceptTcpClient(gcnew AsyncCallback(DoAcceptTcpClientCallback),RemotePM);

 static void DoAcceptTcpClientCallback(IAsyncResult^ result)
      {
          TcpClientConnected->Reset();
          // Get the listener that handles the client request.
  
          TcpListener^ listener = (TcpListener^) result->AsyncState;
  
  
          TcpClient ^client = listener->EndAcceptTcpClient(result);
          client->ReceiveBufferSize=2048;
          client->GetStream()->BeginRead( myReadBuffer, 0, myReadBuffer->Length,
              gcnew AsyncCallback( myReadCallBack ), client->GetStream());
          TcpClientConnected->WaitOne();
          listener->BeginAcceptTcpClient(gcnew AsyncCallback(DoAcceptTcpClientCallback),listener);
      }

      static void myReadCallBack( IAsyncResult^ ar )
      {
        NetworkStream^ myNetworkStream = safe_cast<NetworkStream^>(ar->AsyncState);
        numberOfBytesRead += myNetworkStream->EndRead( ar );
        while ( myNetworkStream->DataAvailable )
            myNetworkStream->BeginRead( myReadBuffer, numberOfBytesRead, myReadBuffer->Length- numberOfBytesRead, gcnew AsyncCallback( myReadCallBack ), myNetworkStream );   
        TcpClientConnected->Set();
      }

现在的问题是这个过程只能收到1个包,收完第一个包以后就再也没反应,myReadCallBack也再没进去。这究竟是何缘故?请高手指教。

[ 本帖最后由 dlyltm 于 2011-7-29 10:08 编辑 ]
搜索更多相关主题的帖子: 服务器 问题 listener result false 
2011-07-29 09:21



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-346331-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.532335 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved