请教各位高手,我用TcpClient连接断开重新连接后BinaryReader对象会报出无法从流的末尾读取数据。
或者
传输连接中读取数据: 您的主机中的软件放弃了一个已建立的连接。。
   在 System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 s
ize)
   在 System.IO.BufferedStream.ReadByte()
   在 System.IO.BinaryReader.ReadByte()
不允许对非连接的套接字执行此操作。binaryreader不支持查找功能,所以不能通过流的长度和是否返回-1来判断是否是末尾。
还有当socket断开重新连接以后是否需要把流重新new一下呢??

解决方案 »

  1.   

    private void ReceiveCallBack(IAsyncResult ar)
            {
                try
                {
                    lock (this)
                    {
                        int recvCount = _client.GetStream().EndRead(ar);                    if (recvCount < 1)
                        {
                            //断开连接事件
                        }                    byte[] recvbytes = new byte[recvCount];
                        Array.Copy(RecvBuffer, 0, recvbytes, 0, recvCount);
                        OnRecvMsg(this, recvbytes);
                    }                lock (this)
                    {
                        //继续接收
                        _client.GetStream().BeginRead(RecvBuffer, 0, 2048, new AsyncCallback(ReceiveCallBack), null);
                    }
                }
                catch (SocketException ex)
                {
                    Util.LogDebug("收消息时候出错" + ex.ToString());
                    OnSocketError(this, new EventArgs());
                }
                catch (Exception ex)
                {
                    Util.LogDebug(ex.ToString());
                }
            }
      

  2.   

    在断开连接时
    try
    {
    sock.Shutdown(SocketShutdown.Both);
    sock.Close ();
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    sock = null;
      

  3.   

    服务器端
    int byteRecv = 0;
    try
    {
    Byte[]  bbb=new Byte[64];
    byteRecv = temp.Receive(bbb,bbb.Length,0);
    if(byteRecv < 1)
    {
    temp.Close();
    temp = null;
    listenshow.Text = "与客户断开连接!从新监听!";
    this.send1.Enabled = false;
                                            this.showimage.Text = "";
    break;
    }
      

  4.   

    to Red_angelX(八戒)
    你在lock (this)
                    {
                        //继续接收
                        _client.GetStream().BeginRead(RecvBuffer, 0, 2048, new AsyncCallback(ReceiveCallBack), null);
                    }
    这里为什么要使用同步的?又为什么在开始读取的时候用异步处理呢?
      

  5.   

    to Red_angelX(八戒)
    我使用的是binaryreader来接收服务端的数据,所以必须写
           InputStreamConvert reader = null;
                    BinaryReader br = null;
                    while (m_client != null && m_client.bRunning)
                    {
                        if (null == m_client.m_tcpClient.Client)
                        {                        reader = null;
                            br = null;
                            try
                            {
                                Thread.Sleep(500);
                            }
                            catch (ArgumentOutOfRangeException e)
                            {
                                Console.WriteLine(e.StackTrace);
                                Console.WriteLine(e.Message);
                            } // end try                        continue;
                        } // end if
                        try
                        {
                            if (reader == null || br == null)
                            {
                                reader = new InputStreamConvert();                            br = new BinaryReader(new BufferedStream(m_client.m_tcpClient.GetStream()));                        } // end if                        byte cmd = reader.ReadJavaByte(br);
    switch(cmd)
    {
    case 0:...................break;
    }
    每次运行到byte cmd = reader.ReadJavaByte(br);的时候有时候会报错,有时候正常,不知道是不是线程有问题,而且我代码里都没有经过同步处理,不知道是不是这个原因