日期:2014-05-17  浏览次数:21007 次

Socket异步接收发送问题
想问下socket 异步接收事件问题 在服务器跟客户端发生过一次数据交换后需要先通过Shutdown() 跟Close释放一次连接清除通信缓存么? 这样的话服务器跟客户端通信岂不是每次通信前都需要连接?



 public void Receive(IAsyncResult iar)//接收数据的方法
         {
             //try
             //{
                 String DateString = string.Empty;

                 ObjectSocket os = (ObjectSocket)iar.AsyncState;

                 Socket handler = os.worksocket;

                 int Read = handler.EndReceive(iar);

                 DateString = Encoding.ASCII.GetString(os.BufferDate, 0, ObjectSocket.BufferSize);

                 if (DateString.Contains("HEADEND"))
                 {
                     int headlleght = Encoding.ASCII.GetBytes(Search_string(DateString, "HEADSTART", "HEADEND")).Length + Encoding.ASCII.GetBytes("HEADSTART").Length + Encoding.ASCII.GetBytes("HEADEND").Length;
                     DateAnalysis(Search_string(DateString, "HEADSTART", "HEADEND"), headlleght, os);
                 }
                    os.worksocket.BeginReceive(os.BufferDate, 0, ObjectSocket.BufferSize, 0,
              new AsyncCallback(Receive), os);//接收完后回调该方法 等待下一次数据接收
             //}
             //catch (Exception e)
             //{

             //}

         }

以上代码是接收后直接回调该客户端没清空缓存 请问我是应该重新断开连接 还是在这步直接清除缓存就成?如果只要清除缓存 应该如何清除?