I created a program in Java using socket programming, which sends messages between two devices. It works great in LAN, but It doesn't work in WAN. I searched a lot and many said to enable port forwarding in my router. I have a doubt, that we use many applications in our computer which connect to the Internet. Various applications also communicate to the server, and none of them require any Port Forwarding . So why this java program requires something like port forwarding to work?
Asked
Active
Viewed 682 times
0
-
2*...Various applications also communicate to the server...* You need port forwarding if *your* program is acting a server, i.e. listening for a connection on a specific port. – President James K. Polk May 22 '21 at 18:48
-
You not only need port forwarding, but also an address on the public internet. Your IP is likely dynamic so will change - you’ll need to set up a dynDNS provider and hope you’re not behind a carrier-grade NAT. – Boris the Spider May 22 '21 at 18:50
-
@PresidentJamesK.Polk yes, I have created Server.java to accept connections sent by Client.java. But can you just explain or provide me any resource that why actually a Server requires Port Forwarding to be enabled – Chinmay Pant May 22 '21 at 18:59
-
@BoristheSpider I have just created this small program for learning purpose, so I don't think dynamic DNS should cause problem for testing it. But the main problem was regarding port forwarding , and understanding the REASON why actually it is required, and why other applications dont – Chinmay Pant May 22 '21 at 19:03
-
A server needs to receive connections - it needs a port forward at each NAT on the route. Your router is the receptionist in a block of flats - if you don’t tell it you’re expecting post it will decline to accept any packages. Regarding not needs dynDNS - what do you plan to enter in the “destination address” of the client? – Boris the Spider May 22 '21 at 19:05
-
@BoristheSpider I made very simple program ,and just provided my Public IP address and port number in my Client.java file and gave port to listen on for Server.java – Chinmay Pant May 22 '21 at 19:10
-
Okay, so you should see the problem. Your router runs a NAT. Your computer’s IP will be an RFC1918 address. Your router will receive a connection on a port - without telling the router that connections on that port need to go to an internal location, the router will think someone is trying to talk to it - and decline the connection. – Boris the Spider May 22 '21 at 19:14
-
@BoristheSpider Okay that is much clear now , but one doubt that why this same condition does not come when we use other applications like spotify or youtube app on our pc , they also communicate to the internet – Chinmay Pant May 22 '21 at 19:21
-
You run YouTube? Do people connect to your computer to watch YouTube? Or do you browse to YouTube _servers_? Can you see the difference? **No one is connecting to you** – Boris the Spider May 22 '21 at 19:27
-
@BoristheSpider , now it is almost clear. Thanks a lot for your kind help. – Chinmay Pant May 22 '21 at 19:28
1 Answers
0
I also came across the same question. The reason is that sockets are default to local host and there is no way you can use that by simply putting the ip of the server into the client when the server is hosted on a local network, to put the server onto the internet we use port forwarding . Port forwarding allows to make a static ip for our computer and allows other computer to easily connect to the server and use it , other wise the ip will dynamically change because with out port forwarding the ip will be dynamic not static.. Hope this solves your problem...
-
1As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 02 '22 at 05:48
-
Yah, But is there a another way without port forwarding maybe like AWS Servers??? – Tejas Bishnoi Jul 08 '22 at 07:10