getting exception from server in socket while displaying a string array in client

SK33

New member
Joined
Sep 26, 2015
Messages
4
Programming Experience
Beginner
I have created a simple server and client program in c#. The server will send a string array to the client and the client will display it and the client will sent an id to server and the server will display it. My sample code is given below. When I run them, I get an exception in the server side. I have marked the line in which I am getting the exception.My sample code is given below:

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);

            }               
        }
    }
}

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();
    }
}
}


The exception I am getting is:


An unhandled exception of type 'System.ObjectDisposedException' occurred in System.dll
Additional information: Cannot access a disposed object.
[/XCODE]
 
Last edited by a moderator:
ObjectDisposedException is because you first close the TcpClient and then try writing to it.

Your current protocol won't work, the xml deserializer will wait for end of stream before returning, which will only happen when NetworkStream is closed and will prevent your further communications. An option is to serialize to a byte array and write that to the stream, at receiver read the bytes and deserialize them. A serializer that will work like that without need to close stream is the BinaryFormatter.

Please read FAQ on how to post code using XCODE tag: BB Code List - C# Developer Forums
 

Latest posts

Back
Top Bottom