Hi all,

I am trying to make a simple TCP server application, where the client should send some numbers, and the server will show it in a text box. So far this is what I come with, but it show an error of cannot accessing a disposed object. Would appreciate if anyone can help me in solving this problem

So for the client

public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            //inizialitation of the thread
            Thread newThread = new Thread(new ThreadStart(Client));
            newThread.Start();

        }


        //function of the client
        public static void Client()
        {
            int i;
            int f;
            byte[] data = new byte[1024];;
            string input, stringData;
            TcpClient server;
            try
            {
                //TcpClient listen to localhost in port 8000
                server = new TcpClient("127.0.0.1", 8000);
            }
            catch (SocketException)
            {
                return;
            }

            //get stream of the connection
            NetworkStream ns = server.GetStream();
            
            //block for sending number 1 -3 to server
            while (true)
            {
                data = new byte[1024];
                for (i = 1; i <= 3; i++)
                {
                    data = Encoding.ASCII.GetBytes(Convert.ToString(i));
                    ns.Write(data, 0, data.Length);
                    //error of Cannot access a disposed object occured in here


                    //sleep using fake loop here                  
                    for (f = 0; f <= 50000000; f++)
                    {
                    }

                }                                             
            ns.Close();
            server.Close();
        }

        
    }
    }
}

and for the server, I am also confused, how to make the server show the numbers in textbox? below is the server codes that I tried

public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            //start the thread between server and form
            Thread newThread = new Thread(new ThreadStart(server));
            newThread.Start();
        }

        //method that works as server
        public static void server()
        {
            int recv;
            byte[] data = new byte[1024];
            TcpListener newsock = new TcpListener(8000);
            newsock.Start();
            TcpClient client = newsock.AcceptTcpClient();
            NetworkStream ns = client.GetStream();
            
            while (true)
            {
                data = new byte[1024];
                recv = ns.Read(data, 0, data.Length);
                if (recv == 0)
                    break;
/*how can I get the data from client and put a function to take the value to be shown in a text box here?*/
            }

            ns.Close();
            client.Close();
            newsock.Stop();
        }
    }
}

yes thank you for the link. I have founded many link different applications of chat, but I can not parse the information that I need (basically, a very simple one).
So I think maybe anyone who have done better programming can discuss about what I have done, and give me any improvement.

Best.

This article has been dead for over six months. Start a new discussion instead.