I am currently trying to host a server socket on my android phone, and use my computer to connect to that one. There are several applications which already do so (for example Skype), so it has to be possible. The problem I am currently facing, is that I cannot connect to the server socket opened on my emulator. This is code I am running in Android Developer Tools emulator to open my server socket:
public void run(){
Log.i(TAG, "1");
try{
serverSocket = new ServerSocket(25555);
Log.i(TAG, "2. Details: " + serverSocket);
while(true)
{
clientSocket = serverSocket.accept(); //it keeps sticking here, and waiting for connections.
Log.i(TAG, "3");
in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream(), true);
String input = in.readLine();
out.println("received: " + input);
in.close();
out.close();
}
} catch(Exception e) {
Log.i(TAG, "ERROR:");
e.printStackTrace();
}
}
and this is the code I am running in normal eclipse to connect to this server socket:
try {
socket = new Socket(-snip-,25555);
System.out.println("Connection has been made");
} catch (NumberFormatException e) {
System.out.println("Connection wasn't made. Closing the application.");
}
This just gives me this error:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
Now my question is, is it something I did wrong in my code, or is it some problem with the emulator?
By the way, I added those lines of code to my AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
Thanks in advance.
Acording to the discussion, you have to connect a client to an Android Virtual Device Server. This means you need to know you Android Virtual Device's IP. There are some ways to achieve this:
The first is to create a network and then connect both your Android Device and your Computer/Client, and then they will have an IP.
You can assign an static IP to the Android Device, in this case you should check this link.