title: Android Socket connection (Permission denied)
body: <p>I'm trying to connect from andriod to simple java server. Server is running and see connection if i do connect from adb <code>adb connect 127.0.0.1:4444</code>
   or even with pc ip <code>adb connect 192.168.1.104:4444</code>
  but android get <code>java.net.SocketException: socket failed: EACCES (Permission denied)</code>. PC and android is in single WIFI router.</p>
  
  <p>MyServer</p>
  
  <pre><code>public class Server {
  
      private static ServerSocket serverSocket;
      private static Socket clientSocket;
      private static InputStreamReader inputStreamReader;
      private static BufferedReader bufferedReader;
      private static String message;
      private static String serverIp = "10.0.2.15";
  
      public static void main(String[] args) {
          try {
  
              serverIp = getServerIp();
  
              if(serverIp != null){
  
                  System.out.println("Listenning on IP:" + serverIp);
  
              }
  
              serverSocket = new ServerSocket(4444); // Server socket
  
          } catch (IOException e) {
              System.out.println("Could not listen on port: 4444  " + e.toString());
          }
  
          System.out.println("Server started. Listening to the port 4444");
  
          while (true) {
              try {
  
  
                  clientSocket = serverSocket.accept(); // accept the client connection
  
  
                  if(clientSocket.getInputStream().read() != -1){
  
                      System.out.println("Socket connection");
  
                  } else {
  
                      System.out.println("Client disconected");
  
                  }
  
                  inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
                  bufferedReader = new BufferedReader(inputStreamReader); // get the client message
                  message = bufferedReader.readLine();
  
                  System.out.println(message);
                  inputStreamReader.close();
                  clientSocket.close();
  
              } catch (IOException ex) {
                  System.out.println("Problem in message reading  " + ex.toString());
              }
          }
  
      }
  
      public static String getServerIp(){
  
          try{
  
              for (Enumeration&lt;NetworkInterface&gt; en = NetworkInterface.getNetworkInterfaces();
                      en.hasMoreElements();){
                  NetworkInterface intf = en.nextElement();
                  for (Enumeration&lt;InetAddress&gt; enumIpAdress = intf.getInetAddresses();
                          enumIpAdress.hasMoreElements();){
  
                          InetAddress inetAddress = enumIpAdress.nextElement();
                          if(!inetAddress.isLoopbackAddress()){
  
                              return inetAddress.getHostAddress().toString();
  
                          }
  
                      }
  
              }
  
          } catch (SocketException e){
  
              System.out.println(e.toString());
  
          }
  
          return null;
      }
  }
  </code></pre>
  
  <p>MyClient connection</p>
  
  <pre><code>public class ServerConnection extends AsyncTask&lt;SocketAddress, String, String&gt; {
  
      @Override
      protected String doInBackground(SocketAddress... params) {
  
          try {
  
              mSocket = new Socket("127.0.0.1", 4444);//test ip
                              //192.168.1.104:4444 - real ip of the server returns the same
  
              PrintWriter printwriter = new PrintWriter(mSocket.getOutputStream(), true);
              printwriter.write("this is test"); // write the message to output stream
  
              printwriter.flush();
              printwriter.close();
              mSocket.close();
  
              Log.i(LOG_TAG, "Socket connecting to " + params[0].toString());
  
              return null;
          } catch (IOException e) {
  
              e.printStackTrace();
              Log.i(LOG_TAG, "===================ERROR====================" +
                      "\n" +
                      e.toString());
  
              return e.toString();
  
          }
      }
  }
  </code></pre>
  
  <p>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.stas.clientserverconnection" &gt;
  
      &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_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=".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>
  
  <p>Exception is on <code>mSocket = new Socket("127.0.0.1", 4444)</code> line</p>
  
  <p>What am i doing wrong?</p>