Sdk: SocketException sometimes reports the incorrect port

Created on 27 Feb 2019  路  17Comments  路  Source: dart-lang/sdk

Hello, I am trying to make a http get request with the following code:

        var uri = new Uri.http('10.125.32.128:10000', '/keyboard');
        print("Targeting " + uri.toString());
        http.get(uri).then((response) {
          print(response);
        });

In the console error, I unfortunately get this response:

flutter: Targeting http://10.125.32.128:10000/keyboard
[VERBOSE-2:shell.cc(184)] Dart Error: Unhandled exception:
SocketException: OS Error: Operation timed out, errno = 60, address = 10.125.32.128, port = 56325

I have assigned port 10000, but the error message says port 56325 is rejected. I have specified and seems to change the port number randomly that it's targeting. Is there a different way I should be specifying the port #?

Note: I am using:

dev_dependencies:
  flutter_test:
    sdk: flutter
  http: ^0.12.0+1
area-library closed-duplicate library-io type-bug

Most helpful comment

none of this actually worked for me :/
this command worked like magic "adb reverse tcp:5000 tcp:5000" 馃挴

All 17 comments

Update: This has nothing to do with the http library. Check out my other comment below.


~I suspect that something else is wrong and leading to this misleading error message, but fwiw, I am seeing the same error when building to Android simulators on MacOS.~

~Confirmed the error on 0.12.0+1 and 0.12.0+2.~

Can you try this request with dart:io directly? I don't think there is anything we can do to solve this in this package and the bug probably needs to be filed against the SDK.

I'm having the same issue, but on Android, I can reach the endpoint with Google Chorme (which means that is not the host misconfigured or the firewall blocking the connection) but I can't reach it with with http.get method

@DouglasDRF, same server or different server?

Does it hang, or is it a different bug? Does it work on desktop?

I'm seeing the same as @Deftness, it looks like a bug. The port number in the string is being ignored no matter how you try to specify it, and simply issues a random one like 35708 when I want to use 8080. Please can you fix this and write a failing test first, thx, as it's stopped my current work flow.

@Deftness This code worked for me

import 'package:http/http.dart' as http;

main() {
  var client = new http.Client();

  var uri = Uri.parse("http://127.0.0.1:8080");
  print(uri.host);
  print(uri.port);

  client.get(uri).then((response) {
    print("Get " + response.body);
  });

  var body =
      """{"name": "Order",  "location": "in_store",  "items": [  {  "milk": "whole",  "size": "medium",  "drink": "americano"}]}""";

  client.post("http://127.0.0.1:8080/order", body: body).then((response) {
    print("Post " + response.statusCode.toString());
  });
}

@tim-holbrook has confirmed this worked for him.

When using the HttpClient in dart:io i get issues with port not being used but as dart:http is now independent of dart:io this is fine. I guess there is an issue in dart:io around parsing with ports @natebosch

This SO post very likely contains your answer.

When testing this issue I just used dart and ran on a command line, no flutter or emulator, for which I used and external IP. I just swapped in the above example as to not publicly share it.

Moved to the SDK repo since it's not specific to package:http. I wonder if the issue is strictly in the error message...

So I agree with @natebosch I believe this is just an issue with the error message.

This code works fine with custom ports (yes its a bit dirty but proves the SDK is fine)

import 'dart:async';
import 'dart:convert';
import 'dart:io';

readResponse(HttpClientResponse response) async {
  var completer = Completer();
  var contents = StringBuffer();
  response.transform(utf8.decoder).listen((data) {
    contents.write(data);
  }, onDone: () => completer.complete(contents.toString()));
  return await completer.future;
}

main() async {
  var uri = Uri.parse("http://10.0.0.1:6100/get_status");
  print(uri.host); // 10.0.0.1
  print(uri.port); // 6100
  print(uri.path); // get_status

  HttpClient client = new HttpClient();
  HttpClientRequest request = await client.getUrl(uri);
  HttpClientResponse response = await request.close();
  print(readResponse(response));

  var uri2 = Uri.http('10.0.0.1:5100', 'get_status');
  print(uri2.host); //10.0.0.1
  print(uri2.port); // 5100
  print(uri2.path); // get_status

  HttpClientRequest request2 = await client.getUrl(uri2);
  HttpClientResponse response2 = await request2.close();
  print(readResponse(response2));
}

However, if I change the port to something that does not have anything on it I get an error with the correct IP but the port is not the correct one
SocketException: OS Error: Connection refused, errno = 61, address = 10.0.0.1, port = 54044

I am not opposed to changing the title of this issue?

Looks like there are few reporting the same issue under different situations

36674 #12693

Thanks @georgeherby ! Will close as a duplicate of #12693 which has the most context.

none of this actually worked for me :/
this command worked like magic "adb reverse tcp:5000 tcp:5000" 馃挴

I tried to find how to resolve this issue. Nothing worked for me. Just turned on my wifi.

i tried everything, working on emulator, android, but the same issue appeared all the time.

even tried adb reverse tcp:port tcp:port

then i used an application called _ngrok_ which redirects to your localhost:port.
still, i was getting the same error.

then, i disabled windows firewall and eureka!!!!

hope this helps someone.

I have also got same problem. I get normally api data from browser (localhost, myPort). When tried to call api from flutter, I got error that details are "Connection timeout .. address=right IpAddress, but port= incorrected(randomly set port number)". Any help?

I have also got same problem. I get normally api data from browser (localhost, myPort). When tried to call api from flutter, I got error that details are "Connection timeout .. address=right IpAddress, but port= incorrected(randomly set port number)". Any help?

Try disabling the windows firewall completely. It worked for me.

Was this page helpful?
0 / 5 - 0 ratings