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

  • 2
    Possible duplicate of [Why would a "java.net.ConnectException: Connection timed out" exception occur when URL is up?](https://stackoverflow.com/questions/86824/why-would-a-java-net-connectexception-connection-timed-out-exception-occur-wh) – doganak Feb 04 '18 at 14:22

1 Answers1

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.