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

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.

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

Send data to Android from Java EE

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% !

Android - Cannot connect to external IP address over mobile 3G/4G network using sockets

Right now, I am writing a server/client program to allow a user to control different parts of their computer from an android phone on the fly. The server runs on the computer and the client runs from the android phone. I am using socket programming to do this, WinSocket on the computer and Android/Java sockets on the phone.
What Works:
The server on the computer end works.
When on the same wifi network, the client on the phone can send and
receive commands to/from the server.
The server is accessible from outside the local wireless network it is connected to. I
tested this by using this open port checking tool to send a request
to the server. The server can successfully receive the request, and
the port checking website reports that the port I am using is
properly forwarded.
When using an app on my android phone that tests open ports, my
server gets the connection request over 3G/4G.
Now for my problem. When I try to connect to the server running on the computer from the client on the phone, when the phone is connected to the internet through 3G or 4G, the socket does nothing and throws a socket timeout exception, and the server does not receive any connection request from the phone. I don't understand this because, like the 4th thing above, the port checking app I used can get through to my server program, but the code I'll post below doesn't do anything when running on 3G/4G.
String hostName = "SERVER_NETWORK_EXTERNAL_IP_HERE";
int port = 58587;
SocketAddress address = new java.net.InetSocketAddress(hostName, port);
sock = new Socket();
sock.connect(address, 5000);
The network activity indicator in the status bar at the top of the screen does not report outbound network activity when running this code, however if I am connected to wifi, everything works correctly. Anyone got any ideas?

socket connection issue between 2 carriers' network

I wrote a simple socket program on android, the server side sets up a server socket waiting for incoming connections, the client side just connect to the server by establish a connection with the server's ip address. I'm using Sprint's 3G Nexus S as the server, and ATT'S 4G Samsung galaxy S3 as client. When my client tries to connect, it throws out the exception"No route to destination". But then I switched to my personal WIFI network, it worked perfectly. Can someone help me with this issue? or is there any special requirement in android to use cellular network to set up socket connection? thank you!
My client side is:
socket = new Socket(serveripaddress, 8008);
out = new PrintWriter(socket.getOutputStream());
My server side is:
ss = new ServerSocket(8008);
update: I just used 2 Sprint cellphones to test my code and it works fine!! This is just a special case in which i have to use phone as server, it is not recommended in general
Carrier firewalls typically don't allow incoming connections to mobile devices. Doing so would leave the mobile devices open to security risks and abuse of their data limits. It can also depend on the APN used for the connection.

Android, Java Socket

I have created a TCP socket client in android and use a open src socket app which i use as a server to send messages to my android app, every thing works fine.
But the doubt is that both device are present using my home wifi connection hence having 192.x.x.x series ip,
But in case my device is connected to wifi router in my home having Ip of 192.x.x.x and server is having static Ip, will my server be able to send message to the remote device, irrespective of the fact there the device is having local network Ip ?
No, you won't be able to send data to the remote device unless and untill it initiates the connection , you cannot initiate a connection to a dynamic Ip(remote Device
)

Categories