0

I am trying to write a client/server type of application using TCP. A client has the client program installed which listens to port (say 11000) at all times. My server sends a quick small string of text to one of the clients (I could be doing a for loop and could be hundreds of clients). Now all of this is working, except here's the thing, if I send commands too quickly, the application crashes, saying 'cannot use disposed object Socket' in this method (I am using this code: http://msdn.microsoft.com/en-us/library/bew39x2a.aspx):

    private static void ConnectCallback(IAsyncResult ar)
    {
        // Retrieve the socket from the state object.
        Socket client = (Socket)ar.AsyncState;

        // Complete the connection.
        client.EndConnect(ar);

        // Signal that the connection has been made.
        connectDone.Set();
    }

Can someone help. Is the port not opened by then? I do call this at the end of every command:

            client.Shutdown(SocketShutdown.Both);
            client.Disconnect(false);
            client.Close();
Rafi W.
  • 154
  • 2
  • 11
  • I guess it's [SO_LINGER related](http://stackoverflow.com/questions/3757289/tcp-option-so-linger-zero-when-its-required). Using TCP for just sending quick strings between every open and close is probably a bit brutal (and expensive in network resources), you may want to just keep the socket open or switch to UDP. – fvu Jul 03 '13 at 15:38
  • I can't keep keep the connection open b/c these strings/data could be sent every other day, so not so often. I can't use UDP b/c our company hates UDP and won't let us use it in anything, plus I can't afford to lose any of the data when it's going through. Is there no way to wait for the port if it's busy? I am not really sure it's a busy issue though. – Rafi W. Jul 03 '13 at 15:43
  • Valid arguments, that's why I said ***may***. Did you read the linked explanation on SO_LINGER? I think that's the explanation for the issue you are experiencing. – fvu Jul 03 '13 at 15:50
  • Yes, I just played with the LingerState, enabling and disabling and setting time to different numbers. It still didn't make a difference. Is the connection cached on the way? Something tells me that the older connection wasn't disposed of and when I open a new one, the old dispose also disposes my new one. Is this possible? – Rafi W. Jul 03 '13 at 16:28
  • Also have a look here: http://stackoverflow.com/questions/2034582/c-sharp-cannot-access-a-disposed-object - it may just be a cosmetic issue that can be suppressed by adding some targeted exception handling. – fvu Jul 03 '13 at 16:30
  • The problem is in the Shutdown (Both), the socket is made available and can be reused by another thread and, when you call close (socket) you, in fact, are closing the Brand new one. I faced a similar problem in a DEC multi processor machine long time ago. Try the code without calling shutdown. – ja_mesa Jul 03 '13 at 17:22
  • I removed the Shutdown (Both), and now it did 3 sends but then it crashes. I made the code Synchronous, rather than Asynchronous and it works fine now. I am not having those issues anymore. Thanks for the tip. – Rafi W. Jul 03 '13 at 19:05

0 Answers0