2012-03-28 21 views
2

私はC#でWindowsフォームアプリケーションを扱っています。私は非同期の方法でサーバーに接続しているソケットクライアントを使用しています。 何らかの理由で接続が切断された場合、ソケットを直ちにサーバーに再接続しようとします。 私はNotifyClientStatusSubscribers(偽)は、関数StopClientが実行されると呼ばれるこの非同期ソケットクライアントの自動再接続

 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) 
     { 

     } 
    } 

のようなルーチンルックス受け取る:

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(); 
     } 
    } 

接続が利用可能になるとソケットが再接続しますが、数秒後に次の処理されない例外が発生します。 "すでに接続されているソケットに対して接続要求が行われました。"

これはどのように可能ですか?

答えて

2

例外がConnectCallbackにあり、実際に正常に接続した可能性があります。 ConnectCallbackのcatchステートメントにブレークポイントを設定し、例外が発生したかどうかを確認します。現在、例外が発生したことを通知するものはありません。

関連する問題