日期:2014-05-18 浏览次数:20911 次
public static ManualResetEvent HallallDone = new ManualResetEvent(false); byte[] recByte = new byte[1024]; private TcpClient hallclient; private void readhallclient() { HallallDone.WaitOne(); AsyncCallback GetStreamMsgCallback = new AsyncCallback(hallReadCallBack); hallclient.GetStream().BeginRead(recByte, 0, recByte.Length, GetStreamMsgCallback, null); //出错的位置,报IO异常:无法从传输连接中读取数据: 由于系统缓冲区空间不足或列队已满,不能执行套接字上的操作。。 readhallclient(); } private void hallReadCallBack(IAsyncResult ar) { int numberOfBytesRead; bool isFinish = true; try { lock ( hallclient.GetStream()) { numberOfBytesRead = hallclient.GetStream().EndRead(ar); if (numberOfBytesRead < 1) { Console.WriteLine("消息长度小于1----Player"); return; } } sb.AddBytes(recByte, numberOfBytesRead); recByte = new byte[1024]; if (hallclient.GetStream().DataAvailable) { isFinish = false; hallclient.GetStream().BeginRead(recByte, 0, recByte.Length, new AsyncCallback(hallReadCallBack), hallclient.GetStream()); } if (isFinish) { string msg = StrHelper.ascarr2str(sb.ReceiveAllByte); msg = msg.TrimEnd('\0'); string result = listenedMsgEffect(msg,CodeControl.LocalCode.Localcode); if (result!=null&&result != "") { writehallclient(result); } sb.Dispose(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }