Socket communication in Android is not working - android

I have a client application which is currently able to communicate to the server from android emulator.
It does not work if I run it from my android phone. What could be the possible issue..??
Note: Its a socket communication and I am using AsyncTask for the same and ensuring that I am not running in the main thread.
IP address and the port numbers are still the same.

If you use socket communication by using your mobile , you should check whether your server ip is public ip that you can access in anywhere or not. unless ip address network is different between server and client (your mobile), It would run very well~

Related

How to get the IP of hostname in a local Wi-Fi network? Android studio (JAVA)

What I am trying to do is make an Android App for my NodeMCU so that I can send data via Websockets and not use the browser. I don't want the end user to be inputting any IP addresses to look for the NodeMCU in the network. I understand that you can set a hostname for NodeMCU.
Is there any way I can get the IP of the NodeMCU in the network using its hostname and then further communicate over it?
You can find ip of server without using its hostname.
Both devices run in the same -wifi- network.
The Android client knows its ip. Say 192.168.2.12.
Now it knows that the ip of the server will be like 192.168.2.##.
Just make a loop where you use a Socket that tries to connect using ip's around the one of the client.
Set a socket time out of two seconds or less.

Can an Androip app on Chromebook create a socket TCP server, listen connections and connect to other socket server on LAN network?

I have an android app that creates a socket TCP server to listen connections from a windows app. And sometimes it connects to the socket server on windows app.
It works properly on android devices. It can also receive broadcast packets.
However, it does not work when I install on Chromebook.
It cannot listen any connection, receive any packets or connect to the server on windows app.
Could anyone help me explain why and guide me some solutions to fix?
Best regards.
I understood about network in Chromebook and found Network Service Discovery as the solution here https://developer.android.com/topic/arc
Check for networking requirements
Chromebooks run the entire Android OS in a container, similar to Docker or LXC. This means that Android will not have direct access to the system's LAN interface. Instead, IPv4 traffic will pass through an internal layer of network address translation (NAT), and IPv6 unicast traffic will be routed through an extra hop. Outbound unicast connections from an Android app to the internet should mostly work as-is; but in general, inbound connections are blocked. Multicast or broadcast packets from Android will not be forwarded to the LAN through the firewall.
As a special exception to the multicast restriction, Chrome OS runs a service that forwards mDNS traffic between Android and the LAN interface, so the standard Network Service Discovery APIs are the recommended way to discover other devices on the LAN segment. After finding a device on the LAN, an Android app can use standard TCP or UDP unicast sockets to communicate with it.
IPv4 connections originating from Android will use the Chrome OS host's IPv4 address. Internally, the Android app will see a private IPv4 address assigned to the network interface. IPv6 connections originating from Android will use a different address from the Chrome OS host, as the Android container will have a dedicated public IPv6 address.

Server on Android reachable from devices not on local network?

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.

ActionScript 3.0 How to properly connect? "localhost" or "192.168.XXX.XXX"?

I am struggling with this for three days now. It is an android client application that connects to the server AIR application through Wifi on local network.
I need my application to connect to the server every time it is launched without asking the user to enter local IP, in case it changed.
It seems there are many ways to make sure the connection is successful, but I want to make sure I can go without the help of RTMFP or PHP or SharedObject stuff.
I am able to get local IP using NetworkInterface and NetworkInfo ANE from Android client. Then I use the variable to
mySocket.connect(ipAddress, 8888);
But there is a problem:
1) When debugging on Android via Wifi
The detected IP 192.168.137.2
2) When debugging on Flash, on computer
The detected IP 192.168.137.3
So, the local IPs are not the same. So client fails to connect. Everything works perfect, if I manually set that IP, but I need a code that works, even if the IP on local network changed.
As I mentioned making a textInput field in case IP changed and ask the user manually enter IP is not an option.
My question is why using "localhost" as host parameter of socket.connect(host, port) does not work? If it did, there would be no need for detecting local IP at all. "localhost" works for me only if the client is running on computer, but not Android.
Is it the problem with Security Policy file? if so, I have no idea how to use that. I can't find any tutorial on that.
You have two devices
1. Android device running an app and acting as a client
2. Computer running an AIR app and acting as a server
Each of those devices will have it's own IP address on the network. In your case android device IP is 192.168.137.2, and computer IP is 192.168.137.3.
In order for client to connect to server there must to be some means of delivering server IP to the client. Normally for an app like this you enter server IP address manually in settings, or have one central place available on the net where server registers it's IP address and client fetches the address from there.
Since you don't want to use any of these options the last resort would be implementing local area network scanning. Where you scan all the segment 192.168.137.* and search for opened port reserved for server. That of course is not good implementation but could work if project is for your own use.
Lastly "localhost" is name mapped to loopback IP 127.0.0.1 which is special address and is used to connect to the same device app is running on. In your case if android app is connecting to localhost it is trying to connect to the android device itself.

Eclipse socket connection between two emulator problems?

I am going to create a SOCKET connection between 2 EMULATOR, i have the below code but Nothing happens. The Server class returns me this Ip address:
10.0.2.15
and i am using this in my Client Class but nothing happens, i am not getting message at server
I am using the code for both server and client from this link Socket communication between two apps on Android
please help me, i have the same code as in the above link, and i am using 10.0.2.15 in Client to connect with server
Actually the Emulator have no ip address, it always contain this type of Ip address "10.0.2.15" which is in fact can't be access from Mobile phone or other devices, because it is an emulator. in contrast the emulator can send a connection request to other server like my Mobile whose ip address is "172.23.0.101", So in this case connection will be established between emulator(as client) and Phone(as Server).
In my project i have this ip address and it works well
Mobile Phone IP Add: 172.23.0.101 (SERVER)
Emulator IP Add: 10.0.2.15 (CLIENT)
Okkkkkk !!!, i solved my problem.I am sending messages from Emulator client to My mobile device where i have run the server program. My mobile device ip is
192.168.180.101
So, i am using this IP Address in client program and sending sms from client to server successfully.
The problem that you couldn't connect tow android emulator instances together via TCP is that they both run on a separate subnet.
Android documentations cover this topic in details and provide solutions for cases where you want to connect different emulator instances together, you can see it here: Android emulator Networking
Also there were couple of questions that are similer to your question:
here and here .

Categories

Resources