Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access a disposed object in System.Net.Sockets.TcpClient [closed]

Tags:

c#

sockets

I'm implementing a chat socket program in windows form application.When I send the data by Send buttuon the first time, server can receive data from client, but it can't in the second time. It error "

Cannot access a disposed object. Object name: System.Net.Socket.TcpClient

" How to fix it?

My Client code :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;


namespace ChatClient
{
    public partial class Form1 : Form
    {
        byte[] data = new byte[1024];
        string input;
        int port;
        TcpClient server;
        int recv;
        string stringData;
        public Form1()
        {
            InitializeComponent();
            Form_Load();
        }
        /// <summary>
        /// 
        /// </summary>
        public void Form_Load()
        {
            rtbContent.AppendText("Please Enter the port number of Server:\n");
            //rtbContent.Text = "Please Enter the port number of Server:\n";
        }
        /// <summary>
        /// Function connect to server with port number
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConnect_Click(object sender, EventArgs e)
        {
            input = txtPort.Text;
            port = Int32.Parse(input);
            try
            {
                server = new TcpClient("127.0.0.1", port);
            }
            catch (SocketException)
            {
                rtbContent.AppendText("Unable to connect to server");
                return;
            }
            rtbContent.AppendText("Connected to Server...\n");
            //rtbContent.Text = "Connected to Server...\n";
        }
        /// <summary>
        /// Function send data to server.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {

                    NetworkStream ns = server.GetStream();
                    StateObject state = new StateObject();
                    state.workSocket = server.Client;
                    server.Client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                          new AsyncCallback((new Form1()).OnReceive), state);

                    input = rtbSend.Text;

                    ns.Write(Encoding.ASCII.GetBytes(input), 0, input.Length);
                    //ns.Flush();

                   // data = new byte[1024];
                   // recv = ns.Read(data, 0, data.Length);
                   // stringData = Encoding.ASCII.GetString(data, 0, recv);
                    //Console.WriteLine(stringData);
                    //rtbContent.AppendText(rtbSend.Text);

                    //rtbContent.AppendText(rtbSend.Text);
                    rtbSend.Text = "";
                    //receiveData(sender, e);
                    ns.Close();

                    server.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Send Button", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// Event called when server send data for client.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void receiveData(object sender, EventArgs e)
        //{
        //    NetworkStream ns = server.GetStream();
        //    StateObject state = new StateObject();
        //    server.Client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
        //          new AsyncCallback((new Form1()).OnReceive), state);
        //    data = new byte[1024];
        //    recv = ns.Read(data, 0, data.Length);
        //    stringData = Encoding.ASCII.GetString(data, 0, recv);
        //    rtbContent.AppendText(stringData);

        //}
        /// <summary>
        /// Event called when Form close.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_FormClosed(Object sender, FormClosedEventArgs e)
        {
            rtbContent.Text = "Disconnecting from server...";
            server.Close();
            MessageBox.Show( "FormClosed Event");
        }
        /// <summary>
        /// Asynchronous Callback function which receives data from client
        /// </summary>
        /// <param name="ar"></param>
        public void OnReceive(IAsyncResult ar)
        {
            String content = String.Empty;

            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            StateObject state = (StateObject)ar.AsyncState;
            Socket handler = state.workSocket;
            int bytesRead;

            if (handler.Connected)
            {

                // Read data from the client socket. 
                try
                {
                    bytesRead = handler.EndReceive(ar);
                    if (bytesRead > 0)
                    {
                        // There  might be more data, so store the data received so far.
                        state.sb.Remove(0, state.sb.Length);
                        state.sb.Append(Encoding.ASCII.GetString(
                                         state.buffer, 0, bytesRead));

                        // Display Text in Rich Text Box
                        content = state.sb.ToString();
                        rtbContent.AppendText(content.ToString());

                        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                            new AsyncCallback(OnReceive), state);

                    }
                }

                catch (SocketException socketException)
                {
                    //WSAECONNRESET, the other side closed impolitely
                    if (socketException.ErrorCode == 10054 || ((socketException.ErrorCode != 10004) && (socketException.ErrorCode != 10053)))
                    {
                        handler.Close();
                    }
                }

            // Eat up exception....
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message + "\n" + exception.StackTrace);

                }
            }
        }
    }
    /// <summary>
    /// Server receive state
    /// </summary>
    public class StateObject
    {
        // Client  socket.
        public Socket workSocket = null;
        // Size of receive buffer.
        public const int BufferSize = 1024;
        // Receive buffer.
        public byte[] buffer = new byte[BufferSize];
        // Received data string.
        public StringBuilder sb = new StringBuilder();
    }
}
like image 373
AzVoz Avatar asked Jan 04 '16 17:01

AzVoz


1 Answers

server.Close();

You're closing your connection when you send data on the first btnSend_Click method. If you omit this from your method, it may solve your problem.

ns.Close();

You may need to omit this as well.

like image 151
HDL_CinC_Dragon Avatar answered Oct 13 '22 10:10

HDL_CinC_Dragon