public void ReceMsgCallBack(IAsyncResult ar)
        {
            ClientState cs = ar.AsyncState as ClientState;
            Socket thisSk = cs.sk;            try
            {
                byte[] fnl = cs.state;
                MemoryStream ms = new MemoryStream();
                BinaryFormatter bf = new BinaryFormatter();
                ms.Write(fnl, 0, fnl.Length);
                ms.Position = 0;
                ArrayList aal = (ArrayList)bf.Deserialize(ms);
                int length = Convert.ToInt32(aal[0]);
                string ffname = aal[1] as string;
                byte[] filelag = new byte[length];
                int bytesRead;
                int totalBytes = 0;
                fs = new FileStream(@"C:\Users\Administrator\" + ffname, FileMode.OpenOrCreate, FileAccess.Write);
                do
                {
                    bytesRead = sk.Receive(filelag);
                    fs.Write(filelag, 0, bytesRead);
                    totalBytes += bytesRead;                }
                while (totalBytes<filelag.Length);
                fs.Close();
                ClientState thiscs = new ClientState();
                thiscs.sk = thisSk;
                thisSk.BeginReceive(thiscs.state, 0, thiscs.state.Length, SocketFlags.None, new AsyncCallback(ReceMsgCallBack), thiscs);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           
        }
这是我的代码 我在客户端发送一个图片文件 将其转换为二进制后发送 然后在服务端接受使用的就是上面这段
但是在发送完一次还可以 继续发送就报错了 不知道是什么原因 还是我代码写错了 求高手赐教

解决方案 »

  1.   

    代码错了呀。ReceMsgCallBackBeginReceive的Callback方法ReceMsgCallBack里面应该用EndReceive来处理的,用Receive变成同步的,是不是搞混了?参考一下MSDN里面的代码:
    public static void Listen_Callback(IAsyncResult ar){
           allDone.Set();
           Socket s = (Socket) ar.AsyncState;
           Socket s2 = s.EndAccept(ar);
           StateObject so2 = new StateObject();
           so2.workSocket = s2;
           s2.BeginReceive(so2.buffer, 0, StateObject.BUFFER_SIZE,0,
                                 new AsyncCallback(Async_Send_Receive.Read_Callback), so2);    
      }  public static void Read_Callback(IAsyncResult ar){
          StateObject so = (StateObject) ar.AsyncState;
          Socket s = so.workSocket;
      
          int read = s.EndReceive(ar);
      
          if (read > 0) {
                  so.sb.Append(Encoding.ASCII.GetString(so.buffer, 0, read));
                  s.BeginReceive(so.buffer, 0, StateObject.BUFFER_SIZE, 0, 
                                           new AsyncCallback(Async_Send_Receive.Read_Callback), so);
          }
          else{
               if (so.sb.Length > 1) {
                    //All of the data has been read, so displays it to the console
      
                    string strContent;
                    strContent = so.sb.ToString();
                    Console.WriteLine(String.Format("Read {0} byte from socket" + 
                                       "data = {1} ", strContent.Length, strContent));
               }
               s.Close();
          }
      }