簡體   English   中英

在處理完成之前已達到流結束

[英]The stream-end was reached before the processing is completed

當我嘗試將某些內容加載到MemoryStream時出現此錯誤。 我嘗試通過服務器通過網絡發送圖片(按字節流)。 但是每次我嘗試發送時,都會說

在處理完成之前已達到流結束。

帶有錯誤的行在下面的代碼中標記

namespace Model_Library
{
    [Serializable]
    public class Package
    {
        public List<object> DATA;
        public int ID;
        public PackageType packetType;

        public Package(PackageType u_type, int u_ID)
        {
            DATA = new List<object>();
            this.ID = u_ID;
            this.packetType = u_type;
        }

        public Package(byte[] packetBytes)
        {
            using (MemoryStream ms = new MemoryStream(packetBytes))
            {
                BinaryFormatter bf = new BinaryFormatter();
                Package p = (Package)bf.Deserialize(ms); //Here is the error
                ID = p.ID;
                DATA = p.DATA;
                packetType = p.packetType;
            }
        }

        public byte[] toBytes()
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, this);
            byte[] bytes = ms.ToArray();
            ms.Close();
            return bytes;
        }
    }

    public enum PackageType
    {
        connect,  
        login,
        registration, 
        friends, 
        message,
        message_confirmation, 
        load_history, 
        search_friends,
        add_friend 
    }
}

確保所有數據均已通過網絡接收,此處未顯示,但您使用的是NetworkStream.DataAvailable,請閱讀此答案,因為其中包含有關如何管理從網絡讀取數據的一些詳細信息-TcpClient.GetStream()。DataAvailable返回false,但流中包含更多數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM