Android Question Bi-directional communication over internet

PhilipK

Member
Licensed User
Longtime User
Hi B4A

I looking to connect one Android device to another over the internet, to retrieve small amounts of data.

Is there a (working) code example that you can point me to which I can try?

I assume that one Android device acts as a client and requires client side code, connecting to the others IP-Address..port. The other device, a server that requires server side code.

All help much appreciated.
 
Last edited:

walterf25

Expert
Licensed User
Longtime User
Hi B4A

I looking to connect one Android device to another over the internet, to retrieve small amounts of data.

Is there a (working) code example that you can point me to which I can try?

I assume that one Android device acts as a client and requires client side code, connecting to the others IP-Address..port. The other device, a server that requires server side code.

All help much appreciated.
Not sure if this would help, but do a search for Wifi-Direct.
 
Upvote 0

PhilipK

Member
Licensed User
Longtime User
Hi Walterf25

Thanks, I have successfully implemented WiFi Direct which is great when both devices are within WiFi range i.e. say 30m. I'm looking to have the same one-to-one capability but at any distance.

I've set up one device as a 'server' with ServerSocket1.listen on port 2222 and found the external IP, by 'what's my IP?': 85.2xx.xxx.xxx OK.
The server android device is on mobile WiFi

The other device opens a Socket ("85.2xx.xxx.xxx",2222,20000) and tries to connect but fails.
This android device is on a local LAN. I have configured the router with port forward and 'direct connection' and set it to DMZ.

Any ideas?
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
If one device is in your house and the other is with you (not in your house) then it should be a simple enough task.

1. Device in house MUST have a static IP address
2. Apps must have an open port number (for example port 888)
3. You MUST know your public IP address (this does not have to be static). You can write a Java app using B4J that runs in the background and email you when your public IP address changes, you can also create an app on your Android device(in the house) that does the same thing.
4. Port Forwarding - Your router MUST have the internet incoming port forwarded to the static IP address of your Android device in your house.

Plan of action:
First make sure that both your devices have static IP address.
Second create an app that works and does exactly what you want internally in your house.
Third Port forward your internet router to the IP address of the device in your house, for example port 888 forward to 192.168.0.10.
Forth when you are outside point your app on your phone to your Public IP address and the router in your house will route the data to your device inside your house. this is only a one way gig though...

That's basically it in a nut shell...
 
Last edited:
Upvote 0

PhilipK

Member
Licensed User
Longtime User
Thanks Peter, useful info.

I have kept it simple and don't understand why it isn't working:

The home device (client) is trying to connect with the server which is a mobile phone connected by mobile wifi.

For the purposes of the trials, although the IPs are dynamic, they are static for the duration and I check them.

I have port forwarded my home device (Static 192.168.1.74) by opening and linking the TCP port range 2000-2500 on the router to the static IP. I'm using port 2222. I cannot believe that the server will not be able to connect to my home device.

Before starting the other mobile device which is on mobile wifi, I open a browser on it and grab the external IP: 85.234.xxx.xxx which I use on the client app.

I check that the server app is listening on port 2222

Then I get the client app to try to connect to the server and.....nothing.

It's extraordinary. I did this stuff with VB6 years ago and didn't get this stoppage!!
 
Upvote 0

DavideV

Active Member
Licensed User
Longtime User
Hi,
have you seen this:
https://www.b4x.com/android/forum/threads/embed-an-http-server-in-your-android-application.25984/

I'm using a more elaborate version of this example on an old smartphone at home and can reach it from everywhere.
I use it as my personal webserver.

Obviously you need to set up correctly your router to get it to work.
My D-link router need the ip address and the net hostname of the device used as server for a correct port forwarding:
read here
http://stackoverflow.com/questions/21898456/get-android-wifi-net-hostname-from-code
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
Start simple. You do not need 500 ports open in your port forwarding.
Set it to one port in the router example 2222 external to 2222 internal.

Also with the server running on your computer try to port check it 2222 with the following website.
http://www.canyouseeme.org/ to see if the server is answering. ( it will also give you your current WAN IP)

If you get Success you are half way there.

Next (double check its the server answering by doing this)

Turn the server off and try the port check again if it fails(Error) then you know your server was answering before.

:) Hopefully you are using a static IP as Peter Simpson mentioned. If not you will be chasing your WAN IP every time your ISP changes.
 
Upvote 0

PhilipK

Member
Licensed User
Longtime User
Thanks for the help. Great response.

The port checking tool is very useful indeed: http://www.canyouseeme.org/...

I installed the socketserver on the device at home so that it would listen for connections. The port checking tool connected immediately with the device at home proving that the port forwarding, DMZ, Firewall are OK. BTW, on the router, I reduced the number of open forwarded TCP ports to one, as suggested!

Moving on, the mobile phone has the same socket server code with the same port open. It is on mobile Wifi only.

ISSUE FOUND: The port checking tool cannot connect to socket on the mobile phone.

Are there any things that need to be done on the phone which might be blocking the port? System settings I've missed? Firewall?

Now I know where to look...the battle continues...
 
Upvote 0

PhilipK

Member
Licensed User
Longtime User
Reported by tool: "Error: I could not see your service on 85.xxx.xxx.xxx on port (2222) Reason: Connection timed out"

The socket server is running in a service.startforeground and there is a timer pinging a click so that I know the service is active.
 
Upvote 0
Top