title: Android get Tcp connection log
body: <p>I have some trouble in getting Tcp connection log of all apps. Basically, I just read the log file "/proc/Pid/net/tcp" as used here:
  <a href="http://code.google.com/p/iptableslog/source/browse/src/com/googlecode/networklog/NetStat.java?r=60cb640ac27f8b4fb06f11d9d81c94591a531862" rel="nofollow">http://code.google.com/p/iptableslog/source/browse/src/com/googlecode/networklog/NetStat.java?r=60cb640ac27f8b4fb06f11d9d81c94591a531862</a></p>
  
  <p>But all the destination ip address are 0.0.0.0, though some apps are connecting to Internet.</p>
  
  <p>Here is my source code:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.FileReader;
  import java.net.InetAddress;
  import java.net.UnknownHostException;
  import java.util.ArrayList;
  import java.util.List;
  
  import android.os.Bundle;
  import android.app.Activity;
  import android.app.ActivityManager;
  import android.app.ActivityManager.RunningAppProcessInfo;
  import android.content.Context;
  import android.content.Intent;
  import android.hardware.Camera;
  import android.util.Log;
  import android.view.Menu;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.TextView;
  
  public class MainActivity extends Activity {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          final TextView textmpid = (TextView) findViewById(R.id.textView2);
  
          Button buttonLoadTrafficStats = (Button) findViewById(R.id.button1);
          buttonLoadTrafficStats.setOnClickListener(new OnClickListener() {
  
              public void onClick(View v) {
  
                  ActivityManager mActivityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
                  List&lt;RunningAppProcessInfo&gt; mRunningProcess = mActivityManager.getRunningAppProcesses();
                  int i = 1;
                  int a;
                  String b;
                  int c;
                  for (ActivityManager.RunningAppProcessInfo amProcess : mRunningProcess){
                      //Get name, Pid, Uid of all running process
                      System.out.println("Application: " +i+ " PID: " + amProcess.pid+ " (processName= " + amProcess.processName + " UID= "+amProcess.uid+")");
                      Log.i("Application", (i++) + "PID: " + amProcess.pid + "(processName=" + amProcess.processName + "UID="+amProcess.uid+")");
                      i++;
                      textmpid.setText("PID: "+ String.valueOf(amProcess.pid));
                      a = amProcess.pid;
                      b = String.valueOf(amProcess.pid);
  
                      ArrayList&lt;Connection&gt; connections = new ArrayList&lt;Connection&gt;();
  
                          try {
                              BufferedReader in = new BufferedReader(new FileReader("/proc/" + a + "/net/tcp"));
                              String line = b;
                              int z; 
                              while((line = in.readLine()) != null) {
                                  System.out.println(" tcp Netstat line: " + line);
                                  line = line.trim();
                                  String[] fields = line.split("\\s+", 10);
                                  int fieldn = 0;
                                  for(String field : fields) {
                                      System.out.println(" tcp Field " + (fieldn++) + ": [" + field + "]");
                                      int m = fieldn; 
                                      String n = field;
                                  }
  
                                  if(fields[0].equals("sl")) {
                                      continue;
                                  }
                                    Connection connection = new Connection();
  
                                      String src[] = fields[1].split(":", 2);
                                      String dst[] = fields[2].split(":", 2);
  
                                      System.out.println(" tcp Netstat: fields[1] " + fields[1]+"  fields[2] " + fields[2]);
                                      System.out.println(" tcp Netstat: src[0] " + src[0] +" src[1] " + src[1]);
                                      connection.src = getAddress(src[0]);
                                      System.out.println(" tcp Netstat: connection.src " + connection.src);
                                      connection.spt = String.valueOf(getInt16(src[1]));
                                      System.out.println(" tcp Netstat: connection.spt " + connection.spt);
                                      connection.dst = getAddress(dst[0]);
                                      System.out.println(" tcp Netstat: connection.dst " + connection.dst);
                                      connection.dpt = String.valueOf(getInt16(dst[1]));
                                      System.out.println(" tcp Netstat: connection.dpt " + connection.dpt);
                                      connection.uid = fields[7];
                                      System.out.println(" tcp Netstat: connection.uid " + connection.uid);
  
                                      connections.add(connection);
  
                              }
  
                          }
                          catch(Exception e) {
                              System.out.println(" checknetlog() Exception: " + e.toString());
                          }                                               
                      }
  
                  }
  
          });
  
      }
  
        public class Connection {
          String src;
          String spt;
          String dst;
          String dpt;
          String uid;
        }
  
        final String states[] = { "ESTBLSH",   "SYNSENT",   "SYNRECV",   "FWAIT1",   "FWAIT2",   "TMEWAIT",
          "CLOSED",    "CLSWAIT",   "LASTACK",   "LISTEN",   "CLOSING",  "UNKNOWN"
        };
  
        private final String getAddress(final String hexa) {
          try {
            final long v = Long.parseLong(hexa, 16);
            final long adr = (v &gt;&gt;&gt; 24) | (v &lt;&lt; 24) |
              ((v &lt;&lt; 8) &amp; 0x00FF0000) | ((v &gt;&gt; 8) &amp; 0x0000FF00);
            return ((adr &gt;&gt; 24) &amp; 0xff) + "." + ((adr &gt;&gt; 16) &amp; 0xff) + "." + ((adr &gt;&gt; 8) &amp; 0xff) + "." + (adr &amp; 0xff);
          } catch(Exception e) {
            Log.w("NetworkLog", e.toString(), e);
            return "-1.-1.-1.-1";
          }
        }
  
        private final String getAddress6(final String hexa) {
          try {
            final String ip4[] = hexa.split("0000000000000000FFFF0000");
  
            if(ip4.length == 2) {
              final long v = Long.parseLong(ip4[1], 16);
              final long adr = (v &gt;&gt;&gt; 24) | (v &lt;&lt; 24) |
                ((v &lt;&lt; 8) &amp; 0x00FF0000) | ((v &gt;&gt; 8) &amp; 0x0000FF00);
              return ((adr &gt;&gt; 24) &amp; 0xff) + "." + ((adr &gt;&gt; 16) &amp; 0xff) + "." + ((adr &gt;&gt; 8) &amp; 0xff) + "." + (adr &amp; 0xff);
            } else {
              return "-2.-2.-2.-2";
            }
          } catch(Exception e) {
            Log.w("NetworkLog", e.toString(), e);
            return "-1.-1.-1.-1";
          }
        }
  
        private final int getInt16(final String hexa) {
          try {
            return Integer.parseInt(hexa, 16);
          } catch(Exception e) {
            Log.w("NetworkLog", e.toString(), e);
            return -1;
          }
        }
  }
  </code></pre>
  
  <p>And I also added the permission "uses-permission android:name="android.permission.INTERNET".</p>
  
  <p>Could someone kindly point out where I did wrong?</p>