Sockets on the android emulator is such a pain. I've been strugling with it for 2 days now.
It seems that there are 2 main problems:
<uses-permission android:name="android.permission.INTERNET"/>
Must be added to AndroidManifest.xml
If you had a real device instead of the emulator, the pain would go away. This is because they implemented a virtual router between the development computher and the emulator.
http://developer.android.com/guide/deve ... networking
Theres also a "guide" for how you would go about getting 2 emulators interconnect.
http://developer.android.com/guide/deve ... connecting
You should be aware that the emulator has its own loopback (127.0.0.1). You can connect to your development machines loopback through 10.0.2.2.
I find it strange that there seem to be no 'guide' to this problem.
If you haven set up your enviorment, you have to go to: ...\android-sdk-windows\tools to be able to use the commands. This is done in cmd.exe
C:\>adb devices
List of devices attached
emulator-5554 device
emulator-5556 device
C:\>adb -s emulator-5554 forward tcp:44000 tcp:44000
--- Update: Think I got it to work --- Her is my step-by-step guide of how I did it:
C:\>cd C:\Prosjekter
C:\Prosjekter\>android create project --package eksempel.server --activity Server --target android-7 --path C:/Prosjekter/Server
C:\Prosjekter\>android create project --package eksempel.klient --activity Klient --target android-7 --path C:/Prosjekter/Klient
Change AndroidManifest.xml for both project, so that it includes the "uses-permission" tag:
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
Change C:/Prosjekter/Server/src/Server.java to:
Using java Syntax Highlighting
package eksempel.server;
import java.io.*;
import java.net.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Server extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String res = "FAIL";
try {
ServerSocket ss = new ServerSocket(12345);
Socket s = ss.accept();
System.out.println("S: Client connected" + s.toString());
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
res = in.readLine();
out.println("PONG" + res);
} catch (IOException e) {
e.printStackTrace();
}
TextView tv = new TextView(this);
tv.setText("S: " + res);
setContentView(tv);
}
}
Parsed in 0.037 seconds, using
GeSHi 1.0.8.4
Change C:/Prosjekter/Klient/src/Klient.java to:
Using java Syntax Highlighting
package eksempel.klient;
import java.io.*;
import java.net.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Klient extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String res = "FAIL";
try {
Socket s = new Socket("10.0.2.2", 12345);
System.out.println("C: Connected to server" + s.toString());
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out.println("PING");
res = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
TextView tv = new TextView(this);
tv.setText("C: " + res);
setContentView(tv);
}
}
Parsed in 0.039 seconds, using
GeSHi 1.0.8.4
Create avd for server and client
C:\Prosjekter\>android create avd --name server --target android-7
C:\Prosjekter\>android create avd --name klient --target android-7
Start avd server and client (this take som time, atleast on my machine)
(non-windows users have to drop the "start" part and create a terminal for each launch of emulators)
C:\Prosjekter\>start emulator -avd server
C:\Prosjekter\>start emulator -avd klient
Build using ant. Most of you might not have and it.
For Eclipseusers without ant:
- Create the projects by: File -> New -> Android Project: "Create project form existing source" and chose "C:\Prosjekter\Server\"
- Specify the the right emulators when you launch the projects. Server first and client last. You may also skip the rest of this "document"
C:\Prosjekter\>cd Server
C:\Prosjekter\Server\>c:\ant\bin\ant.bat debug
C:\Prosjekter\Server\>cd..
C:\Prosjekter\>cd Klient
C:\Prosjekter\Klient\>c:\ant\bin\ant.bat debug
C:\Prosjekter\Klient\>cd..
Look at the gui window, you can see something like "5554:server" and "5556:klient"
C:\Prosjekter\>adb devices
List of devices attached
emulator-5556 device
emulator-5554 device
Forward from localhost to server emulator (emulator-5554 = server)
C:\Prosjekter\>adb -s emulator-5554 forward tcp:12345 tcp:12345
C:\Prosjekter\>adb -s emulator-5554 install C:\Prosjekter\Server\bin\Server-debug.apk
C:\Prosjekter\>adb -s emulator-5556 install C:\Prosjekter\Klient\bin\Klient-debug.apk
On the server-emulator, go to the Server app and start it
On the client-emulator, go to the Klient app and start it
The client sends the string "PING" to the server. The server read the string and replays "PING" + "PONG" to the client.
If all works you should see: "S: PING" on the server and "C: PINGPONG" on the client
If you see "S: FAIL" and/or "C: FAIL" then your out of luck. Make sure you did the portforwarding right, and check your firewall.
Attachments: (a reason to become a member, non-members cant see/access them)
- Beutifull diagram of the relations between network and android emulators
- Klient and Server Eclipse project