title: Android Socket doesn't connect to laptop server - but local Java program can
body: <p>Networking being a pain. In a service using AsyncTasks I attempt to start socket communication:</p>
  
  <pre><code>public class TransmitService extends Service {
  
      private Socket echoSocket = null;
      private static PrintWriter out = null;
      private String HOST = null;
      private int PORT = -1;
      private static Context context;
      public static boolean isConnected = false;
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          // TODO do something useful
          context = getApplicationContext();
          HOST = intent.getExtras().getString("HOST");
          PORT = intent.getExtras().getInt("PORT");
          Toast.makeText(context, "HOST/PORT: "+HOST+"/"+PORT, Toast.LENGTH_LONG).show();
          new initNetworkTask().execute();
          return Service.START_NOT_STICKY;
      }
  
      // Send the orientation data
      public static void sendData(float f1, float f2, float f3) {
          new sendDataTask().execute(f1, f2, f3);
      }
  
      static class sendDataTask extends AsyncTask&lt;Float, Void, Void&gt; {
  
          @Override
          protected Void doInBackground(Float... params) {
              try {
                  JSONObject j = new JSONObject();
                  j.put("yaw", params[0]);
                  j.put("pitch", params[1]);
                  j.put("roll", params[2]);
                  String jString = j.toString();
                  out.println(jString);
              } catch (Exception e) {
                  Log.e("sendDataTask", e.toString());
              }
              return null;
          }
  
      }
  
      class initNetworkTask extends AsyncTask&lt;Void, Void, Void&gt; {
          @Override
          protected Void doInBackground(Void... params) {
              try {
                  echoSocket = new Socket(HOST, PORT);
                  out = new PrintWriter(echoSocket.getOutputStream(), true);
                  out.println("Welcome.");
                  isConnected = true;
              } catch (Exception e) {
                  Log.e("initNetworkTask", e.toString());
                  isConnected = false;
              }
              return null;
          }
  
          @Override
          protected void onPostExecute(Void result) {
          }
      }
  
      @Override
      public IBinder onBind(Intent intent) {
          // TODO for communication return IBinder implementation
          return null;
      }
  }
  </code></pre>
  
  <p>My "server" is just a python script running on my laptop:</p>
  
  <pre><code>import socket
  
  HOST = '192.168.###.#'     #(numbers omitted from S/O question)             
  PORT = 10000             
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.bind((HOST, PORT))
  s.listen(1)
  conn, addr = s.accept()
  print 'Connected by', addr
  while 1:
      data = conn.recv(1024)
      if data is not None:
          print data
  conn.close()
  </code></pre>
  
  <p>The following server works just fine with this Java client running on the same laptop:</p>
  
  <pre><code>public class DataSender {
      public static void main(String[] args) throws IOException {
  
          Socket echoSocket = null;
          PrintWriter out = null;
          //BufferedReader in = null;
  
          try {
              echoSocket = new Socket("192.168.###.#", 10000);
              out = new PrintWriter(echoSocket.getOutputStream(), true);
              //in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
          } catch (UnknownHostException e) {
              System.err.println("Don't know about host: taranis.");
              System.exit(1);
          } catch (IOException e) {
              System.err.println("Couldn't get I/O for " + "the connection to: taranis.");
              System.exit(1);
          }
  
          BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
          String userInput;
  
          while ((userInput = stdIn.readLine()) != null) {
              out.println(userInput); //out.println - THIS IS HOW DATA IS SENT
              //System.out.println("echo: " + in.readLine());
          }
  
          out.close();
          //in.close();
          stdIn.close();
          echoSocket.close();
      }
  }
  </code></pre>
  
  <p>I really want to get this to work in my Android app - my purpose is to continuously stream orientation data via <code>TransmitService.sendData(f1,f2,f3)</code> to my laptop. </p>
  
  <p>In terms of testing: I have turned off windows firewall, I have tested on the same WiFi connection (Starbucks) and tried a few other ports (80, 4444, 4445, 5000). </p>
  
  <p>The comment error I get in my Android app is:</p>
  
  <p><code>java.net.ConnectException: failed to connect to /192.168.###.# (port 10000): connection failed: ETIMEDOUT (Connection timed out)</code></p>
  
  <p>Thanks for taking a look, would be happy to provide more information / run more tests to get this issue resolved. I'm also interested in considering other solutions for sending the orientation data from phone to laptop over internet. </p>