Connecting from android client to server - android

I am trying to send simple text from android as client to a server app in java . The server listens to a specific port (say 3363) and the android client app successfully sends data from the test server ip : 10.0.2.2 to any server port (say in this case 3363) .
But when I try to make it listen to any other IP , it just does not go through. If someone can please help me with this .

Possible cause :
1) In the first case, your device and server both are connected to same network, e.g, using same Wi-Fi network, hence working. Later when you change the server IP address, the firewall of network of new IP address in blocking the packets.

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.

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.

Android Client App with 3G and WIFI Network

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.

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 .

need to get IP address for client server in Android, Is there a better way?

I used sockets to connect two Android tablets as a network over wifi using the same router. one is a server and the other is a client.
the problems is that every time I want to connect the client and server tablets, I have to type in the server tablet's IP address in the client tablet. Is there a way to avoid this?
I have not figured out how to send the server IP address to the client tablet before they are networked.
I wish there was a way to network the two tablets without having to know the IP address of the server tablet.
One idea is to have the server tablet connect to a website written in php and have the website get the IP address and store it in a database. next i would have the client tablet get the IP address from that website. Then the client tablet having the server's IP will be abble to make the connection. All of this will avoid the user having to know the IP and typing it in every time they want to connect.
I am sure that others have done something like this before and solved this problem. Any ideas?
I had this kind of android app. What I did is by using a Multicasting network. See an example here
Since Multicast Network uses a group IP, here's my suggested idea
The server tablet will act as a receiver
The clients will be a sender (They will send their local IP to the receiver)
If the server Android cannot be assigned a fixed public IP address, which means it gets LAN IP using DHCP, it can ask for specific IP address from the DHCP server (Maybe you need some configuration for the router), which is the router here.
Another method is using Reverse Address Resolution Protocol (RARP), with which you can ask the switch for the IP address of a host with a specific MAC address. The MAC address is fixed for your server Android phone.
These are just theoretical feasible. You need to look into the APIs to call in Android phones.
After the connection, you can retrieve the socket address
using the new client
( Socket client = server.accept(); )
from
client.getLocalAddress().toString() //ip server
client.getRemoteSocketAddress().toString() //ip client
and store them in a HashMap list, for example:
String name=client.toString();
name=name.substring(name.indexOf("/")+1,name.indexOf(","));
List<String> index = new ArrayList<String>(list.keySet());
if(index.indexOf(name)<0)
list.put(name,client.getRemoteSocketAddress().toString());

Categories

Resources