Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

new Socket(host,port) does not throw ConnectException: Connection refused ?  RSS feed

 
David Balažic
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I encountered a puzzling mystery. I an project I work, we have the following code:



Once I run the corresponding server on another port and noticed, that the above code line does not fail. On a coworkers PC it throws this exception:




The code is run from inside Eclipse Java EE v3.5.2 as a JUnit test.

If I change the JRE used for execution of the test from default JRE ( C:\Program Files\Java\jre6 ) to jdk1.6.0_20 ( C:\Program Files\Java\jdk1.6.0_20 ), then it works correctly (throws the exception).

OS is Windows XP Pro SP3, JRE is Suns Java package 1.6u20 from java.com and JDK is Suns JDK 1.6.0 update20.

Any idea what is happening?

Regards,
David
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since it tries to connect to the specified port on "localhost", if the exception does not occur, it means that the specified port is open on "localhost".
Did you check whether the port is open or not?
 
David Balažic
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The port is closed. 100%.

As I said, if I run the same program using JDK, then it works as expected (throws the exception).
 
David Balažic
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is getting weirder...

I wrote and compiled the below program. I have nothing listening on port 3300.

I started it from cygwin:

$ /cygdrive/c/Program\ Files/Java/jdk1.6.0_20/bin/java SocTest
START
CONNECTED 1
CONNECTED 2
CONNECTED 3
STOP

$ /cygdrive/c/Program\ Files/Java/jdk1.6.0_20/jre/bin/java SocTest
START
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at SocTest.main(SocTest.java:13)
Got an java.net.ConnectException: Connection refused: connect
STOP

$ /cygdrive/c/Program\ Files/Java/jre6/bin/java SocTest
START
CONNECTED 1
CONNECTED 2
CONNECTED 3
STOP


I'll also try on another PC.

 
David Balažic
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runing the same class file on another PC:

C:\cygwin\home\stein\x>"\Program Files\Java\jre6\bin\java" SocTest
START
CONNECTED 1
CONNECTED 2
CONNECTED 3
STOP

C:\cygwin\home\stein\x>"\Program Files\Java\jdk1.6.0_16\bin\java" SocTest
START
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:525)
at java.net.Socket.connect(Socket.java:475)
at java.net.Socket.<init>(Socket.java:372)
at java.net.Socket.<init>(Socket.java:186)
at SocTest.main(SocTest.java:13)
Got an java.net.ConnectException: Connection refused: connect
STOP

C:\cygwin\home\stein\x>"\Program Files\Java\jdk1.6.0_16\jre\bin\java" SocTest
START
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:525)
at java.net.Socket.connect(Socket.java:475)
at java.net.Socket.<init>(Socket.java:372)
at java.net.Socket.<init>(Socket.java:186)
at SocTest.main(SocTest.java:13)
Got an java.net.ConnectException: Connection refused: connect
STOP


The JRE6 is :
java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing)

OS is again windows XP Pro SP3
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!