Chat Application Can't run on different WIFI connection in java - java

I developed a chat application which is working great on same wifi connections on different machines.
Concept is.. One Server program is running on One machine which is set by ip and particular Port number so that client hit to server on a particular Port number.
There are two wifi connection running FCS and FCS1
My laptop is getting access to FCS wifi and if client interface is running on same wifi it's working; but when it connects to FCS1 the server doesn't get the IP info from the FCS1 network.
I'm not sure whether the problem is redirecting the IP on the router. Do I need to configure the router?

If the server and the client are running on two different networks without a valid IP address for the server, you need to use a VPN connection.
Or if you have control over the router, you could give a static IP address to the server and redirect any traffic on port -say- 7644 on router to server:7644.

Related

Java run ServerSocket on PC

A common problem for a common project: I'm trying to run a ServerSocket Java program on my local PC and connect to it from an Android client. As others have experienced, things go smoothly when the client connects to the local address (eg. 192.168.xxx.xxx). If I try to connect through the internet, nothing happens:
Now, here are the things I tried:
Created inbound/outbound rules in Windows Firewall to allow traffic for the Server Port (3000)
Used the public IP in the client socket (IP obtained here)
Set up port forwarding on my router: inbound connection on port 3000 are redirected to the local IP of the server (eg. 192.1686.xxx.xxx again)
Check that the IP address of the server is listening to the port 3000 using "netstat -anp tcp".
Disabled any firewall on my router, just in case.
Still, there is no connection. Using this tool (https://www.canyouseeme.org/) I triedd checking that the port was visible (with the server app running of course), but nothing, all I get is a time out.
I'm out of options, I was hoping someone could show me what I'm missing.
Thanks.
(1) Does your PC where your server runs on have a public IP?
If you haven't applied for one and are running your code in a home or working environment, your computer only has a private IP. And considering the fact, your server and client connect well in the same LAN, the problem may be the IP.
(2)If you do have a public Ip, does your server listen on all IP address or just the LAN? Check the server bind IP is '0.0.0.0', and in ServerSocket constructor you can just set it to be null.

Socket TIMEOUT when phone is on 3G, and works when on Wifi- Why?

I'm programming a client-server app, my client being an android phone, my server being my laptop.
So my issue is that this one line of code:
Socket connectionToServerSocket = new Socket(hostName, portNumber);
works perfectly fine when my cellular phone connects to the Internet with my home wifi connection, and simply times out when connected through 3G (cellular provider) eg it blocks for a while then throws a timeout exception.
The funky thing is,that I can see (using OS Monitor) that some apps are connected through very common ports, for example port #80, but switching portNumber (as well as the port that the server is listening to) to 80 doesn't help (eg it still times out), and I've tried many different ports-same result.
DNS works fine (eg it translates the logical String which I gave hostName to the correct IP) but it doesn't send the server anything...
I'm lost,what could be the reason? How can I check and resolve it?
I've run into this issue as well doing a similar application.
Your laptop and phone can connect to each other while on the same network because they share a IP address lookup through your router.
When the device is connected to the WIFI, it's request get passed through the router to check for IP addresses, it will find your laptops IP and save a request to a DNS because it can find the laptops IP already. The laptop works the same way, it finds the Ip address of the client through the router as well.
However, when your phone is on 3G, it has no way of knowing exactly where your laptop's IP address is. That's why it times out: it goes from your router to your nearest DNS (where it tries to resolve the correct IP Address), if it cannot find a domain or IP that matches it will fail.
Some steps to fix this . . .
Depending on your router you can set up port forwarding for your laptop's IP. This means incoming requests to your router can be piped to your laptop's server implementation.
Then go to any site like this http://touch.whatsmyip.org/ on your laptop to get your laptops IP. Save this to add to your clients Socket set up.
For debugging until your laptop server is visible for DNS lookup, go into your client code and add this.
Socket debugSocket = new Socket("the.laptop.ip", 80);
Some warnings:
Depending on your Router, your ip may change during restarts
With port-forwarding any browser with your ip, ex) 178.12.434.01 can log onto your laptops personal server
Future Changes:
Once a dedicated server is up and running, registered with a domain you can change the above ip parameter to "your.domain.com", and behind the scenes the actual IP address to your server will be found via DNS lookup.
the reason for that is that the server in your laptop is closed to external network by default, what you need to do is something called port forwarding
*take note: port forwarding put your server in a cyber security risk, make sure you make the right adjustments to keep your server safe.
case 1: It is working when your laptop and your phone is connected to wifi right ?
Try this once
case 2 :
connect your phone to 3G.
enable hotspot on phone.
connect your laptop to your phone's hotspot.
check the IP of your laptop if it is changed replace that in Socket object. Socket connectionToServerSocket = new Socket(newIP, portNumber);
Run your project.
Just try this once you will get what i am trying to say.
You are getting the timeout exception because your server that is
having the service is not reachable from the external network.
Hope this will help :)

Getting Connectiontimedout error when creating a remote SocketServer in Java

In the socket programming i am able to connect to the server socket when it is on the same pc i.e 127.0.0.1 but when my friend at a remote location runs the server program and i try to connect to it it shows the Connectiontimedout Error.
I'm giving the ip address and port number right.
Do i need to add something extra?
In order to access server remotely, your friend should bind the server to an IP address which is accessible from your machine. This will not be the case if your friend's ISP or wifi router has allocated a private IP address to him.
In such case both of you can join a Virtual Private Network to be on the same network.
Another option is port forwarding. If both of you can access a common machine then your friend can forward a port from the common machine to the application server's port to his machine. Now you can access your friends application server by accessing the socket at forwarded port on common machine.
If both of you are already on the same network then it might be possible that the server is listening on 127.0.0.1 interface only.
There are possibly other middle-boxes that do NAT (Network Address Translation) in the path between you and your friend. These normally prevent the initiation of TCP or other connections over the Internet.
Try doing the same with both of you on the same LAN (Local Area Network) or with a Hamachi VPN to simulate a LAN over the Internet.
Another possibility is configuring your router/NAT at your location to forward the port for your application to the IP address of your machine. In this case make sure to give your friend your public IP (you can get that with http://checkip.dyndns.org/).

Connect via TCP and public IP without forwarding

I'm trying to write a simple chat program using TCP in java.
To connect to a server I need to know its IP address. I'm connected to a router in my network, that connects me to the Internet.
When I type local IP (assigned by router) it works pretty well between my two computers.
But when I typed public IP (I got it from google: "what is your ip"...) - it didn't work.
So I opened router's setup and make forwarding rule from my public IP and specific port to the local IP - and it worked.
--------- Question:
But how to do this without forwarding? I want to write a chat program. I can't tell my users: "just do the port forwarding" ;)
One opens my program and logs into central server, then writes there public IP. Some other user opens this server, download the file and gets first user's IP (or any other needed info).
But if first user didn't do the port forwarding, it won't work. How to make it work?
For this you will need to understand how something called NAT (network address translation) works. In simple terms the NAT is responsible for sending packets to the right computer on the internal network from the external.
Say for example you have computer A as a server on the internal network, and have computer B on the external. If you then try to connect to computer A from computer B, it will not work because NAT (your router) doesent know what computer to send that packet of data to, on the internal network.
Its diffrent when you want to connect to an external server. Lets say computer A (client) is on the external network, and computer B (server) is on the internal network with the router port forwarded to its IP-address. Then you will be able to connect to the server because NAT knows where to send the data packets.
So to keep all your users from port forwarding their routers:
Port forward your server on your internal network
Connect the clients on another network
For clients on the internal network; use the local IP of the server
Hope this helped!
-Kad

Android - Java TCP connection NAT traversal

I am trying to set up a client/server model with android clients and a Java server.
The connection is over TCP.
I have all of my code working when the android device is on WIFI, however whenever I connect to a wireless network (eg 3g, 4g) the sockets refuse to connect.
I've been reading about NAT traversal but i'm not sure where to begin when implementing the necessary steps to allow the connection.
NOTE: my server has a static IP address
My guess is you have a network related problem. Assuming your server is behind a router, your server socket has to bind into its LAN IP (not directly binding the public internet IP). Then you need to setup port forwarding, firewall rules etc to ensure everyone on the internet can see your server (checking with online port open checker / telnet from another network is a good test)

Categories