Help / Resources for connecting to testnet electrum server

Hello everyone!

I’ve been using this list to try to connect to the testnet servers. https://github.com/Electron-Cash/Electron-Cash/blob/master/electroncash/servers_testnet.json

For whatever reason though, I’m not succeeding although connecting to mainnet servers works just fine. I’m getting errors like this:

SocketException: OS Error: Connection refused, errno = 111, address = testnet imaginary cash, port = 33250
SocketException: HTTP connection timed out after 0:00:01.000000, host: bch0 kister net, port: 51002
Unhandled Exception: HttpException: Invalid request method, uri = https //bch0 kister net:51002
(took out the .'s from the links above to stay within limit)

I have also subscribed to the 1209k site (can’t put the link because of limitations of 2 per new user, but please do google 1209k tbch, it should be the first result) that shows us the status of the server and there doesn’t seem to be an issue with the servers I connect to.

How can I make progress on fixing this, as it was working fine about two weeks ago with the same code for me?

  • Does anyone know how to find info on various server config and their websocket handshake protocols?

  • In case it helps, I am using Dart/Flutter. If anyone is interested in helping to take a look further, I’d be happy to share more of the code or demo with you offline and solve this together!! You will have my eternal gratitude.

Here is a snippet of the relevant code that upgrades existing http connections. Is there something I am obviously doing wrong or is there a better way of connecting to the testnet server?

  final r = Random();
    final key = base64.encode(List<int>.generate(8, (_) => r.nextInt(255)));
    _log.fine('generated key: $key');

    final client = HttpClient();
    client.connectionTimeout = const Duration(milliseconds: 1000);

    // This allows secure connections with bad server certificates (self-signed)
    client.badCertificateCallback =
        (X509Certificate cert, String host, int port) => true;
    final request = await client.getUrl(address);
    request.headers.add('Connection', 'upgrade');
    request.headers.add('Upgrade', 'websocket');
    request.headers
        .add('Sec-WebSocket-Version', '13'); // insert the correct version here
    request.headers.add('Sec-WebSocket-Key', key);

    final response = await request.close();

    _log.fine('connected w status code ${response.statusCode} ');
    // todo check the status code, key etc
    final socket = await response.detachSocket();

    _log.fine(
        'connecting to websocket ${socket.address} at ${socket.remotePort}');

    _rpcSocket = WebSocket.fromUpgradedSocket(
      socket,
      serverSide: false,
    );

(attribution for most of it as adapted from: stackoverflow com/a/53727270)

Many thanks to @test who set up the faucet couple months back. I have been testing transactions sent via the wallet app I’m working on using these coins! And thank you to everyone else for your help in advance!

Here is the link of servers used in this line (final request = await client.getUrl(address);), in case anyone needs it:

List<String> electrumTestnetUrls = [
  // 'https:// testnet. bitcoincash. network:60002', // need to be fixed, maybe find Websocket port like for electroncash.de
  // 'https:// testnet. imaginary. cash:50002', // need to be fixed
  // 'ws:// electroncash. de:60004', // 50003 TCP
  // 'wss:// electroncash. de:60004', // 50003 TCP
  // 'ws:// electroncash. de:60003', // 50003 TCP
  // 'wss:// electroncash. de:60003', // 50003 TCP
  // 18333 Bitcoin Cash testnet
  // 50003 Electron Cash testnet
  // 50004 Electron Cash testnet SSL
  // 60003 Websocket testnet
  // 60004 Websocket testnet SSL
  // 'https:// blackie. c3-soft. com:60002',
  // 'https:// bch0. kister. net:51002',
  // 'https:// electrs. electroncash. de:60002',
  // 'https:// tbch. loping. net:60002',
  'https:// scgjgc67226l65u52wyvulioxixv34p5dth73oj35ej7ham2zavdtxid. onion:60002'
];

Spaces added in the urls to comply with limit on links for new users.

Hi there,

It looks like you’re connecting to the standard “Electrum” port which isn’t HTTP (it’s JSON over bare TCP and/or SSL). You want to connect to the websocket secure (WSS) ports. For example, my server’s WSS port for testnet is: blackie.c3-soft.com port 60004

bch0.kister.net doesn’t happen to have a WSS port.

The 1209k site doesn’t track WSS ports.

1 Like

Thank you so much for explaining Calin :slight_smile: Following your advice, I managed to get the Websocket to connect, but somehow the rpcClient isn’t sticking :frowning:

“Socket disconnected prematurely” is an error given when get rpcSocket turns up an null WebSocket object (see the end of the 2nd chunk of code).

Would you have any idea how to proceed? I’ll sleep over it and try again tomorrow.
Thank you so much already, truly!

Here are the flutter logs:

I/flutter ( 8594): [FINE] JSONRPCWebsocket -- 2021-12-06 22:39:14.741918 -- starting to connect to wss://blackie c3-soft com:60004
I/flutter ( 8594): Exception: Socket disconnected prematurely
I/flutter ( 8594): [FINE] JSONRPCWebsocket -- 2021-12-06 22:39:14.777361 -- starting to connect to wss://bch0 kister net
I/flutter ( 8594): Exception: Socket disconnected prematurely
I/flutter ( 8594): [FINE] JSONRPCWebsocket -- 2021-12-06 22:39:14.798589 -- starting to connect to wss://blackie c3-soft com:60004
I/flutter ( 8594): Exception: Socket disconnected prematurely
I/flutter ( 8594): [SEVERE] Wallet -- 2021-12-06 22:39:24.380052 -- Oh no!


I/flutter ( 8594): [FINE] JSONRPCWebsocket -- 2021-12-06 22:39:27.198612 -- connected to websocket, current state 1
I/flutter ( 8594): [FINE] JSONRPCWebsocket -- 2021-12-06 22:39:27.247353 -- connected to websocket, current state 1


I/flutter ( 8594): [FINE] Wallet -- 2021-12-06 22:40:03.654889 -- Grabbing UTXOs from electrum and then updating balance via vault.
I/flutter ( 8594): [FINE] JSONRPCWebsocket -- 2021-12-06 22:40:03.657014 -- starting to connect to wss://blackie.c3-soft.com:60004
I/flutter ( 8594): Exception: Socket disconnected prematurely
I/flutter ( 8594): [FINE] JSONRPCWebsocket -- 2021-12-06 22:40:03.678117 -- starting to connect to wss://blackie.c3-soft.com:60004
I/flutter ( 8594): Exception: Socket disconnected prematurely
I/flutter ( 8594): [FINE] JSONRPCWebsocket -- 2021-12-06 22:40:03.690499 -- starting to connect to wss://blackie.c3-soft.com:60004
I/flutter ( 8594): Exception: Socket disconnected prematurely
E/flutter ( 8594): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Exception: Socket disconnected prematurely
E/flutter ( 8594): #0      JSONRPCWebsocket.rpcSocket
E/flutter ( 8594): #1      JSONRPCWebsocket.subscribe
E/flutter ( 8594): #2      ElectrumClient.blockchainScripthashSubscribe
E/flutter ( 8594): #3      Wallet.startUtxoListeners
E/flutter ( 8594): #4      new Wallet.<anonymous closure>
E/flutter ( 8594): #5      ElectrumFactory.getInstance
E/flutter ( 8594): #6      ElectrumFactory.getInstance
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594): #7      ElectrumFactory.getInstance
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594): #8      Wallet.refreshBalanceRemote
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594): #9      _DevToolsPageState.build.<anonymous closure>
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594):
I/flutter ( 8594): [FINE] JSONRPCWebsocket -- 2021-12-06 22:40:04.290134 -- connected to websocket, current state 1
I/flutter ( 8594): [FINE] JSONRPCWebsocket -- 2021-12-06 22:40:04.305716 -- connected to websocket, current state 1
I/flutter ( 8594): [FINE] JSONRPCWebsocket -- 2021-12-06 22:40:04.317505 -- connected to websocket, current state 1
I/flutter ( 8594): [FINE] Wallet -- 2021-12-06 22:40:06.847375 -- Grabbing UTXOs from electrum and then updating balance via vault.
I/flutter ( 8594): [FINE] JSONRPCWebsocket -- 2021-12-06 22:40:06.863767 -- starting to connect to wss://blackie.c3-soft.com:60004
I/flutter ( 8594): Exception: Socket disconnected prematurely
I/flutter ( 8594): [FINE] JSONRPCWebsocket -- 2021-12-06 22:40:06.874963 -- starting to connect to wss://blackie.c3-soft.com:60004
I/flutter ( 8594): Exception: Socket disconnected prematurely
I/flutter ( 8594): [FINE] JSONRPCWebsocket -- 2021-12-06 22:40:06.879812 -- starting to connect to wss://blackie.c3-soft.com:60004
I/flutter ( 8594): Exception: Socket disconnected prematurely
E/flutter ( 8594): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Exception: Socket disconnected prematurely
E/flutter ( 8594): #0      JSONRPCWebsocket.rpcSocket
E/flutter ( 8594): #1      JSONRPCWebsocket.subscribe
E/flutter ( 8594): #2      ElectrumClient.blockchainScripthashSubscribe
E/flutter ( 8594): #3      Wallet.startUtxoListeners
E/flutter ( 8594): #4      new Wallet.<anonymous closure>
E/flutter ( 8594): #5      ElectrumFactory.getInstance
E/flutter ( 8594): #6      ElectrumFactory.getInstance
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594): #7      ElectrumFactory.getInstance
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594): #8      Wallet.refreshBalanceRemote
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594): #9      _DevToolsPageState.build.<anonymous closure>
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594):
I/flutter ( 8594): [FINE] JSONRPCWebsocket -- 2021-12-06 22:40:07.483055 -- connected to websocket, current state 1
I/flutter ( 8594): [FINE] JSONRPCWebsocket -- 2021-12-06 22:40:07.494254 -- connected to websocket, current state 1
I/flutter ( 8594): [FINE] JSONRPCWebsocket -- 2021-12-06 22:40:07.507279 -- connected to websocket, current state 1
I/flutter ( 8594): ping
E/flutter ( 8594): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Exception: Socket disconnected prematurely
E/flutter ( 8594): #0      JSONRPCWebsocket.rpcSocket
E/flutter ( 8594): #1      new ElectrumClient.<anonymous closure>
E/flutter ( 8594): #2      _rootRun (dart:async/zone.dart:1420:47)
E/flutter ( 8594): #3      _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 8594): #4      _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
E/flutter ( 8594): #5      _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1276:23)
E/flutter ( 8594): #6      _rootRun (dart:async/zone.dart:1428:13)
E/flutter ( 8594): #7      _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 8594): #8      _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1260:23)
E/flutter ( 8594): #9      Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter ( 8594): #10     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:395:19)
E/flutter ( 8594): #11     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:426:5)
E/flutter ( 8594): #12     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:187:12)
E/flutter ( 8594):
I/flutter ( 8594): ping
E/flutter ( 8594): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = bch0*kister*net, port = 44204
E/flutter ( 8594): #0      _NativeSocket.startConnect (dart:io-patch/socket_patch.dart:677:35)
E/flutter ( 8594): #1      _RawSocket.startConnect (dart:io-patch/socket_patch.dart:1680:26)
E/flutter ( 8594): #2      RawSocket.startConnect (dart:io-patch/socket_patch.dart:27:23)
E/flutter ( 8594): #3      RawSecureSocket.startConnect (dart:io/secure_socket.dart:237:22)
E/flutter ( 8594): #4      SecureSocket.startConnect (dart:io/secure_socket.dart:60:28)
E/flutter ( 8594): #5      _ConnectionTarget.connect (dart:_http/http_impl.dart:2437:24)
E/flutter ( 8594): #6      _HttpClient._getConnection.connect (dart:_http/http_impl.dart:2805:12)
E/flutter ( 8594): #7      _HttpClient._getConnection (dart:_http/http_impl.dart:2810:12)
E/flutter ( 8594): #8      _HttpClient._openUrl (dart:_http/http_impl.dart:2694:12)
E/flutter ( 8594): #9      _HttpClient.openUrl (dart:_http/http_impl.dart:2568:7)
E/flutter ( 8594): #10     _WebSocketImpl.connect (dart:_http/websocket_impl.dart:1027:24)
E/flutter ( 8594): #11     WebSocket.connect (dart:_http/websocket.dart:379:22)
E/flutter ( 8594): #12     JSONRPCWebsocket.connect
E/flutter ( 8594): #13     ElectrumFactory.getInstance
E/flutter ( 8594): #14     ElectrumFactory.getInstance
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594): #15     Wallet.initialize
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594): #16     WalletModel.initializeModel
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594):
I/flutter ( 8594): ping
I/chatty  ( 8594): uid=10133(com.willyounerime.neri) 1.ui identical 1 line
I/flutter ( 8594): ping



E/flutter ( 8594): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Exception: Socket disconnected prematurely
E/flutter ( 8594): #0      JSONRPCWebsocket.rpcSocket
E/flutter ( 8594): #1      new ElectrumClient.<anonymous closure>
E/flutter ( 8594): #2      _rootRun (dart:async/zone.dart:1420:47)
E/flutter ( 8594): #3      _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 8594): #4      _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
E/flutter ( 8594): #5      _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1276:23)
E/flutter ( 8594): #6      _rootRun (dart:async/zone.dart:1428:13)
E/flutter ( 8594): #7      _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 8594): #8      _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1260:23)
E/flutter ( 8594): #9      Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter ( 8594): #10     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:395:19)
E/flutter ( 8594): #11     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:426:5)
E/flutter ( 8594): #12     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:187:12)
E/flutter ( 8594):
I/chatty  ( 8594): uid=10133(com.willyounerime.neri) 1.ui identical 1 line
E/flutter ( 8594): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Exception: Socket disconnected prematurely
E/flutter ( 8594): #0      JSONRPCWebsocket.rpcSocket
E/flutter ( 8594): #1      new ElectrumClient.<anonymous closure>
E/flutter ( 8594): #2      _rootRun (dart:async/zone.dart:1420:47)
E/flutter ( 8594): #3      _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 8594): #4      _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
E/flutter ( 8594): #5      _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1276:23)
E/flutter ( 8594): #6      _rootRun (dart:async/zone.dart:1428:13)
E/flutter ( 8594): #7      _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 8594): #8      _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1260:23)
E/flutter ( 8594): #9      Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter ( 8594): #10     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:395:19)
E/flutter ( 8594): #11     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:426:5)
E/flutter ( 8594): #12     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:187:12)
E/flutter ( 8594):
E/flutter ( 8594): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Exception: Socket disconnected prematurely
E/flutter ( 8594): #0      JSONRPCWebsocket.rpcSocket
E/flutter ( 8594): #1      new ElectrumClient.<anonymous closure>
E/flutter ( 8594): #2      _rootRun (dart:async/zone.dart:1420:47)
E/flutter ( 8594): #3      _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 8594): #4      _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
E/flutter ( 8594): #5      _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1276:23)
E/flutter ( 8594): #6      _rootRun (dart:async/zone.dart:1428:13)
E/flutter ( 8594): #7      _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 8594): #8      _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1260:23)
E/flutter ( 8594): #9      Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter ( 8594): #10     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:395:19)
E/flutter ( 8594): #11     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:426:5)
E/flutter ( 8594): #12     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:187:12)
E/flutter ( 8594):
E/flutter ( 8594): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Exception: Socket disconnected prematurely
E/flutter ( 8594): #0      JSONRPCWebsocket.rpcSocket
E/flutter ( 8594): #1      new ElectrumClient.<anonymous closure>
E/flutter ( 8594): #2      _rootRun (dart:async/zone.dart:1420:47)
E/flutter ( 8594): #3      _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 8594): #4      _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
E/flutter ( 8594): #5      _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1276:23)
E/flutter ( 8594): #6      _rootRun (dart:async/zone.dart:1428:13)
E/flutter ( 8594): #7      _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 8594): #8      _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1260:23)
E/flutter ( 8594): #9      Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter ( 8594): #10     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:395:19)
E/flutter ( 8594): #11     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:426:5)
E/flutter ( 8594): #12     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:187:12)
E/flutter ( 8594):
I/flutter ( 8594): ping
E/flutter ( 8594): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = bch0*kister*net, port = 44232
E/flutter ( 8594): #0      _NativeSocket.startConnect (dart:io-patch/socket_patch.dart:677:35)
E/flutter ( 8594): #1      _RawSocket.startConnect (dart:io-patch/socket_patch.dart:1680:26)
E/flutter ( 8594): #2      RawSocket.startConnect (dart:io-patch/socket_patch.dart:27:23)
E/flutter ( 8594): #3      RawSecureSocket.startConnect (dart:io/secure_socket.dart:237:22)
E/flutter ( 8594): #4      SecureSocket.startConnect (dart:io/secure_socket.dart:60:28)
E/flutter ( 8594): #5      _ConnectionTarget.connect (dart:_http/http_impl.dart:2437:24)
E/flutter ( 8594): #6      _HttpClient._getConnection.connect (dart:_http/http_impl.dart:2805:12)
E/flutter ( 8594): #7      _HttpClient._getConnection (dart:_http/http_impl.dart:2810:12)
E/flutter ( 8594): #8      _HttpClient._openUrl (dart:_http/http_impl.dart:2694:12)
E/flutter ( 8594): #9      _HttpClient.openUrl (dart:_http/http_impl.dart:2568:7)
E/flutter ( 8594): #10     _WebSocketImpl.connect (dart:_http/websocket_impl.dart:1027:24)
E/flutter ( 8594): #11     WebSocket.connect (dart:_http/websocket.dart:379:22)
E/flutter ( 8594): #12     JSONRPCWebsocket.connect
E/flutter ( 8594): #13     ElectrumFactory.getInstance
E/flutter ( 8594): #14     Wallet.refreshBalanceRemote
E/flutter ( 8594): #15     _DevToolsPageState.build.<anonymous closure>
E/flutter ( 8594): #16     _DevToolsPageState.build.<anonymous closure>
E/flutter ( 8594): #17     _InkResponseState._handleTap
E/flutter ( 8594): #18     GestureRecognizer.invokeCallback
E/flutter ( 8594): #19     TapGestureRecognizer.handleTapUp
E/flutter ( 8594): #20     BaseTapGestureRecognizer._checkUp
E/flutter ( 8594): #21     BaseTapGestureRecognizer.handlePrimaryPointer
E/flutter ( 8594): #22     PrimaryPointerGestureRecognizer.handleEvent
E/flutter ( 8594): #23     PointerRouter._dispatch
E/flutter ( 8594): #24     PointerRouter._dispatchEventToRoutes.<anonymous closure>
E/flutter ( 8594): #25     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:400:8)
E/flutter ( 8594): #26     PointerRouter._dispatchEventToRoutes
E/flutter ( 8594): #27     PointerRouter.route
E/flutter ( 8594): #28     GestureBinding.handleEvent
E/flutter ( 8594): #29     GestureBinding.dispatchEvent
E/flutter ( 8594): #30     RendererBinding.dispatchEvent
E/flutter ( 8594): #31     GestureBinding._handlePointerEventImmediately
E/flutter ( 8594): #32     GestureBinding.handlePointerEvent
E/flutter ( 8594): #33     GestureBinding._flushPointerEventQueue
E/flutter ( 8594): #34     GestureBinding._handlePointerDataPacket
E/flutter ( 8594): #35     _rootRunUnary (dart:async/zone.dart:1444:13)
E/flutter ( 8594): #36     _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter ( 8594): #37     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
E/flutter ( 8594): #38     _invoke1 (dart:ui/hooks.dart:185:10)
E/flutter ( 8594): #39     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:293:7)
E/flutter ( 8594): #40     _dispatchPointerDataPacket (dart:ui/hooks.dart:98:31)
E/flutter ( 8594):
E/flutter ( 8594): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = bch0*kister*net, port = 44230
E/flutter ( 8594): #0      _NativeSocket.startConnect (dart:io-patch/socket_patch.dart:677:35)
E/flutter ( 8594): #1      _RawSocket.startConnect (dart:io-patch/socket_patch.dart:1680:26)
E/flutter ( 8594): #2      RawSocket.startConnect (dart:io-patch/socket_patch.dart:27:23)
E/flutter ( 8594): #3      RawSecureSocket.startConnect (dart:io/secure_socket.dart:237:22)
E/flutter ( 8594): #4      SecureSocket.startConnect (dart:io/secure_socket.dart:60:28)
E/flutter ( 8594): #5      _ConnectionTarget.connect (dart:_http/http_impl.dart:2437:24)
E/flutter ( 8594): #6      _HttpClient._getConnection.connect (dart:_http/http_impl.dart:2805:12)
E/flutter ( 8594): #7      _HttpClient._getConnection (dart:_http/http_impl.dart:2810:12)
E/flutter ( 8594): #8      _HttpClient._openUrl (dart:_http/http_impl.dart:2694:12)
E/flutter ( 8594): #9      _HttpClient.openUrl (dart:_http/http_impl.dart:2568:7)
E/flutter ( 8594): #10     _WebSocketImpl.connect (dart:_http/websocket_impl.dart:1027:24)
E/flutter ( 8594): #11     WebSocket.connect (dart:_http/websocket.dart:379:22)
E/flutter ( 8594): #12     JSONRPCWebsocket.connect
E/flutter ( 8594): #13     ElectrumFactory.getInstance
E/flutter ( 8594): #14     ElectrumFactory.getInstance
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594): #15     ElectrumFactory.getInstance
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594): #16     Wallet.refreshBalanceRemote
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594): #17     _DevToolsPageState.build.<anonymous closure>
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594):
E/flutter ( 8594): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = bch0*kister*net, port = 44228
E/flutter ( 8594): #0      _NativeSocket.startConnect (dart:io-patch/socket_patch.dart:677:35)
E/flutter ( 8594): #1      _RawSocket.startConnect (dart:io-patch/socket_patch.dart:1680:26)
E/flutter ( 8594): #2      RawSocket.startConnect (dart:io-patch/socket_patch.dart:27:23)
E/flutter ( 8594): #3      RawSecureSocket.startConnect (dart:io/secure_socket.dart:237:22)
E/flutter ( 8594): #4      SecureSocket.startConnect (dart:io/secure_socket.dart:60:28)
E/flutter ( 8594): #5      _ConnectionTarget.connect (dart:_http/http_impl.dart:2437:24)
E/flutter ( 8594): #6      _HttpClient._getConnection.connect (dart:_http/http_impl.dart:2805:12)
E/flutter ( 8594): #7      _HttpClient._getConnection (dart:_http/http_impl.dart:2810:12)
E/flutter ( 8594): #8      _HttpClient._openUrl (dart:_http/http_impl.dart:2694:12)
E/flutter ( 8594): #9      _HttpClient.openUrl (dart:_http/http_impl.dart:2568:7)
E/flutter ( 8594): #10     _WebSocketImpl.connect (dart:_http/websocket_impl.dart:1027:24)
E/flutter ( 8594): #11     WebSocket.connect (dart:_http/websocket.dart:379:22)
E/flutter ( 8594): #12     JSONRPCWebsocket.connect
E/flutter ( 8594): #13     ElectrumFactory.getInstance
E/flutter ( 8594): #14     ElectrumFactory.getInstance
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594): #15     Wallet.refreshBalanceRemote
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594): #16     _DevToolsPageState.build.<anonymous closure>
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594):
I/flutter ( 8594): ping
E/flutter ( 8594): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Exception: Socket disconnected prematurely
E/flutter ( 8594): #0      JSONRPCWebsocket.rpcSocket
E/flutter ( 8594): #1      new ElectrumClient.<anonymous closure>
E/flutter ( 8594): #2      _rootRun (dart:async/zone.dart:1420:47)
E/flutter ( 8594): #3      _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 8594): #4      _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
E/flutter ( 8594): #5      _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1276:23)
E/flutter ( 8594): #6      _rootRun (dart:async/zone.dart:1428:13)
E/flutter ( 8594): #7      _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 8594): #8      _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1260:23)
E/flutter ( 8594): #9      Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter ( 8594): #10     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:395:19)
E/flutter ( 8594): #11     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:426:5)
E/flutter ( 8594): #12     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:187:12)
E/flutter ( 8594):
E/flutter ( 8594): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Exception: Socket disconnected prematurely
E/flutter ( 8594): #0      JSONRPCWebsocket.rpcSocket
E/flutter ( 8594): #1      new ElectrumClient.<anonymous closure>
E/flutter ( 8594): #2      _rootRun (dart:async/zone.dart:1420:47)
E/flutter ( 8594): #3      _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 8594): #4      _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
E/flutter ( 8594): #5      _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1276:23)
E/flutter ( 8594): #6      _rootRun (dart:async/zone.dart:1428:13)
E/flutter ( 8594): #7      _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 8594): #8      _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1260:23)
E/flutter ( 8594): #9      Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter ( 8594): #10     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:395:19)
E/flutter ( 8594): #11     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:426:5)
E/flutter ( 8594): #12     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:187:12)
E/flutter ( 8594):
I/lyounerime.ner( 8594): NativeAlloc concurrent copying GC freed 7228(349KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 2510KB/5021KB, paused 588us total 218.375ms
E/flutter ( 8594): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = bch0*kister*net, port = 44240
E/flutter ( 8594): #0      _NativeSocket.startConnect (dart:io-patch/socket_patch.dart:677:35)
E/flutter ( 8594): #1      _RawSocket.startConnect (dart:io-patch/socket_patch.dart:1680:26)
E/flutter ( 8594): #2      RawSocket.startConnect (dart:io-patch/socket_patch.dart:27:23)
E/flutter ( 8594): #3      RawSecureSocket.startConnect (dart:io/secure_socket.dart:237:22)
E/flutter ( 8594): #4      SecureSocket.startConnect (dart:io/secure_socket.dart:60:28)
E/flutter ( 8594): #5      _ConnectionTarget.connect (dart:_http/http_impl.dart:2437:24)
E/flutter ( 8594): #6      _HttpClient._getConnection.connect (dart:_http/http_impl.dart:2805:12)
E/flutter ( 8594): #7      _HttpClient._getConnection (dart:_http/http_impl.dart:2810:12)
E/flutter ( 8594): #8      _HttpClient._openUrl (dart:_http/http_impl.dart:2694:12)
E/flutter ( 8594): #9      _HttpClient.openUrl (dart:_http/http_impl.dart:2568:7)
E/flutter ( 8594): #10     _WebSocketImpl.connect (dart:_http/websocket_impl.dart:1027:24)
E/flutter ( 8594): #11     WebSocket.connect (dart:_http/websocket.dart:379:22)
E/flutter ( 8594): #12     JSONRPCWebsocket.connect
E/flutter ( 8594): #13     ElectrumFactory.getInstance
E/flutter ( 8594): #14     ElectrumFactory.getInstance
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594): #15     Wallet.refreshBalanceRemote
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594): #16     _DevToolsPageState.build.<anonymous closure>
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594):
E/flutter ( 8594): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = bch0*kister*net, port = 44238
E/flutter ( 8594): #0      _NativeSocket.startConnect (dart:io-patch/socket_patch.dart:677:35)
E/flutter ( 8594): #1      _RawSocket.startConnect (dart:io-patch/socket_patch.dart:1680:26)
E/flutter ( 8594): #2      RawSocket.startConnect (dart:io-patch/socket_patch.dart:27:23)
E/flutter ( 8594): #3      RawSecureSocket.startConnect (dart:io/secure_socket.dart:237:22)
E/flutter ( 8594): #4      SecureSocket.startConnect (dart:io/secure_socket.dart:60:28)
E/flutter ( 8594): #5      _ConnectionTarget.connect (dart:_http/http_impl.dart:2437:24)
E/flutter ( 8594): #6      _HttpClient._getConnection.connect (dart:_http/http_impl.dart:2805:12)
E/flutter ( 8594): #7      _HttpClient._getConnection (dart:_http/http_impl.dart:2810:12)
E/flutter ( 8594): #8      _HttpClient._openUrl (dart:_http/http_impl.dart:2694:12)
E/flutter ( 8594): #9      _HttpClient.openUrl (dart:_http/http_impl.dart:2568:7)
E/flutter ( 8594): #10     _WebSocketImpl.connect (dart:_http/websocket_impl.dart:1027:24)
E/flutter ( 8594): #11     WebSocket.connect (dart:_http/websocket.dart:379:22)
E/flutter ( 8594): #12     JSONRPCWebsocket.connect
E/flutter ( 8594): #13     ElectrumFactory.getInstance
E/flutter ( 8594): #14     Wallet.refreshBalanceRemote
E/flutter ( 8594): #15     _DevToolsPageState.build.<anonymous closure>
E/flutter ( 8594): #16     _DevToolsPageState.build.<anonymous closure>
E/flutter ( 8594): #17     _InkResponseState._handleTap
E/flutter ( 8594): #18     GestureRecognizer.invokeCallback
E/flutter ( 8594): #19     TapGestureRecognizer.handleTapUp
E/flutter ( 8594): #20     BaseTapGestureRecognizer._checkUp
E/flutter ( 8594): #21     BaseTapGestureRecognizer.handlePrimaryPointer
E/flutter ( 8594): #22     PrimaryPointerGestureRecognizer.handleEvent
E/flutter ( 8594): #23     PointerRouter._dispatch
E/flutter ( 8594): #24     PointerRouter._dispatchEventToRoutes.<anonymous closure>
E/flutter ( 8594): #25     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:400:8)
E/flutter ( 8594): #26     PointerRouter._dispatchEventToRoutes
E/flutter ( 8594): #27     PointerRouter.route
E/flutter ( 8594): #28     GestureBinding.handleEvent
E/flutter ( 8594): #29     GestureBinding.dispatchEvent
E/flutter ( 8594): #30     RendererBinding.dispatchEvent
E/flutter ( 8594): #31     GestureBinding._handlePointerEventImmediately
E/flutter ( 8594): #32     GestureBinding.handlePointerEvent
E/flutter ( 8594): #33     GestureBinding._flushPointerEventQueue
E/flutter ( 8594): #34     GestureBinding._handlePointerDataPacket
E/flutter ( 8594): #35     _rootRunUnary (dart:async/zone.dart:1444:13)
E/flutter ( 8594): #36     _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter ( 8594): #37     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
E/flutter ( 8594): #38     _invoke1 (dart:ui/hooks.dart:185:10)
E/flutter ( 8594): #39     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:293:7)
E/flutter ( 8594): #40     _dispatchPointerDataPacket (dart:ui/hooks.dart:98:31)
E/flutter ( 8594):
E/flutter ( 8594): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = bch0*kister*net, port = 44250
E/flutter ( 8594): #0      _NativeSocket.startConnect (dart:io-patch/socket_patch.dart:677:35)
E/flutter ( 8594): #1      _RawSocket.startConnect (dart:io-patch/socket_patch.dart:1680:26)
E/flutter ( 8594): #2      RawSocket.startConnect (dart:io-patch/socket_patch.dart:27:23)
E/flutter ( 8594): #3      RawSecureSocket.startConnect (dart:io/secure_socket.dart:237:22)
E/flutter ( 8594): #4      SecureSocket.startConnect (dart:io/secure_socket.dart:60:28)
E/flutter ( 8594): #5      _ConnectionTarget.connect (dart:_http/http_impl.dart:2437:24)
E/flutter ( 8594): #6      _HttpClient._getConnection.connect (dart:_http/http_impl.dart:2805:12)
E/flutter ( 8594): #7      _HttpClient._getConnection (dart:_http/http_impl.dart:2810:12)
E/flutter ( 8594): #8      _HttpClient._openUrl (dart:_http/http_impl.dart:2694:12)
E/flutter ( 8594): #9      _HttpClient.openUrl (dart:_http/http_impl.dart:2568:7)
E/flutter ( 8594): #10     _WebSocketImpl.connect (dart:_http/websocket_impl.dart:1027:24)
E/flutter ( 8594): #11     WebSocket.connect (dart:_http/websocket.dart:379:22)
E/flutter ( 8594): #12     JSONRPCWebsocket.connect
E/flutter ( 8594): #13     ElectrumFactory.getInstance
E/flutter ( 8594): #14     ElectrumFactory.getInstance
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594): #15     Wallet.refreshBalanceRemote
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594): #16     _DevToolsPageState.build.<anonymous closure>
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594):
E/flutter ( 8594): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = bch0*kister*net, port = 44252
E/flutter ( 8594): #0      _NativeSocket.startConnect (dart:io-patch/socket_patch.dart:677:35)
E/flutter ( 8594): #1      _RawSocket.startConnect (dart:io-patch/socket_patch.dart:1680:26)
E/flutter ( 8594): #2      RawSocket.startConnect (dart:io-patch/socket_patch.dart:27:23)
E/flutter ( 8594): #3      RawSecureSocket.startConnect (dart:io/secure_socket.dart:237:22)
E/flutter ( 8594): #4      SecureSocket.startConnect (dart:io/secure_socket.dart:60:28)
E/flutter ( 8594): #5      _ConnectionTarget.connect (dart:_http/http_impl.dart:2437:24)
E/flutter ( 8594): #6      _HttpClient._getConnection.connect (dart:_http/http_impl.dart:2805:12)
E/flutter ( 8594): #7      _HttpClient._getConnection (dart:_http/http_impl.dart:2810:12)
E/flutter ( 8594): #8      _HttpClient._openUrl (dart:_http/http_impl.dart:2694:12)
E/flutter ( 8594): #9      _HttpClient.openUrl (dart:_http/http_impl.dart:2568:7)
E/flutter ( 8594): #10     _WebSocketImpl.connect (dart:_http/websocket_impl.dart:1027:24)
E/flutter ( 8594): #11     WebSocket.connect (dart:_http/websocket.dart:379:22)
E/flutter ( 8594): #12     JSONRPCWebsocket.connect
E/flutter ( 8594): #13     ElectrumFactory.getInstance
E/flutter ( 8594): #14     ElectrumFactory.getInstance
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594): #15     ElectrumFactory.getInstance
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594): #16     Wallet.refreshBalanceRemote
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594): #17     _DevToolsPageState.build.<anonymous closure>
E/flutter ( 8594): <asynchronous suspension>
E/flutter ( 8594):

And here is some of the RPCWebsocket code which is referenced in the error logs…:

  void connect(String address) async {
    _log.fine('starting to connect to $address');

    _rpcSocket = await WebSocket.connect(address);

    _log.fine('connected to websocket, current state ${rpcSocket.readyState}');

    _rpcSocket.listen((dynamic data) {
      // jsonDecode takes the raw JSON data and decodes into a map - jsonResult.
      Map<String, dynamic> jsonResult = jsonDecode(data);
      // Logs the resulting decoded Json.
      // _log.fine('listening result: $jsonResult');

      // Something is wrong with the logic here? We are attempting to
      // deserialize into RPCResponse without first checking if
      // it is a notification. However, in my logs, I rarely see
      // a notification or case where there is no id...

      // Attempt to deserialize response
      final response = RPCResponse.fromJson(jsonResult);
      // _log.fine('jsonResult id: ${response.id}');

      // Checks if the result is a Notification (RPCRequest object)
      // or a Response (RPCResponse object)
      if (response.id == null) {
        final notification = RPCRequest.fromJson(jsonResult);
        return _handleNotification(notification);
      }
      return _handleResponse(response);
    }, onError: (Object error) {
      if (disconnectHandler != null) {
        disconnectHandler(error);
      }
      for (final requestHandler in outstandingRequests.entries) {
        requestHandler.value(RPCResponse(null,
            error: Error(
                0, 'Disconnected from electrum while awaiting response')));
      }
    }, onDone: () {
      if (disconnectHandler != null) {
        disconnectHandler(null);
      }
      for (final requestHandler in outstandingRequests.entries) {
        requestHandler.value(RPCResponse(null,
            error: Error(
                0, 'Disconnected from electrum while awaiting response')));
      }
      // Nothing to do?
    }, cancelOnError: true);
  }

  WebSocket get rpcSocket {
    if (_rpcSocket == null) {
      throw Exception('Socket disconnected prematurely');
    }
    return _rpcSocket;
  }

class ElectrumClient extends JSONRPCWebsocket {
  static final _log = Logger('ElectrumClient');

  // ignore: unused_field
  Timer _pingTimer;

  ElectrumClient({DisconnectHandler disconnectHandler})
      : super(disconnectHandler: disconnectHandler) {
    _pingTimer = Timer(Duration(seconds: 60), () {
      if (rpcSocket != null) {
        print('ping');
        serverPing();
      }
    });
  }

class ElectrumFactory {
  ElectrumFactory(this.urls, {this.onConnected});

  ElectrumClient _client;
  List<String> urls;
  ConnectHandler onConnected;

  /// Builds client if non-existent and attempts to connect before resolving.
  Future<ElectrumClient> getInstance({int retry = 2}) async {
    try {
      if (_client == null) {
        _client = ElectrumClient(disconnectHandler: (error) {
          _client.dispose();
          _client = null;
        });
        final urls = this.urls.sublist(0);
        urls.shuffle();
        // await _client.connect(Uri.parse(urls[0]));
        _client.connect((urls[0]));
        await onConnected(_client);
      }
    } catch (err) {
      print(err);
      _client = null;
      if (retry == 0) {
        rethrow;
      }
      return await getInstance(retry: retry - 1);
    }
    return _client;
  }
}

Finally fixed the issue and marked @cculianu’s response as solution :slight_smile:

I was having issues with WebSocket (it kept dropping the connection) and had to use WebSocketChannel to manage the WebSocket instead.

Anyone else who needs to know more please feel free to raise a question or contact me. Thanks so much @cculianu :slight_smile: