0

Possible Duplicate:
Java sockets - java.net.ConnectException: Connection refused: connect

I've created a simple chat program which communicates using sockets. Everything works fine when I'm running it on localhost. However, the problems occur when I try to link the client and server programs using my IP.

http://www.canyouseeme.org/ can connect to my server on port 9999 so I know that the server is fine and the port is open. However, my client can't connect.

The error log...

java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Client.connect(Client.java:129)
at Client.main(Client.java:47)

Does anybody have any idea what might be causing this? Thanks in advance.

Links to the full source code:

http://pastebin.com/2XftHtn9

Community
  • 1
  • 1
user1248420
  • 57
  • 1
  • 2
  • 5
  • Are they on the same subnet? Are there any network appliances that may be firewalling that port? How about the local firewall on the server? Or TCP Wrappers? Can you telnet to the port from your localhost or is it just the application? – nsfyn55 Mar 04 '12 at 17:52
  • Please don't repost questions. – Brian Roach Mar 04 '12 at 17:58

1 Answers1

2

Are you trying to connect to your own server using its public IP address from inside your LAN? For most SoHo routers, port forwarding only works WAN-to-LAN, not LAN-to-LAN. What you're looking for is called "hairpin NAT", and many SoHo routers just don't do it. To reach your server from inside your LAN, use its inside IP address, not its public IP address.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Thanks, I'll send the client to a friend and see if they can connect. – user1248420 Mar 04 '12 at 17:55
  • You were right! Thank you such much :D – user1248420 Mar 04 '12 at 18:01
  • This is the issue. Since the server is binding to `0.0.0.0` (because no IP address is specified in the server code) you just need to connect to `localhost` (or the machine's IP address on the local private network) if you're running the client on the local machine. – Brian Roach Mar 04 '12 at 18:01