Tested on the desktop - everything works. On the device, the application closes when a button is pressed. Tested without it - all the same. Checked on several guides - they work on about the same princes, but even when trying to copy from them, the problem remains.

package com.example.theonr.reboot2.feature; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.InetAddress; import java.net.Socket; public class MainActivity extends AppCompatActivity { Button btnOk; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnOk = (Button) findViewById(R.id.btnOk); View.OnClickListener oclBtnCancel = new View.OnClickListener() { @Override public void onClick(View v) { action(); } }; btnOk.setOnClickListener(oclBtnCancel); } public void action(){ try { System.out.println("Start..."); Socket pingSocket = null; PrintWriter out = null; BufferedReader in = null; try { pingSocket = new Socket(InetAddress.getByName("192.168.0.1"), 23); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(pingSocket.getOutputStream())), true); in = new BufferedReader(new InputStreamReader(pingSocket.getInputStream())); } catch (IOException e) { return; } for(int i=0; i<2; i++) in.readLine(); out.println("admin"); for(int i=0; i<1; i++)in.readLine(); out.println("pwd"); for(int i=0; i<15; i++)in.readLine(); out.println("reboot"); for(int i=0; i<2; i++)in.readLine(); out.close(); in.close(); pingSocket.close(); } catch (IOException e) {} System.out.println("Finished!"); } } 
  • error code in the studio - George Chebotaryov
  • I do not have the opportunity to get the code, since the device does not connect to the PC for debugging (my personal problem, which I cannot fix now), but I don’t know how to do it directly from it. That is why I initially tested on the desktop (the benefit of the library, in general, the same ones are used) - Nikita Kovalev
  • one
    Possible duplicate question: How to fix android.os.NetworkOnMainThreadException - zRrr
  • connect to the fabric app. It stores crash data in the system. It will be possible to look at the reception. Perhaps, then you will be able to correct the error yourself) But if anything - open a new question with an error code - Georgy Chebotaryov
  • Thank you for doing this. This is good advice) - Nikita Kovalev

1 answer 1

The problem was solved, the matter was really in the streams (thanks to the commentator). When working with a network, it was impossible to work in the same stream as the main program.

  • Expand the answer exactly how the problem was solved. - VAndrJ