1

In my Rust app, I start Iron like so:

let host: &str = &format!("localhost:{}", port);
info!("Server running at http://{}", host);
Iron::new(Chain::new(router)).http(host).expect("Could not start Iron server");

It responds:

INFO Server running at http://localhost:3000

I can curl it:

$ curl "http://localhost:3000/v1/foo"
{"bar":"baz"}

However, in Scala I cannot connect:

$ scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_40).
Type in expressions for evaluation. Or try :help.

scala> scala.io.Source.fromURL("http://localhost:3000/v1/foo").mkString
java.net.ConnectException: Connection refused
  at java.net.PlainSocketImpl.socketConnect(Native Method)

spray-client also cannot connect:

spray.can.Http$ConnectionAttemptFailedException: Connection attempt to 127.0.0.1:3000 failed

Both of these attempts are from the same IP and localhost is correct. The Iron server logs nothing on a failed connection request.

Different combinations of localhost vs 127.0.0.1 in both client and server do not fix the problem. I misdiagnosed this. Using 127.0.0.1 in the Rust client does fix the problem.

After taking a break, the code started working. I don't recall if I restarted Iron. I then did several hours of development against it. At some stage it stopped working again. Restarts of the JVM and/or Iron server are not helping to fix the issue.

This is not specific to my Rust app;

I can recreate the problem with the example hello world Iron app.

$ git clone https://github.com/iron/iron.git
$ (cd iron && cargo run --example hello)

and then

$ curl "http://localhost:3000/"
Hello world!

but

$ scala
scala> scala.io.Source.fromURL("http://localhost:3000/").mkString
java.net.ConnectException: Connection refused
  • OSX 10.11.6
  • cargo 0.13.0-nightly (9399229 2016-09-14)
  • also tested against cargo 0.13.0-nightly (19cfb67 2016-09-28)
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Synesso
  • 37,610
  • 35
  • 136
  • 207
  • 2
    In your Scala app, if you connect to 127.0.0.1, instead of localhost, would that work? (or similarly, if you start the rust app on 127.0.0.1?) – Erik Pragt Sep 30 '16 at 00:39
  • Thanks for the suggestion Erik. After taking lunch and coming back again, it is working. I don't know what could have been wrong, but I'm assuming I made a simple mistake. Deleting this question... – Synesso Sep 30 '16 at 03:16
  • Restored this question. It is an intermittent problem! – Synesso Sep 30 '16 at 06:24
  • Guess 1: you are running on Windows and the firewall or antivirus is blocking Java from connecting anywhere. – yǝsʞǝla Sep 30 '16 at 06:57
  • Guess 2: You have some weird network setup where localhost needs to be looked up from DNS rather than from hosts file. Maybe you are behind a corporate proxy and this messes up the lookup. curl can use proxy settings from the environment vars, java can't so you have to pass it through JVM args or to your application. – yǝsʞǝla Sep 30 '16 at 07:01
  • OSX & no proxy settings. Vanilla all the way down. – Synesso Sep 30 '16 at 07:02
  • Maybe it's time to fire up Wireshark and see what's going on. You can filter by localhost interface and HTTP or TCP/IP protocol. – yǝsʞǝla Sep 30 '16 at 07:09
  • Check you don't have any weird invisible characters in the address string - could be a result of copy paste from somewhere – yǝsʞǝla Sep 30 '16 at 07:27
  • Aleksey, Erik sorry for the misinformation in the original question. I misdiagnosed the problem. Using `127.0.0.1` in the client must have worked (I guess I didn't wait for my extra long server initialisation before testing). – Synesso Sep 30 '16 at 08:41
  • 1
    Thanks for the followup, interesting problem – yǝsʞǝla Sep 30 '16 at 08:42

1 Answers1

4

According to this comment on this bug report, "Iron will resolve ('localhost') to IPv6 by default while your other services use IPv4"

Bind Iron to 127.0.0.1 whilst that bug is unresolved.

Synesso
  • 37,610
  • 35
  • 136
  • 207