1

On virtual device is working, on real device not. Checked all ip, many ports, but nothing.

Android 7.1.1 Lenovopad TB-X304F

In manifest:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Code:

public class MainActivity extends AppCompatActivity {

EditText e1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    e1 = (EditText)findViewById(R.id.editText);
}

public void send_text(View v) {
    String message = e1.getText().toString();
    myTask mt = new myTask();
    mt.execute(message);

    Toast.makeText(getApplicationContext(),"Data sent: "+e1.getText().toString(), Toast.LENGTH_LONG).show();
}

class myTask extends AsyncTask<String, Void, Void>{
    Socket s;
    PrintWriter printWriter;

    @Override
    protected Void doInBackground(String... params){
        try {
            String message = params[0];
            s = new Socket("192.168.1.5", 6001);
            printWriter = new PrintWriter(s.getOutputStream());
            printWriter.write(message);
            printWriter.flush();
            printWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Error:

W/System.err: java.net.ConnectException: Connection timed out

1

1 Answer 1

0

The mistake was that I did not know that I had to disable the firewall and open the port. I opened the port and it all worked.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.