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

C#socket接受二进制文件 如何知道已经全部接受完成了呢

 class ClientState 
    {
        public byte[] state = new byte[1024];
        public Socket sk;
    }
    class FileState
    {
      public  FileStream fs;
       public string filename;
       public const int length = 1024;
       public byte[] state = new byte[length];
      public Socket sk;
    }
 private void AsyCallback(IAsyncResult ar)
        {
            //获取侦听器
            TcpListener tl = ar.AsyncState as TcpListener;
            //获取客户端的Socket
            sk = tl.EndAcceptSocket(ar);
           
            ClientState cs = new ClientState();
            cs.sk = sk;
            
            sk.BeginReceive(cs.state, 0, cs.state.Length, SocketFlags.None, new AsyncCallback(Read_Callback), cs);
            //异步接受数据
            tl.BeginAcceptSocket(new AsyncCallback(AsyCallback), tl);

        }

    public  void Read_Callback(IAsyncResult ar)
        {
                ClientState cs = (ClientState)ar.AsyncState;
                Socket s = cs.sk;

                int read = s.EndReceive(ar);
                byte[] fnl = cs.state;