0

I'm trying to connect a client android to a app server java, but no work. This is code:

Android client;

_cb_led1.setOnClickListener(new View.OnClickListener() 
{ 
    public void onClick(View v)
    {
        Socket mySocket = new Socket("127.0.0.1", 9090);
        PrintStream p = new PrintStream(mySocket.getOutputStream());
        p.println("Mensaje");
    }
}); 

Java Server:

 s = new ServerSocket(9090);
 sc = s.accept();
 System.out.println("Conexión establecida");
 b = new BufferedReader( new InputStreamReader ( sc.getInputStream() ) );
 while ( true ) 
 {
      mensaje = b.readLine();
      System.out.println(mensaje);
   }
        b.close();

        sc.close();
        s.close();
    } 
    catch (IOException e) 
    {
        System.out.println("No puedo crear el socket");
    }
}

any suggestions

thank you very much

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Jjreina
  • 2,633
  • 6
  • 33
  • 54

2 Answers2

1

127.0.0.1 points to localhost on the emulator. You have to either use the actual ip address of your computer or 10.0.2.2 which points to localhost on the computer running the emulator.

Kristian
  • 6,443
  • 6
  • 27
  • 29
  • 1
    For more check out [Using the Android Emulator: Network Address Space](http://developer.android.com/guide/developing/devices/emulator.html#networkaddresses) – Philipp Reichart Oct 23 '11 at 16:47
  • Thanks Philipp I've been looking for that – Kristian Oct 23 '11 at 16:53
  • No, To make it work I had to put client and server on different machines. Whith java client and server work fine on same machines. Thx. – Jjreina Oct 24 '11 at 06:56
0

127.0.0.1 means "this machine". Is the server really on the same Android device (or emulator)?

If it is, why bother with socket connections? If it's not, please specify a real address or name.

From the standpoint of the Android emulator, the computer it's hosted on is not the same machine. If that's where the server is running, use its publicly available IP address.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • The server is on the same machine that the client (emulator andrid) as this is only a test. I has tried to use the public IP and no connection is established THX. – Jjreina Oct 23 '11 at 16:38