0

Here is a sample server and client code in c#. The server will send an array of string and the client will receive it and display and then the client will send an id and the server will receive it and display. But I am getting an exception in my the server while running both of them.

The exception is as follows:

An unhandled exception of type 'System.ObjectDisposedException' occurred in System.dll

Additional information: Cannot access a disposed object.

Client:

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                byte[] data = new byte[1024];
                string stringData;

                TcpClient tcpClient = new TcpClient("127.0.0.1", 1234);
                NetworkStream ns = tcpClient.GetStream();             

                var serializer = new XmlSerializer(typeof(string[]));
                var stringArr = (string[])serializer.Deserialize(tcpClient.GetStream());

                foreach (string s in stringArr)
                {
                    Console.WriteLine(s);
                }

                string input = Console.ReadLine();
                ns.Write(Encoding.ASCII.GetBytes(input), 0, input.Length);
                ns.Flush();
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
            }

            Console.Read();
        }
    }
}

Server:

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace server
{
    class Program
    {
        static void Main(string[] args)
        {  
            TcpListener tcpListener = new TcpListener(IPAddress.Any, 1234);
            tcpListener.Start();  

            while (true)
            {                     
                TcpClient tcpClient = tcpListener.AcceptTcpClient();
                byte[] data = new byte[1024];

                NetworkStream ns = tcpClient.GetStream();
                string[] arr1 = new string[] { "one", "two", "three" };

                var serializer = new XmlSerializer(typeof(string[]));
                serializer.Serialize(tcpClient.GetStream(), arr1);

                tcpClient.Close();

                int recv = ns.Read(data, 0, data.Length); //getting exception in this line

                string id = Encoding.ASCII.GetString(data, 0, recv);

                Console.WriteLine(id);              
            }               
        }
    }
}

Is there anything wrong?

What will I need to change to avoid this exception?

3
  • 2
    I see it is the 3rd time you post the same question with added details. You may edit your question instead of reposting it. This way, the previous answers wouldn't be lost. Commented Sep 27, 2015 at 19:10
  • 1
    Why are you doing tcpClient.Close() if you want to read data from that stream afterwards? Don't close the connection just yet. Commented Sep 27, 2015 at 19:11
  • If I don't use this then, the string array is not getting displayed @Maximilian Gerhardt Commented Sep 28, 2015 at 6:00

1 Answer 1

1

Once you call

tcpClient.Close();

it cleans up resources associated with it, including disposing ns.

The following line

int recv = ns.Read(data, 0, data.Length); //getting exception in this line

attempts to read from ns after you just (indirectly) disposed of it.

Do not close the connection until you are done with it. Also, use the using keyword instead of explicitly closing the connection, as it will ensure proper cleanup even if an exception is thrown.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.