I have been trying to set up an app that allows communication between two devices, a server and a client. I have established the connection and can send messages between the two as long as they are connected to the same source of internet. When I connect the devices to two different sources, I am not able to establish a connection between the two. It works by entering the IP address of the server you want to connect to.
For example, if my server device is connected to my home Wi-Fi and my client device is connected to the same Wi-Fi router, communication works as supposed to. But, when I connect the server to Wi-Fi and my client to cellular data (or different Wi-Fi router), the client is not able to find the server.
From all the searching I've done, I found that it is because my server is bound to my local host. So my question is, How can I make my server reachable from anywhere regardless of what network you are connected to?
I used this tutorial to get my server running:
http://android-er.blogspot.com/2014/02/android-sercerclient-example-server.html
The very first comment is a question that asks why it only works on a local network and someone answered saying you have to set up port forwarding on your router. From what I understand, this will work only if the server device is always connected to the same router. This will not be the case for my app. The server will have a new IP address as the device changes networks. I want this IP address to always be reachable no matter what network the server is connected to.
You will need to create a relay server. This would be set up using a java application on the PC and it would manage virtual "rooms" that then relay the messages to the clients. The computer you run your relay server off of will need port forwarding but the clients will not. Much of the code from that tutorial could be applied to this concept.
See: How to create a java Server that accepts client connections and then build a relay connection for a client pair
If your server is behind a firewall, you will not be able to reach it unless that port is open on the firewall. The only way to avoid this is to have a non-firewalled server.
Related
I'm developing an application that need an internet permission all the time.
When there is a WIFI connection the application can connect to my server (if they're in the same network)
actually I have 2 question..
1) Lets say that the server is connected to network A and the client is connected to Network B (on
WIFI) why it doesn't show me on the server side the message that I send?
I made a simple project that when the client is connecting to the server it send "hello" to the
server, the server read it and tell me the address of the client and the message (print to the
console), it works fine when both server and client are at the same Network.. but when they're in
different networks it doesn't work.
2) How a 3G application is working? how can I connect the client who uses a 3G network to my server?
When you are on the same network then you give the the ip the ROUTER gave to your pc and phone (assuming that what your using) and it looks something like 192.168.0.101\2 (its your router Default address + 1 each time a device connects to the router) ...
The problem is that when you want to connnect from an outside source you must give the ip of your real address given by your ISP.
go to command\terminal and type ipconfig and look for IPv4 ------ thats your real ip when looking from the "outside" world , now thats not all you also need to configure your router to port forwad the ports your using to the specific PC that is the server.
How what and why : Port Forwarding
hope this helps.
I want to have my app able to connect to other devices over the same wifi network and transfer some data, that is phone A and phone B have my app installed and are on the same network. I am able to connect two devices currently with one acting as server and the other being client but this is with a static IP reference to the server device. In the open world this IP will change of course so my problem is how do I get phone A to start up as a server device then have phone B see the server device on the network and connect to it. So the server device would accept this client connection and carry out the needed actions when the connection is made. Is this even possible?
I believe you would have A device listening on port number x, and the client version (device B) looking for that same port number x. This could be wrong.
This is a job for DNS. If you don't have a DNS server available you could invent a discovery protocol in which machine A periodically multicasts the IP & port on which it is listening.
Machine B joins the multicast group. Once machine B knows A's address and port it can open a socket using the code you already have.
I have a HTTP server hosted on my android device. I want it to be able to access it from the internet even when it is connected on home wifi(behind a wireless router).
Further, configuring static port forwarding on router is not an option, since from the internet, I won't have access to the router configuration.
Do I have any option?
I tried this:
Android App sends a HTTP request from android device(with local IP address, say 192.168.0.1) from a particular port number(say 8080), to a web server on internet through home WIFI,
Router would modify the source address to that of its public IP address(say 111.100.45.65) and source port number to any random port number(say 39824) and makes an entry in its dynamic NAT table.
Internal IP Internal port External port
192.168.0.1 8080 39824
As per my understanding this entry is retained for atleast few hours.
Web server on the internet would report the source IP and port as 111.100.45.65 and 39824 respectively.
After sending the HTTP request from android app to web server on the internet, I run an HTTP server on port 8080 of the device. Now, if I were to send HTTP request from the internet to the address 111.100.45.65 at port 39824, it should be received by HTTP server running on device(router would resolve the request from its dynamic NAT table and direct it to my device).
But for some reason, this request doesn't get resolved. Any pointers.
Firstly regarding access to router configurations from the Internet, most routers do actually support logins from the WAN interface. It's turned off by default on most routers, but there is no reason why you can't enable this as long as your password is secure.
Secondly, what you tried would work if you setup a proxy on the web server on the Internet.
Punch a TCP hole by initiating connection from your Android device to the web server on the Internet.
Now the web server on the Internet knows the public IP and the destination port to be able to talk to the Android device.
An automated script can configure the proxy server on the web server on the Internet that will forward HTTP requests to the Android device.
By the way, I am doubtful that the NAT entry is retained for at least a few hours. You may wish to implement a keep-alive mechanism between your Android device and the web server on the Internet so that the TCP hole remains open.
No. Your app can communicate over port 39824 with the http server on the web. Nobody else can use that port to make contact from web to your http server on your device. You need portforwarding on your router for your device be reachable from internet.
Can i interact with a server(android device) from a client(android device) just to query a database located in the server using TCP sockets?
And Yes and No.
If your device is within wireless network and have own local IP. You can connect to it from client phone if it is in the same network just providing proper IP and Port.
If server phone is located in another wireless network, then Port have to be forwarder using router settings to the server port. Bear in mind that if you will reconnect to wireless network by phone, then IP will be automatically assigned by network infrastructure and it might be changed from previous.
If you want to connect to server phone when it is on 3G network (or similar non wifi), you wont have any specific IP, and it is not static, that way it will be almost impossible to create reliable network. It is more Peer-to-Peer model of network you are looking for.
It is much better if you use Server in the middle as Gateway, that way it will be reliable. The only thing is that you will have to host server and its processes with all traffic and so on.
I am implementing a mobile chat application, I am using socket connection for implementing p2p connectivity. My chat is working fine with android devices. My issue is I can connect a device in 3g network or with in the same WiFi network but the connection is not working when a device form outside WiFi try to connect a device in the WiFi network. I know it's because of the local IP of the device assigned by the WiFi. How can I root and connect a device in the WiFi?
Sounds more like a router settings issue, than an app issue (meaning that users of your app would need to do this for their own networks as well).
Your wifi router needs to know how to forward communication to your device. So say that your app is connecting on port 1234, then you need to tell your router to forward communication from the outside on port 1234 to the internal IP of your phone in your wifi network.
The phone that is outside of the network should target your network's external IP and port 1234.
Sajan, your issue is not only an IP mapping issue, but also a NAT traversal issue. It is not always possible to punch holes in NATs. And when it is, it must be done with some sophisticated techniques including a super peer located outside of your wifi/lan which will read the translated address from your inside peer.
Unless you are using UDP and the NAT is friendly, it is not possible to send TCP communication directly to the inside peer. In most case, you'll have to check what is possible with the NAT, and if there is something possible, you'll need to perform mapping prediction and tell both peer to attempt com on predicted IP addresses.
That's valid if you don't want you users to have to configure their NATs. And even in that case, such configuration will not always be enough (if there are cascades of NATs, or proxies for example).