could you please advice how to connect to development machine from device?
I can easily do this from Emulator, but from device I have connection timeout exception.
Here is my code:
String hostname = "10.0.2.2";
int port = 4444;
Socket socket = null;
try{
socket = new Socket(InetAddress.getByName(hostname), port);
} catch(UnknownHostException e) {
Log.e("Capturer","UnknownHostException : " + e.getMessage(), e);
} catch(IOException e) {
Log.e("Capturer", "IOException : " + e.getMessage(), e);
}
Once you are on your device, you must use the actual (external visible) ipaddress/hostname for your development machine. If you connect via wifi with your device you should be able to use an internal(to your network) network ip address, if you are on 3g or edge, you will need your external, publicly visible ip address, if you have a network this will only get your to your modem/router, and you will need to setup the correct port forwarding for this to work.
If you are just trying to send data from the device to the development machine for development purposes, you could also look into adb. In particular, check out adb forward. This would allow you to send data over the USB connection.
Related
I'm trying to create a node js server for android phones. How can I create it at home in a local network without outer internet connections? I have wifi at home so my phone can connect to local network. I use official socket.io tutorial and I don't know what to write here (instead of http://chat.socket.io):
private Socket mSocket;
{
try {
mSocket = IO.socket("http://chat.socket.io");
} catch (URISyntaxException e) {}
}
open command prompt on your PC and hit ipconfig. note down your wireless IP address. Let's assume it to be 192.168.1.10. you can also set a static IP for your PC on your router. Next from your socket io code check what port number you are using. for example
http.listen(3000, function(){
console.log('listening on *:3000');
says that you are using port 3000.
Hence in your Android code you need to use http://192.168.1.10:3000/
I am trying to connect 2 devices through sockets, so that they can exchange data. They are also connected via wifi hotspot. I am using Services.
Device 1 is the hotspot (where the ServerSocket is implemented), Device 2 is the one who connects to it (where the Socket is implemented).
I did some research and i am able to get the ip of each one of them (but calculated on their own class). But in order for me to create the client socket, i need the IP Address of the host (the phone that is working as a hotspot) in the other class. I can not get it on the server side, because that part of the code wont be executed, since i am using one phone to create the hotspot network and another one to connect to it.
I know that usually the IP Address of a device that is tethering is generally the same, but i can not trust that, because i gotta make sure it works on all phones.
So, how can i get the ip address of the server (hotspot host) in the client (phone connected to that hotspot) service ?
In the client side you can use dhcp.gateway to get the server side(The one who created hotspot) ip address.
private final WifiManager manager;
private final DhcpInfo dhcp;
private InetAddress getServerIP() {
manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
dhcp = manager.getDhcpInfo();
final String address = Formatter.formatIpAddress(dhcp.gateway);// gateway -
default gateway IP address
InetAddress serverIP = null;
try {
serverIP = InetAddress.getByName(address);
if(mDebug)
Log.i("Server IP ","" + serverIP.toString());
} catch (Exception e) {
if(mDebug)
Log.e("Cannot find server's IP. Error ","" + e.toString());
}
return serverIP ;
}
Determine the ip of the gateway. Programmatically getting the gateway and subnet mask details. Use the WifiManager.getDhcpInfo().gateway
.
So , the flow of the application is ,
first , I use my android phone to connect a device (which is a server) through wifi, then , I would like to send a string to that device, and that is all.
The problem is , when I try to send like this
new Thread(new Runnable() {
#Override
public void run() {
try {
Socket socket = new Socket("192.168.8.101",2001);
DataOutputStream DOS = new DataOutputStream(socket.getOutputStream());
DOS.writeUTF("61");
socket.close();
} catch (Exception e) {
final String error = e.getMessage();
runOnUiThread(new Runnable(){
#Override
public void run() {
Toast.makeText(mContext, error, Toast.LENGTH_SHORT).show();
}
});
Log.d("test1",e.getMessage());
e.printStackTrace();
}
}).start();
it return the failed to connect to /192.168.8.101 (port 2001) : connect failed EHOSTUNREACH (No route to host) in the exception
How to fix that , and by programming or using some way , are there any way to check whether the ip and port is correct ? Thanks
Make sure you turn off the firewalls on that port.
To make sure IP is up we usually ping that machine.
There is a tool called wireshark that taps the packets coming to your machine. So put you send function in while 1000 loop and pump messages. Turn wiresahrak at the server and see if you are able to get the packets there.
There are lots of other programs which can connects to sockets. take a sample java program and try to get connected to the other socket. If it does, your android stuff will also do.
Check your router's security configurations. I had the same problem, then I turned off the option "Client Isolation" in my router's Wireless configuration and it worked.
I've written a small file transfer program for android using standard Java sockets. The program works fine except for the following case:
I connect two android devices A and B over WiFi tethering connection. Device A is sharing the connection (enabled as wireless hotspot). When I run java server on A and client on B, the program works okay but when I run the server on device B, it can't accept any socket binding request from A. It doesn't throw any exception. Seems like the request is not reaching the server! However, both the devices are connected (ping test is okay in both directions). Can't I run socket server on a device connected as hotspot client? I thought once the networking is setup correctly, the application would work in any direction.
Also, Wireshark traces reveal nothing. What am I missing here? Please help! Here are my code snippets:
Server side (waiting for client connection):
while (true) {
try {
socket = serversocket.accept();
Runnable connectionHandler = new ConnectionHandler(
socket, fileArray, filepathNameArray,
SenderActivity.this, userID, handler);
new Thread(connectionHandler).start();
userID = userID + 1;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I would appreciate any kind of help! Thanks in advance.
I have few question regarding Socket Communication in Android.
1)I have developed server socket app for android that listens to port 8888.
a) When i host my server on the emulator I'm unable to communicate to it through Client application that I have on my PC since both (Emulator & Client) app are on my laptop & on the same network i think that they should be able to communicate with each other.
b) When i deploy the same server app on my android mobile device and try to communicate it through the same Client Application that I have on my PC, the client application gives a timeout exception as its unable to communicate to it.
My first question is How can i test server/client socket app with Emulator & 1 android device? Can i even use my PC's client socket application to test my server socket?
**I have Client Socket Application for my other application so theres no problem with the client application.
2) My second question is to test my server app on the android device do I have to forward the desired port?
a) For Emulator: How can I forward port?
b) For Device: How can i forward port?
c) Can i forward port programmatically?
**Just for Information:
I'm using Eclipse as android developement tool.
** MY Server Code as there can also be problem with my server socket code too.
Socket socket = null;
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
ServerSocket serverSocket = null;
try
{
serverSocket = new ServerSocket(SERVERPORT);
System.out.println("Listening :" + SERVERPORT);
System.out.println("Server IP:" + SERVERIP);
}
catch (Exception e)
{
e.printStackTrace();
}
while(true)
{
try
{
socket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("ip: " + socket.getInetAddress());
String str = in.readLine();
System.out.println("message Received: " + str);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if( socket!= null)
{
try
{
socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if( dataInputStream!= null)
{
try
{
dataInputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if( dataOutputStream!= null)
{
try
{
dataOutputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
Edited:
A very interesting part is that if i set port to 8080 PC's Client Socket App do connect to Android App on my android device but i don't receive socket on my server nor the data I send. Nothing happens after the link => socket = serversocket.accept();
Also I have set the permission in the manifest.
have you set the right permission inside your Manifest.xml?
<uses-permission android:name="android.permission.INTERNET" />
is needet to setup sockets
For your first question, on how to access the network from the emulator: the emulator runs on its own network address space, isolated from your PC. You have to configure network redirection to access devices on your network.
See more details in https://developer.android.com/studio/run/emulator-networking.html
Thanks to the question and answers. It helps me figure out the narrow path towards my working combination.
The procedure turned out to be trickier than we expected. The socket client side has to use "127.0.0.1", instead of "192.168.0.15" or "10.0.2.15", to connect to the Android emulator on the same computer. The following are 6 steps I tried and works.
Step 0: Go to terminal
Step 1: find out port of Android emulator console using adb, Android Debug Bridge (in my case the port is 5554)
/Users/zhijunsheng/Library/Android/sdk/platform-tools/adb devices
List of devices attached
9357cefb device
emulator-5554 device
Step 2: retrieve auth_token of Android emulator console
cat /Users/<my_home_dir>/.emulator_console_auth_token
sncfmfC+Lg9OtAT4
Step 3: connect to Android emulator console
telnet localhost 5554
Trying ::1...
Connected to localhost.
OK
Step 4: authenticate
auth sncfmfC+Lg9OtAT4
Step 5: add the redirection instruction
redir add tcp:50000:50001
I also have a video in YouTube recording what I tried.