Send data to Android from Java EE - java

I am trying to send data over TCP/IP from a Java EE application to an Android app. In Android I have the following in a AsyncTask:
try {
ServerSocket serverSocket = new ServerSocket(48760);
while (true) {
Socket socket = serverSocket.accept();
// Do something interesting with sent data
}
} catch (IOException e) {
e.printStackTrace();
}
I have verified that the app executes until it blocks at the accept() call, as expected.
on the server side (Java EE) I use:
// address contains the IP of the android device as a string.
InetAddress addr = InetAddress.getByName(address);
Socket conn = new Socket(addr, 48760);
Here I get "java.net.ConnectException: Connection timed out: connect" when the application reaches "new Socket()". Any ideas as to why this is?
I have in my manifest. And the ip in "address" seems to be the correct one (the ip provided by my service provider).
Is there a better way of sending messages? I was planing on using Serializable objects (from Android to JavaEE I use HTTP, through a Servlet) but I am open for suggestions.

Okay there is a problem with your implement !
Why you need to connect from server to client (who cares but client is server now !) !?
You must fix this problem !
And lets show you what is your problem !
If your server (JAVA EE Server) is on public internet so it means your server has a public IP address . This public IP address is accessible from all devices that connected to internet ! You can control all ports of server and so on ... , so every one can connect to your web application in public Internet.
But in client side somethings are difference !
When you connect to internet in client side this will happen :
1 - Your device connect to router (it can be a modem in your home or mobile service)
2 - Your router has an IP address in IPS network and ISP network translate it (using NAT) to a possible public IP
3 - now your device(s) has the same IP as that IP assigned to your router in ISP
Your problem is in section 2 ! because most of ISP(s) share a public IP between clients so it means when your assigned IP is ADDR_1 there is another clients that has the same IP ADDR_1 and you cant control all ports on it . Maybe when you listening on port 8080 in your local network the ISP assign another port for your public IP (Regardless of NAT will block your listening ports in most of ISP(s))
So every clients doesn't have unique IP and too hard to find them in public internet (it will be harder when new NAT(s) [Symmetric-NAT] exists on ISP)
There is several ways to handle this problem , but there isn't any way to work 100% !

Related

How can I connect a client to a server in Java using Socket

I am trying to write a client-server app that connects a PC as a client to my PC as a server.
When I enter 127.0.0.1 as server IP in client side, in my PC, it works properly so it's not a coding problem. Also when I enter my IP (got it from nslookup command in kali) and connect to internet, client connects to server properly.
But when I open my client app in other PC and server app in my PC, the client side a "Connection Time out" Exception will be thrown.
I have tried turning off the firewall in the client side (Windows 10) but not from the server side.
Here are my codes:
Server:
ServerSocket server = new ServerSocket(SERVER_PORT);
Socket client = server.accept();
//Some codes
Client:
Socket server = new Socket(SERVER_IP, SERVER_PORT);
For better answer we need more informations from you. But from this what you post, this is best answer i can make. You didn't explain what is your goal, do you want to make connection inside local network or you would like to make connection to 'outside world', over internet.
If you like to make two PCs communicate inside local network, than you must make sure they are on same network, or more accurate, that they are connected to same router. You have to set your server IP address to IP address on your local network, that would probably be something like 192.168.xxx.xxx
If you want to communicate over internet, i suggest test in local network first, as described before. If it works on local network, then you have to deal with firewalls, router setting,etc to make it work on the internet too.
Take look at this too
Use Socket-communication over different networks

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 :)

Why can not I create tcp socket from Android device to PC

I'm trying to connect my Android app to desktop server. The problem is when I try to create LAN via portable Wi-Fi hotspot on my smartphone everything works fine, but when I connect PC and phone to the router I have TimeOutException creating a socket in Android app. On the other hand, when I connect desktop client to desktop server via router it works fine too, so the problem occures only when I try to connect mobile client to desktop server via router.
Client code (Java):
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), 1000); // TimeOutException
Server code (C++/Qt):
QTcpServer m_tcp_server;
// ...
m_tcp_server->listen(QHostAddress::Any, m_port);
ip address and port are right (100%)
UPD:
Thank you, guys.
It was just a router problem. I tested in on the another one and there is no problem
try check which ip you have assigned on android pohone and if you have route to the destination server ip.
Is possilbe you get different ip on wifi and different ip by cable.
Also you can try an android net tool to try connect to the server.
I had same problem, In my case i created server socket with wrong ip address.
Hotspot network band is 192.168.43.1~255
You need to create server socket with ip address that hotspot dhcp made like 192.168.43.11
I had wrongfully made server socket with ip 192.168.0.22, this is wrong.
And your android app should open client socket with it.

Java TCP/IP Server - Client Communication over the internet

I'd like to get a tcp/ip connection working over the internet. I already have two classes, one tcpserver which handles requests and a tcpclient which connects, sends and receives data.
Until now I had it working perfectly on local networks. The usual 127.0.0.1 / localhost and my 192.168.xxx.xxx adresses are not giving any problems.
My question is, what do I have to do to make it work over the internet. As I might want to do some research involving a simple http/server I need this to work.
I doubt its my firewall but maybe its the ISP that blocks this type of connection. Anybody has an idea?
You need to open a port in the router that leads to the one that the server is listening on. You then connect to your public IP. This ip can be found on http://www.visaminip.se/
To provide a service over the internet you need a public IP address. 10.x.x.x and 192.168.x.x are private networks. 127.x.x.x is for the local machine only.
The client can connect to the public IP address of a sepecific port.
The server's modem/router/firewalls allows this port to be open and passes traffic to your server on that port.

Categories