title: Can't connect to socket using Android Studio
body: <p>I have a problem connecting to telnet using Android Studio. Connecting through a terminal works just fine, but when I try to connect to Android Studio it doesn't connect. I just can't figure out what I'm doing wrong.</p>
  
  <p>I can tell it's not connected because when I look at my telnet connection, a new message should come up, but the telnet does not change at all. I can provide extra information, if needed.</p>
  
  <p>My MainActivity.java:</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      private static final String LOGTAG = "MainPAge";
  
      private ServerConnect mSC;
  
      Thread serverThread = null;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Log.i(LOGTAG, "onCreate entered");
  
          this.serverThread = new Thread(new ServerConnect(this));
          this.serverThread.start();
  
          // Connect to server and continue from here TODO
          //// Your code should go here
          final Button send = (Button) findViewById(R.id.btnSendCmd);
  
  
  
  ;
          send.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
  
  
              }
          });
  
  
          //Makes the receiving text area scrollable
          TextView tv = (TextView) findViewById(R.id.txtServerResponse);
          tv.setMovementMethod(new ScrollingMovementMethod());
  
          //This is an example of how to set events to button clicks
          Button button = (Button) findViewById(R.id.btnKill);
          button.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  //OnClick actions here
                  System.exit(0);
  
              }
          });
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
  
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
  
  }
  </code></pre>
  
  <p>ServerConnect.java:</p>
  
  <pre><code>public class ServerConnect extends Thread{
  
      public static final int BUFFER_SIZE = 2048;
  
      private Socket socket;
      private static final int SERVERPORT = 9999;         //This is the port that we are connecting to
                                                          //Remember the channel simulator is 9998
  
      private static final String SERVERIP = "10.0.2.2";  //This address is magically mapped to the host's loopback.
      private static final String LOGTAG = "SocketTester";
  
      boolean terminated = false;
  
      private PrintWriter out = null;
      private BufferedReader in = null;
  
      Activity parentref;
  
      /**
       *
       * @param parentRef Expects a reference to the calling activity, e.g. new ServerConnect(this);
       */
      public ServerConnect(Activity parentRef)
      {
          parentref=parentRef;
      }
  
      /**
       * Sends commands to the server. Called from UI thread via a button press
       * @param cmd
       */
      public void send(String cmd)
      {
          try
          {
              Log.i(LOGTAG,"Sending command: "+cmd);
              out.println(cmd);
          }
          catch(Exception e)
          {
              Log.e(LOGTAG,"Failed to send command : "+e);
          }
      }
  
      /**
       * Main thread loop that grabs incoming messages
       */
      public void run()
      {
          Log.i(LOGTAG,"Running socket thread");
  
  
          try
          {
              InetAddress svrAddr = InetAddress.getByName(SERVERIP);
              socket = new Socket(svrAddr, SERVERPORT);
  
              //Setup i/o streams
              out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
              in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  
              //Keep listening for messages from server
              while(!terminated)
              {
  
                  final String message = in.readLine();
                  if(message != null &amp;&amp; message != "")
                  {
                      Log.i(LOGTAG,"MSG recv : "+message);
  
                      //Update GUI with any server responses
                      final TextView txtv = (TextView) parentref.findViewById(R.id.txtServerResponse);
                      parentref.runOnUiThread(new Runnable() {
                          @Override
                          public void run() {
  
  
  
                              /**
                               *
                               *
                               * This is where the the UI gets updated by the socket
                               *
                               *
                               */
                              Log.i(LOGTAG, "TEST");
  
  
                              txtv.setText(message+"\n"+txtv.getText());
                          }
                      });
                  }
              }
  
          }
          catch (UnknownHostException uhe)
          {
              Log.e(LOGTAG,"Unknownhost\n"+uhe.getStackTrace().toString());
          }
          catch (Exception e) {
              Log.e(LOGTAG, "Socket failed\n"+e.getMessage());
              e.printStackTrace();
          }
  
          disconnect();
          Log.i(LOGTAG,"Thread now closing");
      }
  
      /**
       * Disconnect from the server as well as closing i/o streams
       */
      public void disconnect()
      {
          Log.i(LOGTAG, "Disconnecting from server");
          try
          {
              in.close();
              out.close();
          }
          catch(Exception e)
          {/*do nothing*/}
  
          try
          {
              socket.close();
          }
          catch(Exception e)
          {/*do nothing*/}
  
  
      }
  
  }
  </code></pre>
  
  <p>And my stacktrace:</p>
  
  <pre><code>04-21 03:26:22.944 25927-25927/? E/Zygote: v2
  04-21 03:26:22.944 25927-25927/? I/libpersona: KNOX_SDCARD checking this for 10004
  04-21 03:26:22.944 25927-25927/? I/libpersona: KNOX_SDCARD not a persona
  04-21 03:26:22.944 25927-25927/? I/SELinux: Function: selinux_compare_spd_ram, index[1], SPD-policy is existed. and_ver=SEPF_SM-G920F_5.1.1 ver=38
  04-21 03:26:22.944 25927-25927/? W/SELinux: Function: selinux_compare_spd_ram, index[1], priority [2], priority version is VE=SEPF_SECMOBILE_6.0.1_0008
  04-21 03:26:22.944 25927-25927/? E/Zygote: accessInfo : 0
  04-21 03:26:22.944 25927-25927/? W/SELinux: SELinux: seapp_context_lookup: seinfo=default, level=s0:c512,c768, pkgname=uk.ac.man.cs.COMP28512.lab4 
  04-21 03:26:22.944 25927-25927/? I/art: Late-enabling -Xcheck:jni
  04-21 03:26:22.984 25927-25927/? D/TimaKeyStoreProvider: TimaSignature is unavailable
  04-21 03:26:22.984 25927-25927/? D/ActivityThread: Added TimaKeyStore provider
  04-21 03:26:23.314 25927-25927/uk.ac.man.cs.COMP28512.lab4 W/ResourcesManager: getTopLevelResources: /data/app/uk.ac.man.cs.COMP28512.lab4-2/base.apk / 1.0 running in uk.ac.man.cs.COMP28512.lab4 rsrc of package uk.ac.man.cs.COMP28512.lab4
  04-21 03:26:23.314 25927-25927/uk.ac.man.cs.COMP28512.lab4 I/InjectionManager: Inside getClassLibPath + mLibMap{0=, 1=}
  04-21 03:26:23.314 25927-25927/uk.ac.man.cs.COMP28512.lab4 D/ResourcesManager: For user 0 new overlays fetched Null
  04-21 03:26:23.324 25927-25927/uk.ac.man.cs.COMP28512.lab4 I/InjectionManager: Inside getClassLibPath caller 
  04-21 03:26:23.324 25927-25927/uk.ac.man.cs.COMP28512.lab4 W/System: ClassLoader referenced unknown path: /data/app/uk.ac.man.cs.COMP28512.lab4-2/lib/arm64
  04-21 03:26:23.334 25927-25927/uk.ac.man.cs.COMP28512.lab4 D/InjectionManager: InjectionManager
  04-21 03:26:23.334 25927-25927/uk.ac.man.cs.COMP28512.lab4 D/InjectionManager: fillFeatureStoreMap uk.ac.man.cs.COMP28512.lab4
  04-21 03:26:23.334 25927-25927/uk.ac.man.cs.COMP28512.lab4 I/InjectionManager: Constructor uk.ac.man.cs.COMP28512.lab4, Feature store :{}
  04-21 03:26:23.334 25927-25927/uk.ac.man.cs.COMP28512.lab4 I/InjectionManager: featureStore :{}
  04-21 03:26:23.334 25927-25927/uk.ac.man.cs.COMP28512.lab4 W/ResourcesManager: getTopLevelResources: /data/app/uk.ac.man.cs.COMP28512.lab4-2/base.apk / 1.0 running in uk.ac.man.cs.COMP28512.lab4 rsrc of package uk.ac.man.cs.COMP28512.lab4
  04-21 03:26:23.334 25927-25927/uk.ac.man.cs.COMP28512.lab4 W/ResourcesManager: getTopLevelResources: /data/app/uk.ac.man.cs.COMP28512.lab4-2/base.apk / 1.0 running in uk.ac.man.cs.COMP28512.lab4 rsrc of package uk.ac.man.cs.COMP28512.lab4
  04-21 03:26:23.424 25927-25927/uk.ac.man.cs.COMP28512.lab4 I/MainPAge: onCreate entered
  04-21 03:26:23.424 25927-25953/uk.ac.man.cs.COMP28512.lab4 I/SocketTester: Running socket thread
  04-21 03:26:23.424 25927-25927/uk.ac.man.cs.COMP28512.lab4 D/Activity: performCreate Call Injection manager
  04-21 03:26:23.424 25927-25953/uk.ac.man.cs.COMP28512.lab4 I/SocketTester: LOL
  04-21 03:26:23.434 25927-25927/uk.ac.man.cs.COMP28512.lab4 I/InjectionManager: dispatchOnViewCreated &gt; Target : uk.ac.man.cs.COMP28512.lab4.MainActivity isFragment :false
  04-21 03:26:23.434 25927-25927/uk.ac.man.cs.COMP28512.lab4 D/SecWifiDisplayUtil: Metadata value : SecSettings2
  04-21 03:26:23.434 25927-25927/uk.ac.man.cs.COMP28512.lab4 D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{32ba211 I.E...... R.....ID 0,0-0,0}
  04-21 03:26:23.444 25927-25954/uk.ac.man.cs.COMP28512.lab4 D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
  04-21 03:26:23.534 25927-25954/uk.ac.man.cs.COMP28512.lab4 D/libEGL: loaded /vendor/lib64/egl/libGLES_mali.so
  04-21 03:26:23.554 25927-25954/uk.ac.man.cs.COMP28512.lab4 D/libEGL: eglInitialize EGLDisplay = 0x7f7a7fa178
  04-21 03:26:23.554 25927-25954/uk.ac.man.cs.COMP28512.lab4 I/OpenGLRenderer: Initialized EGL, version 1.4
  04-21 03:26:23.564 25927-25954/uk.ac.man.cs.COMP28512.lab4 D/mali_winsys: new_window_surface returns 0x3000,  [1440x2560]-format:1
  04-21 03:26:23.584 25927-25927/uk.ac.man.cs.COMP28512.lab4 V/ActivityThread: updateVisibility : ActivityRecord{5c6e414 token=android.os.BinderProxy@7720f66 {uk.ac.man.cs.COMP28512.lab4/uk.ac.man.cs.COMP28512.lab4.MainActivity}} show : true
  04-21 03:26:23.594 25927-25927/uk.ac.man.cs.COMP28512.lab4 I/InjectionManager: dispatchCreateOptionsMenu :uk.ac.man.cs.COMP28512.lab4.MainActivity
  04-21 03:26:23.594 25927-25927/uk.ac.man.cs.COMP28512.lab4 I/InjectionManager: dispatchPrepareOptionsMenu :uk.ac.man.cs.COMP28512.lab4.MainActivity
  04-21 03:26:23.604 25927-25927/uk.ac.man.cs.COMP28512.lab4 W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
  04-21 03:26:23.604 25927-25954/uk.ac.man.cs.COMP28512.lab4 D/libGLESv1: DTS_GLAPI : DTS is not allowed for Package : uk.ac.man.cs.COMP28512.lab4
  04-21 03:26:23.754 25927-25927/uk.ac.man.cs.COMP28512.lab4 D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1
  04-21 03:26:23.764 25927-25927/uk.ac.man.cs.COMP28512.lab4 I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@7720f66 time:49211824
  </code></pre>
  
  <p>Any help is appreciated!</p>