socket beginreceive中断流接收

时间:2015-05-19 11:44:23

标签: c# sockets serialization stream

我编写了一个程序,通过网络从计算机传递屏幕信息。 它是在流上工作的,我每隔几秒就会序列化到一个网络流来发送屏幕截图。 这是代码:

 NetworkStream stream;
 byte[] inBytes;
 BinaryFormatter bFormat;
 Bitmap inImage;
 int count = 0;

  public void startListening()
  {
       bFormat = new BinaryFormatter();
       stream = client.GetStream();

       while (true)
       {
            try
            {
                inBytes = bFormat.Deserialize(stream) as byte[];
                inImage = ByteToImage(inBytes);

                theImage.Image = (System.Drawing.Image)inImage;
                count++;
            }
            catch { }
        }
    }

它的工作非常好,但是当我尝试时,我遇到了一个问题 在连接时使用tcp客户端上的beginreceive

      StateObject state = new StateObject();
      state.workSocket = tcpClient.Client;
      tcpClient.Client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0
                   ,new AsyncCallback(OnReceive), state);

我将onreceive方法留空了以确保问题不在方法本身的代码中

  public void OnReceive(IAsyncResult ar)
  {} 

接收方法工作正常(尝试使用消息框),但它会影响流的序列化,并且我得到此错误system.runtime.serialization.serializationexception no map for object 任何想法的家伙?我试图使用不同的线程..但是没有真正起作用

流来自另一个程序..发送它的客户端我没有在这里发布它的代码,因为我不认为问题来自那里......它是服务器中的东西:(

这是在谁的问题

的beginreceive中的代码
 public void OnReceive(IAsyncResult ar)
    {

        String content = String.Empty;

        StateObject state = (StateObject)ar.AsyncState;
        Socket handler = state.workSocket;
        int bytesRead;

        if (handler.Connected)
        {

            // Read data from the client socket. 
            try
            {
                bytesRead = handler.EndReceive(ar);
                if (bytesRead > 0)
                {


                    // There  might be more data, so store the data received so far.
                    state.sb.Remove(0, state.sb.Length);
                    state.sb.Append(Encoding.ASCII.GetString(
                                     state.buffer, 0, bytesRead));




                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                       new AsyncCallback(OnReceive), state);

                }
                else
                {
                    String remoteIP = ((IPEndPoint)handler.RemoteEndPoint).Address.ToString();
                    String remotePort = ((IPEndPoint)handler.RemoteEndPoint).Port.ToString();
                    DisconnectClient(remoteIP, remotePort);
                }



            }

            catch (SocketException socketException)
            {

                //WSAECONNRESET, the other side closed impolitely
                if (socketException.ErrorCode == 10054 || ((socketException.ErrorCode != 10004) && (socketException.ErrorCode != 10053)))
                {

                    // Complete the disconnect request.
                    String remoteIP = ((IPEndPoint)handler.RemoteEndPoint).Address.ToString();
                    String remotePort = ((IPEndPoint)handler.RemoteEndPoint).Port.ToString();
                    DisconnectClient(remoteIP, remotePort);

                    handler.Close();
                    handler = null;
                    MessageBox.Show("disconnected");

                }
            }


            catch (Exception exception)
            {
                MessageBox.Show(exception.Message + "\n" + exception.StackTrace);

            }
        }

    }

0 个答案:

没有答案