注册 登录
编程论坛 VC.NET论坛

有关异步监听的问题

dlyltm 发布于 2011-07-29 09:21, 1807 次点击
我做了一个服务器异步监听、异步读取数据的程序,主要代码如下:
 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 编辑 ]
0 回复
1