自动重新连接异步套接字客户端

时间:2012-03-28 17:01:10

标签: c# sockets asynchronous asyncsocket winsockets

我正在使用C#中的Windows窗体应用程序。我正在使用一个套接字客户端,它以异步方式连接到服务器。 如果连接因任何原因而中断,我希望套接字尝试立即重新连接到服务器。 我的接收程序看起来像这样

        public void StartReceiving()
    {
        StateObject state = new StateObject();
        state.workSocket = this.socketClient;
        socketClient.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(OnDataReceived), state);
    }

    private void OnDataReceived(IAsyncResult ar)
    {
        try
        {
            StateObject state = (StateObject)ar.AsyncState;
            Socket client = state.workSocket;

            // Read data from the remote device.
            int iReadBytes = client.EndReceive(ar);
            if (iReadBytes > 0)
            {
                byte[] bytesReceived = new byte[iReadBytes];
                Buffer.BlockCopy(state.buffer, 0, bytesReceived, 0, iReadBytes);
                this.responseList.Enqueue(bytesReceived);
                StartReceiving();
                receiveDone.Set();
            }
            else
            {
                NotifyClientStatusSubscribers(false);
            }
        }
        catch (Exception e)
        {

        }
    }

当调用NotifyClientStatusSubscribers(false)时,执行函数StopClient:

public void StopClient()
    {
        this.canRun = false;
        this.socketClient.Shutdown(SocketShutdown.Both);
        socketClient.BeginDisconnect(true, new AsyncCallback(DisconnectCallback), this.socketClient);
    }

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

            // Complete the disconnection.
            client.EndDisconnect(ar);

            this.socketClient.Close();
            this.socketClient = null;
        }
        catch (Exception e)
        {

        }
    }

现在我尝试通过调用以下函数重新连接:

    public void StartClient()
    {
        this.canRun = true;
        this.MessageProcessingThread = new Thread(this.MessageProcessingThreadStart);
        this.MessageProcessingThread.Start();
        this.socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        this.socketClient.LingerState.Enabled = false;
    }

    public void StartConnecting()
    {
        socketClient.BeginConnect(this.remoteEP, new AsyncCallback(ConnectCallback), this.socketClient);
    }

    private void ConnectCallback(IAsyncResult ar)
    {
        try
        {
            // 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();

            StartReceiving();

            NotifyClientStatusSubscribers(true);
        }
        catch(Exception e)
        {
            StartConnecting();
        }
    }

当连接可用时套接字重新连接,但几秒钟后我得到以下未处理的异常: “已在已连接的套接字上发出连接请求。”

这怎么可能?

1 个答案:

答案 0 :(得分:2)

如果您在ConnectCallback中获得例外并且您已成功连接,则可能会发生这种情况。在ConnectCallback的catch语句中设置一个断点,看看是否有异常在那里引发 - 目前没有什么可以告诉你有异常。