title: Android socket client - can't send messages
body: <p>First off, I'm a complete newbie to Android, Java and posting on Stacks Overflow so apologies if I'm doing anything wrong!</p>
  
  <p>I'm trying to create a simple app for a project that utilises sockets. I want to set up a client on an Android device that sends commands to a server running on a PC. I've written my server in Python and have tested it, so I know that side of the project works. I'm having issues with the implementation of the Android app.</p>
  
  <p>I followed this guide to write my client app:
  <a href="https://examples.javacodegeeks.com/android/core/socket-core/android-socket-example/" rel="nofollow noreferrer">https://examples.javacodegeeks.com/android/core/socket-core/android-socket-example/</a></p>
  
  <p>When I run my app, my server acknowledges that a client has connected to it. However, when I try to send data, nothing is received by the server. This is what my code looks like (it's pretty much the same as that in the link above):</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>package com.example.user.sockettest;
  
  import java.io.BufferedWriter;
  import java.io.IOException;
  import java.io.OutputStreamWriter;
  import java.io.PrintWriter;
  import java.net.InetAddress;
  import java.net.Socket;
  import java.net.UnknownHostException;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.EditText;
  
  public class MainActivity extends Activity {
  
  
      private Socket socket;
  
      private static final int SERVERPORT = 8000;
      private static final String SERVER_IP = "192.168.0.17";
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          new Thread(new ClientThread()).start();
      }
  
      public void onClick(View view) {
          try {
              EditText et = (EditText) findViewById(R.id.editText);
              String str = et.getText().toString();
              PrintWriter out = new PrintWriter(new BufferedWriter(
                      new OutputStreamWriter(socket.getOutputStream())),
                      true);
              out.println(str);
          } catch (UnknownHostException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      class ClientThread implements Runnable {
  
          @Override
          public void run() {
  
              try {
                  InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
  
                  socket = new Socket(serverAddr, SERVERPORT);
  
              } catch (UnknownHostException e1) {
                  e1.printStackTrace();
              } catch (IOException e1) {
                  e1.printStackTrace();
              }
  
          }
  
      }
  }
  </code></pre>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical" &gt;
  
          &lt;EditText
              android:id="@+id/editText"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="TestData" &gt;
          &lt;/EditText&gt;
  
          &lt;Button
              android:id="@+id/myButton"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="onClick"
              android:text="Send" &gt;
          &lt;/Button&gt;
  
      &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>AndroidManifest.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.user.sockettest"&gt;
  
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="17" /&gt;
  
      &lt;uses-permission android:name="android.permission.INTERNET" &gt;
      &lt;/uses-permission&gt;
  
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" &gt;
      &lt;/uses-permission&gt;
  
      &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
  
          &lt;activity android:name="com.example.user.sockettest.MainActivity"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>As you can see, I've set permissions as others have recommended on other threads but this does not seem to help.
  The socket runs on a separate thread so I doubt it's a blocking issue either.</p>
  
  <p>My experience with socket programming is mainly in Python so I'm sure I'm missing something very basic in its Java implementation!</p>
  
  <p>Any help or pointers would be really appreciated! </p>
  
  <p>EDIT:
  Update code that is working now, thanks to Gabe Sechan's answer</p>
  
  <pre><code>public class MainActivity extends Activity {
  
  
      private Socket socket;
      boolean sendData = false;   // Flag to indicate whether data should be sent
      String message;
  
      private static final int SERVERPORT = 8000;
      private static final String SERVER_IP = "192.168.0.17";
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          new Thread(new ClientThread()).start();
      }
  
      public void onClick(View view) {
          EditText et = (EditText) findViewById(R.id.editText);
          message = et.getText().toString();
          sendData = true;
      }
  
      class ClientThread implements Runnable {
  
          @Override
          public void run() {
              try {
                  InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
  
                  socket = new Socket(serverAddr, SERVERPORT);
                  BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  
                  // Loop forever checking if a message needs to be send
                  while (socket.isConnected()){
                      if (sendData){
                          // If a message is ready to be sent, call sendMessage method
                          sendMessage();
                      }
                  }
              } catch (UnknownHostException e1) {
                  e1.printStackTrace();
              } catch (IOException e1) {
                  e1.printStackTrace();
              }
          }
  
          private void sendMessage(){
              // Send message over socket
              try {
                  PrintWriter out = new PrintWriter(socket.getOutputStream());
                  out.println(message);
                  out.flush();
                  // Reset flag
                  sendData=false;
              } catch (UnknownHostException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>