title: How to correct the Socket Exception in emulator
body: <p>I have written code for Client and server connectivity in android.When I run in Emulator it shows 09-16 19:04:14.783: E/ClientActivity(407): java.net.ConnectException: /10.0.2.15:8080 - Connection refused.I referred many developer.android for redirection.But it didn't work.Can anybody say the reason for this exception and how to fix this issue?
  ServerActivity.java</p>
  
  <pre><code>                    package com.example.network;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.Inet4Address;
  import java.net.InetAddress;
  import java.net.NetworkInterface;
  import java.net.ServerSocket;
  import java.net.Socket;
  import java.net.SocketException;
  import java.util.Enumeration;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.os.Handler;
  import android.util.Log;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class ServerActivity extends Activity {
  
      private TextView serverStatus;
  
      // default ip
      public static String SERVERIP = "";
  
      // designate a port
      public static final int SERVERPORT = 8080;
  
      private Handler handler = new Handler();
  
      private ServerSocket serverSocket;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_server);
          serverStatus = (TextView) findViewById(R.id.textView1);
  
          SERVERIP = getLocalIpAddress();
  
          Thread fst = new Thread(new ServerThread());
          fst.start();
      }
  
      public class ServerThread implements Runnable {
  
          public void run() {
              try {
                  if (SERVERIP != null) {
                      handler.post(new Runnable() {
                          @Override
                          public void run() {
          //                  Toast.makeText(getApplicationContext(), i.getHostAddress.toString(),Toast.LENGTH_LONG ).show();
                              serverStatus.setText("Listening on IP: " + SERVERIP);
                          }
                      });
                      serverSocket = new ServerSocket(SERVERPORT);
                      while (true) {
                          // listen for incoming clients
                          Socket client = serverSocket.accept();
                          handler.post(new Runnable() {
                              @Override
                              public void run() {
                                  serverStatus.setText("Connected.");
                              }
                          });
  
                          try {
                              BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                              String line = null;
                              while ((line = in.readLine()) != null) {
                                  Log.d("ServerActivity", line);
                                  handler.post(new Runnable() {
                                      @Override
                                      public void run() {
                                          // do whatever you want to the front end
                                          // this is where you can be creative
                                      }
                                  });
                              }
                              break;
                          } catch (Exception e) {
                              handler.post(new Runnable() {
                                  @Override
                                  public void run() {
                                      serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones.");
                                  }
                              });
                              e.printStackTrace();
                          }
                      }
                  } else {
                      handler.post(new Runnable() {
                          @Override
                          public void run() {
                              serverStatus.setText("Couldn't detect internet connection.");
                          }
                      });
                  }
              } catch (Exception e) {
                  handler.post(new Runnable() {
                      @Override
                      public void run() {
                          serverStatus.setText("Error");
                      }
                  });
                  e.printStackTrace();
              }
          }
      }
  
      // gets the ip address of your phone's network
      private String getLocalIpAddress() {
          try {
              for (Enumeration&lt;NetworkInterface&gt; en =               NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                  NetworkInterface intf = en.nextElement();
                  for (Enumeration&lt;InetAddress&gt; enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                      InetAddress inetAddress = enumIpAddr.nextElement();
                      if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); }
                  }
              }
          } catch (SocketException ex) {
              Log.e("ServerActivity", ex.toString());
          }
          return null;
      }
  
      @Override
      protected void onStop() {
          super.onStop();
          try {
               // make sure you close the socket upon exiting
               serverSocket.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
      }
  
  }
  ClientActivity.java
          package com.example.network;
  
  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.os.Bundle;
  import android.os.Handler;
  import android.util.Log;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.EditText;
  
  public class ClientActivity extends Activity {
  
      private EditText serverIp;
  
      private Button connectPhones;
  
      private String serverIpAddress = "";
  
      private boolean connected = false;
  
      private Handler handler = new Handler();
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_client);
  
          serverIp = (EditText) findViewById(R.id.editText1);
          connectPhones = (Button) findViewById(R.id.button1);
          connectPhones.setOnClickListener(connectListener);
      }
  
      private OnClickListener connectListener = new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              if (!connected) {
                  serverIpAddress = serverIp.getText().toString();
                  if (!serverIpAddress.equals("")) {
                      Thread cThread = new Thread(new ClientThread());
                      cThread.start();
                  }
              }
          }
      };
  
      public class ClientThread implements Runnable {
  
          public void run() {
              try {
                  InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
                  Log.d("ClientActivity", "C: Connecting...");
                  Socket socket = new Socket(serverAddr, ServerActivity.SERVERPORT);
                  connected = true;
                  while (connected) {
                      try {
                          Log.d("ClientActivity", "C: Sending command.");
                          PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                                      .getOutputStream())), true);
                              // where you issue the commands
                              out.println("Hey Server!");
                              Log.d("ClientActivity", "C: Sent.");
                      } catch (Exception e) {
                          Log.e("ClientActivity", "S: Error", e);
                      }
                  }
                  socket.close();
                  Log.d("ClientActivity", "C: Closed.");
              } catch (Exception e) {
                  Log.e("ClientActivity", "C: Error", e);
                  connected = false;
              }
          }
      }
  }
  </code></pre>
  
  <p>manifest.xml</p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.apache.android.xmpp"&gt;
  
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  
      &lt;application  android:name="XMPPDemosApplication"
              android:label="XMPP Demo"&gt;
          &lt;activity android:name=".XMPPClient" android:label="XMPPClient"&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>