I decided to connect the server with the Android application. Entered the necessary port: 7777, ip: 127.0.0.1 and clicked to start, but the client server does not see.

Here is the application code:

public class MainActivity extends AppCompatActivity { int sPort = 7777; String ip = "127.0.0.1"; private TextView textView; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView); button = (Button) findViewById(R.id.button); View.OnClickListener onClickListener = new View.OnClickListener() { @Override public void onClick(View v) { if(v.getId() == R.id.button) { new Thread(new Runnable() { @Override public void run() { try { textView.setText("Server off"); InetAddress inetAddress = InetAddress.getByName(ip); Socket socket = new Socket(ip, sPort); textView.setText("Connected"); InputStream sin = socket.getInputStream(); OutputStream sout = socket.getOutputStream(); DataInputStream in = new DataInputStream(sin); DataOutputStream out = new DataOutputStream(sout); } catch (Exception x) { x.printStackTrace(); } } }).start(); } } }; button.setOnClickListener(onClickListener); } } 
  • and the server is running on the same Android device? - Nofate
  • The server is running in the console, I previously wrote it in Notepad. The client is enabled on the Android Studio emulator. - Ker_ Jen
  • 127.0.0.1 is the loopback address of the device inside the emulator. The address of your computer on the network "computer ⇔ emulator" - 10.0.2.2 - Nofate
  • @Nofate, Thank you very much, for a long time I could not figure it out! - Ker_ Jen

1 answer 1

From official documentation .

Each emulator instance has an internally isolated network address space of 10.0.2/24 behind a virtual router. The network has the following addresses:

  • 10.0.2.1 - The main gateway.
  • 10.0.2.2 - A special alias for your loopback interface on the main machine outside the emulator ( 127.0.0.1 your computer).
  • 10.0.2.3 - Primary DNS server.
  • 10.0.2.4 / 10.0.2.5 / 10.0.2.6 - Optional additional DNS servers.
  • 10.0.2.15 - Address of the emulated device within the emulator network.
  • 127.0.0.1 - loopback interface of the emulated device within the emulator network.