objectdisposedexception是未处理的问题

时间:2014-04-21 13:55:37

标签: c# sockets

我有这段代码:

private void DownLoaderSession()
{
    try
    {          
        stream = client.GetStream();

        // Buffer for reading data
        byte[] bytes = new byte[256];
        string data = null;
        bool runFlag = true;
        // Enter the listening loop. 
        while (runFlag)
        {
            int recLen = stream.Read(bytes, 0, bytes.Length);
            // Translate data bytes to a ASCII string.
            data = System.Text.Encoding.ASCII.GetString(bytes, 0, recLen);
            // Process the data sent by the client.
            data = data.ToUpper();
            string[] param = data.Split('#');
            switch (param[0])  // CPMMAND
            {
            case "GETFILE":
                FileInfo ftemp = new FileInfo(ClientForm.SharedFolderPath + "\\" + param[1] );   // file name
                fileSize = ftemp.Length;              // size of file in long
                Send("FILEREADY#" + param[1] + "#" + fileSize.ToString());
                UpLoad(param[1]);
                break;
            case "GETALLDETAILS":
                string temp = FindAllDetails(param[1]);
                Send("DETAILSREADY#"+temp);
                this.client.Close();
                break;
            }
        }
    }
}

我将数据从一个类发送到另一个类:

int recLen = stream.Read(bytes, 0, bytes.Length);

我收到错误:

  

objectdisposedexception未处理

我在互联网上搜索,但找不到答案。

2 个答案:

答案 0 :(得分:0)

此声明:

 case "GETALLDETAILS":
            string temp = FindAllDetails(param[1]);
            Send("DETAILSREADY#"+temp);
            this.client.Close();
            break;

关闭客户端(假设它是TcpClient),流将被处理掉。

break;不会突破while语句,只会突破switch。

答案 1 :(得分:0)

您应该在此行之后将runFlag设置为false

this.client.Close();