Android:: socket communication between pc and phone over wifi?
I have a working code in which i can exchange data between pc and android
emulator. Now, I want to test this application with a real android device.
i.e i want to exchange text between my pc in which i have a java server
and the android phone. For my emulator i was using the emulator virtual ip
address and it was working fine. But, to test my application in phone I
connected the phone and pc in same network and used the ip address of the
phone in my application. Then I installed the application in phone but the
connection did not established. How other changes is to be done in code to
use this application in phone. Here, is my android code,
try {
client = new Socket("10.0.2.2", 9999);
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.println(messsage[0] + "\n");
System.out.println("messsage::"+messsage[0]);
printwriter.flush();
BufferedReader in = new BufferedReader(new
InputStreamReader(client.getInputStream()));
serverMessage = in.readLine();
if (serverMessage.equals("exit")) {
runOnUiThread(new Runnable() {
public void run() {
tv.setText("Thankyu visit again!!" + "\n");
}
}); }
else {
runOnUiThread(new Runnable() {
public void run() {
tv.setText(serverMessage.toString() + "\n");
}
});
}
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
Also, i have given this permission in my manifest file:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission
android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission
android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
This is my first android app, So please help!!!
No comments:
Post a Comment