Deniz Yılmaz
Deniz Yılmaz

Reputation: 13

Android crash when using sockets

I'm trying to get packets from a C++ TCP socket on a Windows server. I tested my server with Putty. It's working, but I can't create a client from Android. I tested this code and it's crashing immediately when I open the app.

API Level: 26 Test Phone: Oreo 8.0

Edit: it's now crashing 3-4 seconds after connecting. I tried with other TCP client apps, and those work correctly.

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  Intent intent = getIntent();
  String message = intent.getStringExtra(LoginActivity.EXTRA_MESSAGE);

  // Capture the layout's TextView and set the string as its text
  TextView textView2 = findViewById(R.id.textView2);
  TextView textView = findViewById(R.id.textView);
  textView2.setText(message);
  ip = message;

  Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
      try {
        TextView textView = findViewById(R.id.textView);
        Socket socket = new Socket("192.168.1.108", 2970);
        BufferedReader in = new BufferedReader(
            new InputStreamReader(socket.getInputStream()));
        textView.setText(in.readLine());
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  });
  thread.start();
}

Upvotes: 0

Views: 148

Answers (2)

madlymad
madlymad

Reputation: 6530

As suggested by mangusta you can try the connection on a different Thread other than the mainThread.

I add here a possible workaround for that:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = getIntent();
    String message = intent.getStringExtra(LoginActivity.EXTRA_MESSAGE);

    // Capture the layout's TextView and set the string as its text
    TextView textView2 = findViewById(R.id.textView2);
    TextView textView = findViewById(R.id.textView);
    textView2.setText(message);
    ip = message;
    new Thread(new Runnable() {
        public void run() {
            // a potentially time consuming task
            try {
                Socket socket = new Socket("192.168.1.108", 2970);
                final BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                textView.post(new Runnable() {
                    public void run() {
                        textView.setText(in.readLine());
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();

                textView.post(new Runnable() {
                    public void run() {
                        textView.setText(e.toString());
                    }
                });
            }

        }
    }).start();
}

This is just a fast implementation just for trying the connection.

Upvotes: 1

DemiDust
DemiDust

Reputation: 333

I think this should be your error: android.os.NetworkOnMainThreadException

try making thread and wrap your socket call around it

        Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            //Do your socket call here

        }
    });

    thread.start();

Also don't forget to do your ui update outside thread (text view)

Upvotes: 1

Related Questions