2

Java Socket Program did not work for WAN

I have written a TCP IP socket program which works fine in my LAN. One of my friend is at bangalore He ran the server and I ran the client with the host name of my friend's IP. In this case my socket program did not work.

5
  • You may want to post the code, we cannot troubleshoot what we cannot see. But it's likely that you are behind a router, and will need to forward the ports your program uses to your computer. Commented Mar 31, 2009 at 4:56
  • Thanks John T, How can I detect that my friend is behind firewall or NAT. If this is the case so how to overcome from this issue. My friend told me he is not behind firewall. int port_number=46216; String host="192.168.1.107"; Socket clientSocket = new Socket(host, port_number); Commented Mar 31, 2009 at 5:05
  • Thanks John T, I need your help to write a socket program to overcome all this type of issue. Commented Mar 31, 2009 at 5:10
  • The host "192.168.1.107" is behind a firewall. You must open up in your router or firewall. If it supports upnp you can use that to do that programatically. Commented Apr 24, 2010 at 12:01
  • stackoverflow.com/questions/17761759/… Commented Jul 22, 2013 at 13:12

5 Answers 5

7

You said that your program is attempting to connect to host 192.168.1.107 port 46216.

192 prefix specifies it is a class C address and is private. Making your program connect to that will force it to remain on the local network searching for that node. You will need to find the IP address of your router (you can use http://whatismyip.org/ to find this out). Then go into your router settings and forward port 46216 to 192.168.1.107 (your node), or even better, your MAC address which is not subject to change (in case your router is running DHCP).

on a side note, it isn't good to hardcode IP addresses. Simply use a textfield to avoid having to redistribute the client when your IP is changed, as it is likely you have a dynamic IP from your ISP.

Sign up to request clarification or add additional context in comments.

Comments

2

Your friend running the server is most likely behind either a firewall or NAT. Make sure you are using the external IP address and if necessary port forwarding the packets to the correct IP.

1 Comment

Thanks Jeff T, How can I detect that my friend is behind firewall or NAT. If this is the case so how to overcome from this issue. My friend told me he is not behind firewall. How to write socket programming in java to overcome these issue.
1

The IP address you gave seems to be a local address, rather than a public internet address. If you are looking for 192.x.x.x, you will not make it out to "the internet", but will be confined behind your router.

WhatIsMyIP is a good way of getting a public IP address, and THAT is the address you should use in your connection. Also, be sure to forward any ports that will be used by your program, because otherwise your router will likely filter the traffic and still create an issue.

Comments

0

You could use an implementation of STUNT or other NAT Traversal protocol.

2 Comments

Thanks Macbirdie, How to use STUNT or NAT Traversal protocol for TCP IP socket programming in java.
You need to learn the basic principle behind those protocols and apply it in your applications, either by using an off-the-shelf library or creating your own code.
0

The ip of computer on thih u deployed your server program is not in your reach. 192.x.x.x ip means (class C) it is for local subnet. You need to have change your ip address of your net-adapter so that your router could route it through internet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.