我搜索了这个主题,但发现很少有帮助的细节.有了这些细节,我试着按如下方式编写一些代码.
注意:请将此帖中分享的详细信息与其他帖子进行比较,然后再将其标记为DUPLICATE,而不仅仅是主题.
- (NSArray *)getDataCountersForType:(int)type {
BOOL success;
struct ifaddrs *addrs = nil;
const struct ifaddrs *cursor = nil;
const struct sockaddr_dl *dlAddr = nil;
const struct if_data *networkStatisc = nil;
int dataSent = 0;
int dataReceived = 0;
success = getifaddrs(&addrs) == 0;
if (success) {
cursor = addrs;
while (cursor != NULL) {
if (cursor->ifa_addr->sa_family == AF_LINK) {
dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr;
networkStatisc = (const struct if_data *) cursor->ifa_data;
if (type == WiFi) {
dataSent …Run Code Online (Sandbox Code Playgroud) 我现在一直在寻找我的答案几个小时,我无法弄明白.请帮忙.
我想要做的是使用Android中的VpnService来获取网络数据包,如应用程序tPacketCapture
我开始使用谷歌的ToyVpn示例代码并对其进行修改,因此我不会将数据发送到服务器.但是,我不确定这是否正确.
我的configure方法在调用establish()之前使用binder.addAddress()的wlan ip地址.我正在使用nexus 7并使用"adb shell netcfg | grep wlan0"来获取地址:
wlan0 UP 192.168.0.6/24 0x00001043 10:bf:48:bf:5f:9d
并在我的方法中添加它:
private void configure() throws Exception {
// If the old interface has exactly the same parameters, use it!
if (mInterface != null) {
Log.i(TAG, "Using the previous interface");
return;
}
// Configure a builder while parsing the parameters.
Builder builder = new Builder();
builder.setMtu(1500);
builder.addAddress("192.168.0.6", 24);
try {
mInterface.close();
} catch (Exception e) {
// ignore
}
mInterface = builder.establish();
}
Run Code Online (Sandbox Code Playgroud)
在调用之后,我调用我修改的run方法来传递String而不是InetSocketAddress,这并不重要,因为我没有在任何地方使用它: …