title: Can't run client/ server application
body: <p>I am currently working on writing a simple message application on Android. I have the server in a class called <code>RunServer</code>, which runs perfectly. Then I have a client in a method in my <code>MainActivity</code> called <code>startClient()</code>. I have a button, which has an <code>onclick="startClient"</code>. But whenever I click on the button, nothing happens. I tried to put in logs everywhere, and this is where it doesn't work:</p>
  
  <pre><code>socket.connect(ipPort);
  </code></pre>
  
  <p>I can't seem to figure out what the problem is. I have tried different ports and IP addresses, but still nothing.</p>
  
  <p><code>MainActivity</code>:</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      private Socket socket;
      private ObjectInputStream ois;
      private DataOutputStream dos;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          runOnUiThread(new Runnable() {
  
              public void run() {
                  EditText et = (EditText) findViewById(R.id.message_id);
                  String message = et.getText().toString();
                  EditText et1 = (EditText) findViewById(R.id.messages);
                  et1.append(message);
              }
          });
      }
  
      public void startClient(View v) {
          socket = new Socket();
          InetSocketAddress ipPort = new InetSocketAddress("10.0.2.2", 4444);
          System.out.println("HEJ!");
          try {
              socket.connect(ipPort); // här tar det stop
              Log.v("It works", "Hello");
              dos = new DataOutputStream(socket.getOutputStream());
          } catch (Exception e) {
          }
          Thread thread = new Thread();
          thread.start();
      }
  }
  </code></pre>
  
  <p><code>RunServer</code>:</p>
  
  <pre><code>public class RunServer {
  
      private ServerSocket server;
      private Socket socket;
      private DataInputStream dis;
      private int port;
  
      public RunServer(int port) {
          this.port = port;
          Thread connectThread = new Thread(new Connect());
          connectThread.start();
      }
  
      private class Connect implements Runnable {
  
          public void run() {
              try {
                  server = new ServerSocket(port);
                  System.out.println("Server running");
                  while (true) {
                      socket = server.accept();
                      Thread clientThread = new Thread(new TalkToClient(socket));
                      clientThread.start();
                      System.out.println("someone connected");
                  }
              } catch (IOException ex) {
                  Logger.getLogger(RunServer.class.getName()).log(Level.SEVERE,
                          null, ex);
              }
              try {
                  server.close();
              } catch (Exception e) {
              }
          }
      }
  
      private class TalkToClient implements Runnable {
  
          private Socket socket;
  
          public TalkToClient(Socket socket) {
              this.socket = socket;
          }
  
          public void run() {
              String clientMsg;
              try {
                  dis = new DataInputStream(socket.getInputStream());
                  while ((clientMsg = dis.readUTF()) != null) {
                      if (clientMsg.length() &gt; 0) {
                          System.out.println("Whatup");
                      }
                  }
              } catch (Exception e) {
                  System.out.println(e);
              }
          }
      }
  
      public static void main(String[] args) {
          new RunServer(4444);
      }
  }
  </code></pre>
  
  <p>The layout:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@drawable/bakgrund"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".MainActivity" &gt;
  
      &lt;TextView
          android:id="@+id/textView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"
          android:text="@string/ip"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:textColor="@color/white" /&gt;
  
      &lt;EditText
          android:id="@+id/ip_id"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentRight="true"
          android:layout_below="@+id/textView1"
          android:background="@color/white"
          android:ems="10"
          android:hint="@string/get_ip"
          android:inputType="numberDecimal" &gt;
  
          &lt;requestFocus /&gt;
      &lt;/EditText&gt;
  
      &lt;TextView
          android:id="@+id/textView2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_below="@+id/ip_id"
          android:text="@string/port"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:textColor="@color/white" /&gt;
  
      &lt;EditText
          android:id="@+id/port_id"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentRight="true"
          android:layout_below="@+id/textView2"
          android:background="@color/white"
          android:ems="10"
          android:hint="@string/get_port"
          android:inputType="number" /&gt;
  
      &lt;TextView
          android:id="@+id/textView3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/port_id"
          android:layout_below="@+id/port_id"
          android:text="@string/message"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:textColor="@color/white" /&gt;
  
      &lt;EditText
          android:id="@+id/message_id"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentRight="true"
          android:layout_below="@+id/textView3"
          android:background="@color/white"
          android:ems="10"
          android:hint="@string/get_message" /&gt;
  
      &lt;Button
          android:id="@+id/connect"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_below="@+id/message_id"
          android:onClick="startClient"
          android:text="@string/connect"
          android:textColor="@color/white" /&gt;
  
      &lt;Button
          android:id="@+id/send"
          style="?android:attr/buttonStyleSmall"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignBaseline="@+id/connect"
          android:layout_alignBottom="@+id/connect"
          android:layout_toRightOf="@+id/connect"
          android:text="@string/send"
          android:textColor="@color/white" /&gt;
  
      &lt;TextView
          android:id="@+id/status_id"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/connect"
          android:layout_below="@+id/send"
          android:layout_marginTop="23dp"
          android:text="@string/status"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:textColor="@color/white" /&gt;
  
      &lt;EditText
          android:id="@+id/messages"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_alignParentLeft="true"
          android:layout_alignParentRight="true"
          android:layout_below="@+id/status_id"
          android:background="@color/white"
          android:ems="10"
          android:inputType="textMultiLine" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>The manifest:</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.datorkomprojekt"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="17" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.datorkomprojekt.MainActivity"
              android:label="@string/app_name" &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>