title: Connecting android client to C server
body: <p>I am having trouble connecting my android client to a C server that I wrote. I found another thread with a similar question but unfortunately it was closed saying that it was too narrow. I hope that somebody can answer it here since this is relevant to me as well and all sample codes that I could find online only had the android client connecting to an android server. My client code is the following:</p>
  
  <pre><code>package edu.upenn.seas.cis542;
  
  import java.io.BufferedWriter;
  import java.io.OutputStreamWriter;
  import java.io.PrintWriter;
  import java.net.InetAddress;
  import java.net.Socket;
  
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.os.Bundle;
  import android.os.Debug;
  import android.os.Handler;
  import android.util.Log;
  import android.view.Gravity;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class Try_uiActivity extends Activity
  {
  
  //private EditText serverIp;
  private Button connectPhones;
  private String serverIpAddress = "MY IP ADDRESS";
  private boolean connected = false;
  private Handler handler = new Handler();
  final int SERVERPORT = 8080;
  
  
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      //NumberPicker t1 = (NumberPicker)findViewById(R.id.numberPicker3);
      //t1.setRange(1, 5);
      setContentView(R.layout.main);
  
  }
  
  /*
   * When the Submit button is clicked, this method
   * gets called.
   */  
  
  
  public void onSubmitClick(View view) {
  
  
  
      int item1, item2, item3;
  
      NumberPicker t1 = (NumberPicker)findViewById(R.id.numberPicker1);
      item1 = t1.getCurrent();
  
      NumberPicker t2 = (NumberPicker)findViewById(R.id.numberPicker2);
      item2 = t2.getCurrent();
  
      NumberPicker t3 = (NumberPicker)findViewById(R.id.numberPicker3);
      item3 = t3.getCurrent();
  
      String userInput = "" + item1 + item2 + item3;
  
      //Log.out("Before connecting");
  
      if (!connected) {
          try {
              InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
  
              Toast toast=Toast.makeText(this, serverAddr.toString(), Toast.LENGTH_LONG);
              toast.setGravity(Gravity.CENTER, 0, 0);
              toast.show();
  
  
              Socket socket = new Socket(serverAddr, SERVERPORT);
              connected = true;
          } catch (Exception e) {
              Toast toast=Toast.makeText(this, "Error2", Toast.LENGTH_LONG);
              toast.setGravity(Gravity.CENTER, 0, 0);
              toast.show();
  
              e.printStackTrace();
  
              connected = false;
          }
      }
  }
  
  
  
  
  }
  </code></pre>
  
  <p>I omitted what I want to do after it is connecting because just creating the socket already gives me an error. My C server opens a connection on the same port that the android program is connecting to. When I tested the server with a java client file, it worked fine. Does anybody know a solution to this? I also added the permissions to the Android Manifest, but that didn't solve the issue. Thanks!</p>