'How to setup connection (choosing ip adress) from Android App on physical device to Python Program using sockets?

I am a beginner in socket programming. I wrote a Python code that creates a socket listening on port 12345 :

port=12343
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
IP = '0.0.0.0'
s.bind((IP,port))
s.listen(nb_max_connex)

print(f"Le serveur est lancé sur {IP} sur le port {port}.")

connexion,addresse=s.accept() #accepter la connexion 
print("Connecté à ",addresse)

Here is my java code :

            Socket soc=new Socket("10.0.2.2",12343);
            if(soc.isConnected()){
                CharSequence text = "La connexion avec le système a été réalisée";
                Context context = GlobalApplication.getAppContext();
                Log.d("CONNECT","connecté au socket");
                //Toast.makeText(context,text,Toast.LENGTH_SHORT).show();
            }

10.0.2.2 works well with android emulator, the connection is successful. However when I install my app on my phone, I can't determine how the connection can be made, I have tried using Socket soc=new Socket("127.0.0.1",12343) but there is no connection...

Could someone help me out ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source