Reputation: 2816
I am sending data from my development machine (Mac) via a Java socket client to android emulator which acts as the socket server. So basically I start android program first which opens socket port and waits for the data.
onStart calls two functions one to get IP, other to open server socket
@Override
protected void onStart() {
super.onStart();
getDeviceIpAddress();
new Thread(new start_siddhi_server()).start();
} //onStart
Code used to get IP is
public void getDeviceIpAddress() {
try {
//Loop through all the network interface devices
for (Enumeration<NetworkInterface> enumeration = NetworkInterface
.getNetworkInterfaces(); enumeration.hasMoreElements(); ) {
NetworkInterface networkInterface = enumeration.nextElement();
//Loop through all the ip addresses of the network interface devices
for (Enumeration<InetAddress> enumerationIpAddr = networkInterface.getInetAddresses(); enumerationIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumerationIpAddr.nextElement();
//Filter out loopback address and other irrelevant ip addresses
if (!inetAddress.isLoopbackAddress() && inetAddress.getAddress().length == 4) {
//Print the device IP address into the text view
Log.d("ip address", inetAddress.getHostAddress());
}
}
}
} catch (SocketException e) {
Log.e("ERROR:", e.toString());
}
}
Code used to start server socket is
class start_siddhi_server implements Runnable {
@Override
public void run() {
Log.d("E", "starting siddhi server.........");
// Creating Siddhi Manager
SiddhiManager siddhiManager = new SiddhiManager();
String definition1 = " define stream temprature (room_no string, temp int);";
String query1 = "@info(name = 'query1') from temprature[temp >= 10] " +
"select room_no, temp " +
"insert into myOutputStream; ";
//Generating runtime
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(definition1 + query1);
//Adding callback to retrieve output events from query
siddhiAppRuntime.addCallback("myOutputStream", new StreamCallback() {
@Override
public void receive(Event[] events) {
// for loop for iterating each event
for (Event event : events) {
Log.d("complex event = ", " Temp is " + event.getData(1));
} //end of for
} // end of receive method
}); // end of callback
// getting input handler
InputHandler inputHandlerA = siddhiAppRuntime.getInputHandler("temprature");
// starting siddhi app
siddhiAppRuntime.start();
// starting server socket
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(7000);
} catch (IOException e) {
e.printStackTrace();
}
Socket socket = null;
Log.d("E", " Siddhi server is waiting for sensor data.........");
while (true) {
try {
socket = serverSocket.accept();
} catch (IOException e) {
e.printStackTrace();
}
if (socket.isConnected() == true) {
Log.d("E", "connection is successful........");
InputStreamReader isReader = null;
try {
isReader = new InputStreamReader(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader input = new BufferedReader(isReader);
String line;
try {
while ((line = input.readLine()) != null) {
String[] tupleData = line.split(",");
String[] sensor_value = new String[10];
int i = 0;
for (String tuple : tupleData) {
String[] tupleValue = tuple.split("=");
sensor_value[i] = tupleValue[1];
i += 1;
}// key pair of tuple ends
Integer patient_id = Integer.valueOf(sensor_value[0]);
Integer sensor_id = Integer.valueOf(sensor_value[1]);
Integer uid = Integer.valueOf(sensor_value[2]);
Long egtl = Long.valueOf(sensor_value[3]);
Long edtl = 0L;
// Long edtl = Long.valueOf(sensor_value[4]);
Integer value = Integer.valueOf(sensor_value[4]);
inputHandlerA.send(new Object[]{uid, value});
// Log.d("patient_id = ", String.valueOf(patient_id));
// Log.d("sensor_id = ", String.valueOf(sensor_id));
// Log.d("uid = ", String.valueOf(uid));
// Log.d("egtl = ", String.valueOf(egtl));
}//while
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
} //socket.isConnected()
} //while (true)
} //run
} // runnig_server
Also, the permissions in manifest file are
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--The Paho Android Service needs the following permissions to work-->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
when I run it using emulator I get the following IP
10.0.2.15
Result of ifconfig is same as well
generic_x86:/ $ ifconfig eth0
eth0 Link encap:UNSPEC Driver virtio_net
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
inet6 addr: fe80::5054:ff:fe12:3456/64 Scope: Link
inet6 addr: fec0::5054:ff:fe12:3456/64 Scope: Site
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:3565 errors:0 dropped:0 overruns:0 frame:0
TX packets:4068 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1190457 TX bytes:971370
But the issue is that I am not able to send data to this port.
I have tried following IP's
10.0.2.1
10.0.2.2
10.0.2.15
localhostip
But when I run the same code in Genymotion I get the following result
IP address: 192.168.56.101
When I send data to this IP it works.
What I am doing wrong?
How can I make it work for the android emulator?
I want to u the e android emulator as it is free 😀
the issue was solved by port forwarding
adb forward tcp:7000 tcp:7000
Now the problem is that I am not able to see network data in profiler in case I am using emulator
But in case I am using Genymotion , I am able to see the network data
Upvotes: 0
Views: 592
Reputation: 69208
You should add a port forwarding
adb forward tcp:7000 tcp:7000
and then use localhost:7000
to send data from your computer to the emulator.
The reason for this is that emulator creates a private virtual network (10.0.2.0) which is not bridged, so to be able to access it you have to forward ports.
Upvotes: 2