我在获取所有应用的Tcp连接日志方面遇到了一些麻烦。基本上,我只读了这里使用的日志文件“/ proc / Pid / net / tcp”: http://code.google.com/p/iptableslog/source/browse/src/com/googlecode/networklog/NetStat.java?r=60cb640ac27f8b4fb06f11d9d81c94591a531862
但是所有目标IP地址都是0.0.0.0,尽管有些应用程序正在连接到Internet。
这是我的源代码:
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<RunningAppProcessInfo> 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<Connection> connections = new ArrayList<Connection>();
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 >>> 24) | (v << 24) |
((v << 8) & 0x00FF0000) | ((v >> 8) & 0x0000FF00);
return ((adr >> 24) & 0xff) + "." + ((adr >> 16) & 0xff) + "." + ((adr >> 8) & 0xff) + "." + (adr & 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 >>> 24) | (v << 24) |
((v << 8) & 0x00FF0000) | ((v >> 8) & 0x0000FF00);
return ((adr >> 24) & 0xff) + "." + ((adr >> 16) & 0xff) + "." + ((adr >> 8) & 0xff) + "." + (adr & 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;
}
}
}
我还添加了权限“uses-permission android:name =”android.permission.INTERNET“。
有人可以指出我做错了吗?
答案 0 :(得分:0)
哦,当我检查这个日志“/ proc / Pid / net / tcp6”时,我找到了正确的目标IP地址。似乎连接类型是tcp6而不是tcp。