Android, Java Socket - java

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
)

Related

Android run external Socket Server from any device

I want to run a socket server on android, and read/write data from/to other android devices with different networks.
Here is the problem:
When I'm trying to setup server with external IP, I get java.net.BindException: bind failed: EADDRNOTAVAIL (Cannot assign requested address)
When I setup server with IPv4 address - other android devices from different networks can't connect to it.
What should I do, to link two or more android devices with sockets from different networks?
The main point is run server on any of that devices (client-server code in one app).
You can't bind to an address you don't have.
You need (a) to bind to an interface on your android device, and (b) arrange port forwarding / open the firewall (depends on router+network config) from your router to your android device.
You do not have to bind your server socket to an ip address.
Just dont call bind().
Further your server is only reachable if it runs on an Android device connected with wifi to a router.
If the Android device is on mobile connection your server is not reachable. –

Can't connect device to localhost

I'm trying to connect my real android device to local server.
So at the beginning I connect all devices to 1 network via Wi-Fi.
Next step is copy IPv4 from ipconfig.
Now I'm entering myIpv4:8080 into browser in my android Device and I get ERR_CONNECTION_TIMED_OUT.
I forgot something ? What can be wrong ?
I should get JSON object like in PC browser.
can you check your web server is listening on a 0.0.0.0 or just localhost?
Localhost is a loop back IP for every machine. If a webserver is listening to Localhost interface, it only accepts network request from the machine it runs on. Your Android request may be ignored.

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.

Chat Application Can't run on different WIFI connection in 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.

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?

Categories