1

I am trying to code a very simple (seems like it to me) extension of the MSDN example here: https://msdn.microsoft.com/en-us/library/bew39x2a(v=vs.110).aspx

The client would run in a loop. In the case below, I am reinitializing the client every time.

    while (true)
        {
            Socket client = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);
            client.BeginConnect(remoteEP,
                new AsyncCallback(ConnectCallback), client);
            connectDone.WaitOne();

            string myCommand = "";

            Console.WriteLine("Enter command:");
            myCommand = Console.ReadLine();



            if (myCommand == "quit") break;
            // Send test data to the remote device.
            Send(client, myCommand + "<EOF>");
            sendDone.WaitOne();

            // Receive the response from the remote device.
            Receive(client);
            receiveDone.WaitOne();

            // Write the response to the console.
            Console.WriteLine("Response received : {0}", response);
            // Release the socket.
            client.Shutdown(SocketShutdown.Both);
            client.Close();

        }

It doesn't work consistently, frequently I get the error "Cannot access a disposed object", but can't figure out what is triggering it. I'm a beginner at socket programming and multithreading.

5
  • Try wrapping the code in a Using statement to make sure everything is disposed of properly. Commented Dec 19, 2016 at 0:14
  • looking at the console, when I get the error, the lines are printed out of order: Commented Dec 19, 2016 at 0:30
  • Socket connected to 192.168.56.1:11000 Enter command: test Sent 9 bytes to server. Response received : Unrecognized command<EOF> Enter command: Socket connected to 192.168.56.1:11000 Commented Dec 19, 2016 at 0:31
  • I put everything in the while loop in an using statement, still getting the same error. Commented Dec 19, 2016 at 0:33
  • I know it's fishing a bit, but you may also want to put the code into a separate async Task function and call it each time with a wait. I can't guarantee it will work, but it will prevent any resource collisions. Commented Dec 19, 2016 at 0:40

1 Answer 1

1

seems to work if I reinitialize the Manual Event Handlers. No idea what this stuff does :)

        while (true)
        {
            using (Socket client = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp))
            {
                connectDone = new ManualResetEvent(false);
                sendDone =  new ManualResetEvent(false);
                receiveDone =  new ManualResetEvent(false);

                client.BeginConnect(remoteEP,
                    new AsyncCallback(ConnectCallback), client);
                connectDone.WaitOne();



                string myCommand = "";

                //Thread.Sleep(100);  
                // Create a TCP/IP socket.

                Console.WriteLine("Enter command:");
                myCommand = Console.ReadLine();



                if (myCommand == "quit") break;
                // Send test data to the remote device.
                Send(client, myCommand + "<EOF>");
                sendDone.WaitOne();

                // Receive the response from the remote device.
                Receive(client);
                receiveDone.WaitOne();

                // Write the response to the console.
                Console.WriteLine("Response received : {0}", response);
                // Release the socket.
                client.Shutdown(SocketShutdown.Both);
                client.Close();
            }

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

1 Comment

A ResetEvent can be described as a door. When you try to enter using WaitOne() and the door is closed (when the MRE is not set) the code execution will be blocked at that line, and you'll have to wait until the door is open for execution to continue. If the door is already open the execution just continues.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.