c#SslStream.EndRead返回零字节

时间:2016-07-22 01:20:52

标签: c# sockets networking sslstream

我的服务器程序,在许多产品环境中运行良好,在我们没有运行的机器上购买。

在这台机器上,当我们调用SslStream.BeginRead时,那么当SslStream.EndRead调用0返回时,如果我们不关闭流,并再次调用SslStream,则会抛出异常。

private void Read()
    {
        try
        {
            if (this._IsClosed)
            {
                return;
            }
            this._Stream.BeginRead(this._Buffer, 0,     BufferManager.Default.OutterReadBufferSize, this.EndRead, null);
        }
        catch (Exception ex)
        {
            _Logger.WarnFormat("Begin Read, session:{0}, {1}", this._Id, ex);
            this.Close();
        }
    }





private void EndReadTaskAction(object ar)
    {
        try
        {
            int len = this._Stream.EndRead((IAsyncResult)ar);
            int used = 0;
            if (len <= 0)
            {
                _Logger.WarnFormat("EndReadTaskAction len less or equal zero, len={0}, session={1}", len, _Id);
                this.Close();
                return;
            }

            if (this._HasPartialPacket)
            {
                if (this._PartialReadedLenth < PacketConstants.HeadLength)
                {
                    int needToRead = PacketConstants.HeadLength - this._PartialReadedLenth;
                    if (needToRead > len)
                    {
                        Buffer.BlockCopy(this._Buffer, used, this._Buffer, GetCurrentPartialReadIndex(), len);
                        this._PartialReadedLenth += len;
                        Read();
                        return;
                    }
                    Buffer.BlockCopy(this._Buffer, used, this._Buffer, GetCurrentPartialReadIndex(), needToRead);
                    len -= needToRead;
                    used += needToRead;
                    this._PartialReadedLenth += needToRead;
                }
                int packetLength = PacketHelper.GetPacketLength(this._Buffer, this._PartialReadIndex);
                int howMuchNeedToRead = packetLength - this._PartialReadedLenth;
                if (howMuchNeedToRead > len)
                {
                    Buffer.BlockCopy(this._Buffer, used, this._Buffer, GetCurrentPartialReadIndex(), len);
                    this._PartialReadedLenth += len;
                    Read();
                    return;
                }
                Buffer.BlockCopy(this._Buffer, used, this._Buffer, GetCurrentPartialReadIndex(), howMuchNeedToRead);
                ProcessPackage(this._Buffer, this._PartialReadIndex, packetLength);
                len -= howMuchNeedToRead;
                used += howMuchNeedToRead;
            }
            this._HasPartialPacket = false;
            this._PartialReadedLenth = 0;
            while (true)
            {
                if (len <= 0)
                {
                    break;
                }

                if (len < PacketConstants.HeadLength)
                {
                    Buffer.BlockCopy(this._Buffer, used, this._Buffer, this._PartialReadIndex, len);
                    this._PartialReadedLenth = len;
                    this._HasPartialPacket = true;
                    break;
                }
                int packetLength = PacketHelper.GetPacketLength(this._Buffer, used);
                if (len < packetLength)
                {
                    Buffer.BlockCopy(this._Buffer, used, this._Buffer, this._PartialReadIndex, len);
                    this._PartialReadedLenth = len;
                    this._HasPartialPacket = true;
                    break;
                }
                ProcessPackage(this._Buffer, used, packetLength);
                used += packetLength;
                len -= packetLength;
            }
            if (_isBeginRead)
            {
                Read();
            }
        }
        catch (Exception ex)
        {
            _Logger.WarnFormat("End read, session:{0}, {1}", this._Id, ex);
            this.Close();
        }
    }

1 个答案:

答案 0 :(得分:0)

你在部署的服务器上说你没有任何问题,在新机器上你遇到了问题,请检查所有dll是否包括及其依赖项,.net框架运行时。

希望有所帮助......