ObjectDisposeException when trying to send thru a reopened socket

后端 未结 1 400
长情又很酷
长情又很酷 2021-01-27 18:08
  1. I\'m using Socket (Socket A = new Socket...) to send/receive.
  2. when something bed happens (disconnection), I\'m trying to close/dispose old object, and then instan
相关标签:
1条回答
  • 2021-01-27 18:17

    Have your Connect method create a new socket and return that socket to send data. Something more like:

    try
    {
       CCMSocket = new Socket();
       CCMSocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
       CCMSocket.Connect(CCMServer, CCMPort);
       return CCMSocket
    }
    

    and

    do
    {
        reconnectCounter++;
        Disconnect(); //<-- Just CCMSocket.Disconnect(true) in a try/catch
        var newSocket = Connect(CCMServer, CCMPort); // <-- method given above
        if (newSocket != null) 
        {
            connected = true;
            newSocket.Send(LoginData); // should work
            CCMSocket = newSocket; // To make sure existing references work
        }
    } while (!connected);
    

    You should also seriously consider the asynchronous socket pattern when building server applications.

    0 讨论(0)
提交回复
热议问题