Jjreina
Jjreina

Reputation: 2703

Java server and Android client won't connect

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

Upvotes: 0

Views: 1259

Answers (2)

Kristian
Kristian

Reputation: 6613

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.

Upvotes: 1

Seva Alekseyev
Seva Alekseyev

Reputation: 61341

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.

Upvotes: 0

Related Questions