bahti
bahti

Reputation: 636

java tcp connection with public i.p

I am trying to tcp connect to a server machine in java, by using its public i.p. but when i run the client application i constantly getting a connection refused error. if i used localhost instead of the public ip, it works perfectly.

i search the internet for several causes of the issue but i couldnt fix it.

i forwarded the port to my machines' local i.p address(192.168.1.3) in routers settings. then i checked if port is listening when i ran the server application using netstat -an. i saw lines like, 0.0.0.0:19999 or []:19999 . 19999 is the port number i am trying to listen to. Then i changed my ServerSocket constructor to the 3 parameter one, which also binds the local address.

InetAddress miad = InetAddress.getByAddress(addr);
ServerSocket socket1 = new ServerSocket(port,10,miad);

addr is the InetAddress of my machines local i.p. After these modifications, when i start the server application, i run netstat and it shows:

TCP   192.168.1.3:19999    0.0.0.0 LISTENING

Here i think that my server is listening on the port specified properly.

I have also disabled my firewall and antivirus software.

I have seen several threads and discussions on the net about the issue, and tried most of the things mentioned there, but i keep getting the connection refused error.

What can i be doing wrong? It runs without any errors when i test with localhost.

Upvotes: 0

Views: 1641

Answers (2)

Oleg Mikheev
Oleg Mikheev

Reputation: 17444

To figure out if your problem relates to Java and programming please do

telnet 192.168.1.3 19999

If it can't connect then superuser.com would be a better place to discuss this issue.

Upvotes: 0

Martijn Courteaux
Martijn Courteaux

Reputation: 68847

This is because of the router (not very sure, but almost). Try to see if a webservice like www.canyouseeme.org can connect to your server.

The main idea is that an internal machine (inside the LAN) cannot connect to a machine inside the same LAN by using the external (public) IP address.

I'm pretty sure that it will work, using you internal ip (192.168.1.3).

And if you are sure that you forwarded ports correctly, CanYouSeeMe will say your server is reachable. If it doesn't, make sure you ISP isn't blocking the ports for some kind of "safety reasons".

Upvotes: 3

Related Questions