Skip to content

SocketException: OS Error: Connection reset by peer, errno = 104 #44164

@103style

Description

@103style

when the sever and client two-way check, i got the error info:

SocketException: OS Error: Connection reset by peer, errno = 104, 

and i use the mqtt library is mqtt_client


flutter doctor -v:

[√] Flutter (Channel stable, v1.9.1+hotfix.5, on Microsoft Windows [Version 10.0.17134.1006], locale zh-CN)
    • Flutter version 1.9.1+hotfix.5 at D:\flutter
    • Framework revision 1aedbb1835 (2 weeks ago), 2019-10-17 08:37:27 -0700
    • Engine revision b863200c37
    • Dart version 2.5.0
[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at D:\Android\sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 28.0.3
    • Java binary at: D:\Android\AndroidStudio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
    • All Android licenses accepted.
[√] Android Studio (version 3.5)
    • Android Studio at D:\Android\AndroidStudio
    • Flutter plugin version 40.2.2
    • Dart plugin version 191.8593
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)

my code: init()connect()

  /// config mqtt secure
  Future<int> init({bool deleteExist: true}) async {
    /// 安全认证
    _client.secure = true;
    final SecurityContext context = SecurityContext.defaultContext;

    String caPath =
        await _getLocalFile("ca.pem", cert_ca, deleteExist: deleteExist);
    String clientKeyPath = await _getLocalFile("clientkey.pem", cert_client_key,
        deleteExist: deleteExist);
    String clientCrtPath = await _getLocalFile("client.pem", cert_client_crt,
        deleteExist: deleteExist);
    try {
      context.setAlpnProtocols(['TLSv1.0', 'TLSv1.1', 'TLSv1.2'], false);
      context.setTrustedCertificates(caPath);
      context.setClientAuthorities(clientCrtPath);
      context.usePrivateKey(clientKeyPath);
    } on Exception catch (e) {
      //出现异常 尝试删除本地证书然后重新写入证书
      log("SecurityContext set  error : " + e.toString());
      return -1;
    }
    _client.securityContext = context;
    _client.setProtocolV311();

    return 0;
  }

  ///  connect
  Future<int> connect(String IMEI) async {
    _client.clientIdentifier = IMEI;
    try {
      await _client.connect();
    } on Exception catch (e) {
      log('MqttUtils::client exception - $e');
      _client.disconnect();
    }
    return 0;
  }


  /// get local file path
  Future<String> _getLocalFile(String filename, String certContent,
      {bool deleteExist: false}) async {
    String dir = (await getApplicationDocumentsDirectory()).path;
    log('dir = $dir');
    File file = new File('$dir/$filename');
    bool exist = await file.exists();
    log('exist = $exist');
    if (deleteExist) {
      if (exist) {
        file.deleteSync();
      }
      exist = false;
    }
    if (!exist) {
      log("MqttUtils: start write cert in local");
      await file.writeAsString(certContent);
    }
    return file.path;
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions