shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
552 stars 179 forks source link

SocketException: Failed host lookup: 'ssl://iotcloud-mqtt.gz.tencentdevices.com:8883' (OS Error: No address associated with hostname, errno = 7) #107

Closed Tumbler0809 closed 5 years ago

Tumbler0809 commented 5 years ago

I/flutter ( 5736): MQTT client connecting.... I/flutter ( 5736): 2019-08-14 20:03:47.420092 -- SynchronousMqttConnectionHandler::internalConnect entered I/flutter ( 5736): 2019-08-14 20:03:47.425662 -- SynchronousMqttConnectionHandler::internalConnect - initiating connection try 0 I/flutter ( 5736): 2019-08-14 20:03:47.426039 -- SynchronousMqttConnectionHandler::internalConnect - insecure TCP selected I/flutter ( 5736): 2019-08-14 20:03:47.556682 -- MqttConnection::_onError - calling disconnected callback I/flutter ( 5736): SocketException: Failed host lookup: 'ssl://iotcloud-mqtt.gz.tencentdevices.com:8883' (OS Error: No address associated with hostname, errno = 7) I/flutter ( 5736): 2019-08-14 20:03:47.566077 -- SynchronousMqttConnectionHandler::disconnect I/flutter ( 5736): 2019-08-14 20:03:47.570387 -- MqttConnectionHandler::sendMessage - MQTTMessage of type MqttMessageType.disconnect I/flutter ( 5736): Header: MessageType = MqttMessageType.disconnect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0 I/flutter ( 5736): 2019-08-14 20:03:47.570632 -- MqttConnectionHandler::sendMessage - not connected E/flutter ( 5736): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method 'cancel' was called on null. E/flutter ( 5736): Receiver: null E/flutter ( 5736): Tried calling: cancel() E/flutter ( 5736): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5) E/flutter ( 5736): #1 _MyAppState._onDisconnected. (package:mqtt_client_example/main.dart:364:20) E/flutter ( 5736): #2 State.setState (package:flutter/src/widgets/framework.dart:1141:30) E/flutter ( 5736): #3 _MyAppState._onDisconnected (package:mqtt_client_example/main.dart:360:5) E/flutter ( 5736): #4 MqttClient._disconnect (package:mqtt_client/src/mqtt_client.dart:323:7) E/flutter ( 5736): #5 MqttClient.disconnect (package:mqtt_client/src/mqtt_client.dart:287:5) E/flutter ( 5736): #6 _MyAppState._disconnect (package:mqtt_client_example/main.dart:355:12) E/flutter ( 5736): #7 _MyAppState._connect (package:mqtt_client_example/main.dart:334:7) E/flutter ( 5736): E/flutter ( 5736): #8 _MyAppState._buildBrokerPage. (package:mqtt_client_example/main.dart:151:15) E/flutter ( 5736): #9 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:635:14) E/flutter ( 5736): #10 _InkResponseState.build. (package:flutter/src/material/ink_well.dart:710:32) E/flutter ( 5736): #11 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24) E/flutter ( 5736): #12 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11) E/flutter ( 5736): #13 TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:312:7) E/flutter ( 5736): #14 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27) E/flutter ( 5736): #15 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:222:20) E/flutter ( 5736): #16 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22) E/flutter ( 5736): #17 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7) E/flutter ( 5736): #18 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7) E/flutter ( 5736): #19 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7) E/flutter ( 5736): #20 _rootRunUnary (dart:async/zone.dart:1136:13) E/flutter ( 5736): #21 _CustomZone.runUnary (dart:async/zone.dart:1029:19) E/flutter ( 5736): #22 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7) E/flutter ( 5736): #23 _invoke1 (dart:ui/hooks.dart:252:10) E/flutter ( 5736): #24 _dispatchPointerDataPacket (dart:ui/hooks.dart:161:5) E/flutter ( 5736):

Tumbler0809 commented 5 years ago

This MQTT service is normal(ssl://iotcloud-mqtt.gz.tencentdevices.com:8883), I have tested with native Android code to connect, subscribe to topics and post messages.

shamblett commented 5 years ago

OK, remove the 'ssl//' and the port spec from the url, leaving 'iotcloud-mqtt.gz.tencentdevices.com' as the host. you are using port 8883 so set the secure flag on the client before connecting.

Tumbler0809 commented 5 years ago

How to set up a server certificate

shamblett commented 5 years ago

Have a look at the iot_core.dart file in the examples folder, this shows how to set up the secure context with your certificate details etc. Also refer to the mqtt_client API.

Tumbler0809 commented 5 years ago

Key authentication, username and password but always fail to handshake, can be successfully connected using MQTT.FX image

I/flutter ( 6114): MQTT client connecting.... I/flutter ( 6114): 2019-08-15 17:29:21.101655 -- SynchronousMqttConnectionHandler::internalConnect entered I/flutter ( 6114): 2019-08-15 17:29:21.108444 -- SynchronousMqttConnectionHandler::internalConnect - initiating connection try 0 I/flutter ( 6114): 2019-08-15 17:29:21.108906 -- SynchronousMqttConnectionHandler::internalConnect - secure selected I/flutter ( 6114): 2019-08-15 17:29:21.112219 -- MqttSecureConnection::connect I/flutter ( 6114): 2019-08-15 17:32:01.393311 -- MqttConnection::_onError - calling disconnected callback I/flutter ( 6114): HandshakeException: Connection terminated during handshake I/flutter ( 6114): 2019-08-15 17:32:01.403611 -- SynchronousMqttConnectionHandler::disconnect I/flutter ( 6114): 2019-08-15 17:32:01.408521 -- MqttConnectionHandler::sendMessage - MQTTMessage of type MqttMessageType.disconnect I/flutter ( 6114): Header: MessageType = MqttMessageType.disconnect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0 I/flutter ( 6114): 2019-08-15 17:32:01.408736 -- MqttConnectionHandler::sendMessage - not connected E/flutter ( 6114): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method 'cancel' was called on null. E/flutter ( 6114): Receiver: null E/flutter ( 6114): Tried calling: cancel() E/flutter ( 6114): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5) E/flutter ( 6114): #1 _MyAppState._onDisconnected. (package:mqtt_client_example/main.dart:404:20) E/flutter ( 6114): #2 State.setState (package:flutter/src/widgets/framework.dart:1141:30) E/flutter ( 6114): #3 _MyAppState._onDisconnected (package:mqtt_client_example/main.dart:400:5) E/flutter ( 6114): #4 MqttClient._disconnect (package:mqtt_client/src/mqtt_client.dart:323:7) E/flutter ( 6114): #5 MqttClient.disconnect (package:mqtt_client/src/mqtt_client.dart:287:5) E/flutter ( 6114): #6 _MyAppState._disconnect (package:mqtt_client_example/main.dart:395:12) E/flutter ( 6114): #7 _MyAppState._connect (package:mqtt_client_example/main.dart:374:7) E/flutter ( 6114): E/flutter ( 6114): #8 _MyAppState._buildBrokerPage. (package:mqtt_client_example/main.dart:153:15) E/flutter ( 6114): #9 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:635:14) E/flutter ( 6114): #10 _InkResponseState.build. (package:flutter/src/material/ink_well.dart:710:32) E/flutter ( 6114): #11 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24) E/flutter ( 6114): #12 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11) E/flutter ( 6114): #13 TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:312:7) E/flutter ( 6114): #14 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27) E/flutter ( 6114): #15 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:222:20) E/flutter ( 6114): #16 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22) E/flutter ( 6114): #17 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7) E/flutter ( 6114): #18 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7) E/flutter ( 6114): #19 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7) E/flutter ( 6114): #20 _rootRunUnary (dart:async/zone.dart:1136:13) E/flutter ( 6114): #21 _CustomZone.runUnary (dart:async/zone.dart:1029:19) E/flutter ( 6114): #22 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7) E/flutter ( 6114): #23 _invoke1 (dart:ui/hooks.dart:252:10) E/flutter ( 6114): #24 _dispatchPointerDataPacket (dart:ui/hooks.dart:161:5) E/flutter ( 6114):

shamblett commented 5 years ago

Your using port 1883, this is not secure so dont set 'client.secure = true;'

Tumbler0809 commented 5 years ago

Thank you, I have solved it.

shamblett commented 5 years ago

OK, I'll leave this issue open for a while in case you find anything else.

103style commented 5 years ago

[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: FileSystemException: Cannot open file, path = '//crt/crt/client.crt' (OS Error: No such file or directory, errno = 2)

code:

    final String currDir =
        '${path.current}${path.separator}crt${path.separator}';
    final SecurityContext context = SecurityContext.defaultContext;
    context.setTrustedCertificates(currDir + path.join('crt', 'client.crt'));
    client.securityContext = context;
    client.setProtocolV311();

filedir:

project
-crt
--cer
---client.crt

@shamblett

shamblett commented 5 years ago

The path you are assembling is //crt/crt/client.crt, however your directory structure is crt/cer/client.crt, sou you should change

context.setTrustedCertificates(currDir + path.join('crt', 'client.crt'));

to

context.setTrustedCertificates(currDir + path.join('cer', 'client.crt'));

I guess.

103style commented 5 years ago

it's my fault, the project filedir:

project
-crt
--crt
---client.crt

and when i use context.setTrustedCertificates(currDir + path.join('crt', 'client.crt'));

[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: FileSystemException: Cannot open file, path = '//crt/crt/client.crt' (OS Error: No such file or directory, errno = 2) still appear. @shamblett

Flutter 1.9.1+hotfix.5 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 1aedbb1835 (7 days ago) • 2019-10-17 08:37:27 -0700
Engine • revision b863200c37
Tools • Dart 2.5.0
103style commented 5 years ago

screenshot

103style commented 5 years ago

@shamblett

and when i copy all file of the example dir in my lib, and update iot_core.dart code like below:

final String currDir =
      '${path.current}${path.separator}lib${path.separator}example${path.separator}';
  final SecurityContext context = SecurityContext.defaultContext;
  context.setTrustedCertificates(currDir + path.join('pem', 'roots.pem'));

run app and [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: FileSystemException: Cannot open file, path = '//lib/example/pem/roots.pem' (OS Error: No such file or directory, errno = 2)

Flutter and dart version:

Flutter 1.9.1+hotfix.5 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 1aedbb1835 (7 days ago) • 2019-10-17 08:37:27 -0700
Engine • revision b863200c37
Tools • Dart 2.5.0
shamblett commented 5 years ago

It looks as though path.current isn't working, or flutter doesn't like the '//' at the start, try hard wiring the path if this works print out what path.current returns.

103style commented 5 years ago

It looks as though path.current isn't working, or flutter doesn't like the '//' at the start, try hard wiring the path if this works print out what path.current returns.

and i try config the path in the pubspec.yaml

import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;

Future<String> loadAsset() async {
  return await rootBundle.loadString('crt/crt/client.crt');
}

and rootBundle.loadString('crt/crt/client.crt') haven't show the error,

but when i load the cert use following code, and the same error OS Error: No such file or directory, errno = 2 appear

final SecurityContext context = SecurityContext.defaultContext;
context.setTrustedCertificates('crt/crt/client.crt');

@shamblett

shamblett commented 5 years ago

OK, you seem to have a flutter error or a path package error or something, how you pass paths to the client isn't really its problem your better off asking on a flutter list.

103style commented 5 years ago

OK, you seem to have a flutter error or a path package error or something, how you pass paths to the client isn't really its problem your better off asking on a flutter list.

ok, i had commited in issues

103style commented 5 years ago

@shamblett finally, i decided do it like this, write fisrt.

  Future<int> main() async {
    final SecurityContext context = SecurityContext.defaultContext;
    String crtPath = await _getLocalFile("client.crt");
    context.setTrustedCertificates(crtPath);
    client.securityContext = context;
    client.setProtocolV311();
    return 0;
  }

  Future<String> _getLocalFile(String filename) async {
    String dir = (await getApplicationDocumentsDirectory()).path;
    File file = new File('$dir/$filename');
    bool exist = await file.exists();
    log('exist = $exist');
    if (!exist) {
       file.writeAsString('''-----BEGIN CERTIFICATE-----
          MIIDQDCCAigCCQCPNMDXN3AfRjANBgkqhkiG9w0BAQsFADBmMQswCQYDVQQGEwJF
          N12AHBWWGeOir6+09zdT1ULBgbwDc8l9mN1G94xe/7UUdN9xNmDe2wV8V181Eau9
          jIL5iFFAogR+hHP9H45VLU3QdNY=
          -----END CERTIFICATE-----
          ''');
    }
    String contents = await file.readAsString();
    log(contents);
    return file.path;
  }
103style commented 5 years ago

@shamblett but we have ca.crt , client.key, cilent.crt ,

context.setTrustedCertificates(crtPath); just load a file。

what should i do finish the verification?