【发布时间】:2018-06-11 01:00:02
【问题描述】:
我正在开发一个带有 VPN 服务的数据包嗅探器 Android 应用程序,但我在将数据包从 Fileinputstream 读取到 bytebuffer 时遇到了麻烦。问题是每次我将数据包写入字节缓冲区时,字节缓冲区内都没有任何数据。请帮帮我。谢谢
FileInputStream in = new FileInputStream(traffic_interface.getFileDescriptor());
FileOutputStream out = new FileOutputStream(traffic_interface.getFileDescriptor());
DatagramChannel tunnel = DatagramChannel.open();
if (!protect(tunnel.socket())) {throw new IllegalStateException("Cannot protect the tunnel");}
tunnel.connect((new InetSocketAddress("127.0.0.1",0)));
tunnel.configureBlocking(false);
int n = 0;
while (!Thread.interrupted()){
packet = ByteBuffer.allocate(65535);
int packet_length = in.read(packet.array());
Log.d("UDPinStream","UDP:" +packet_length);
if(packet_length != -1 && packet_length > 0){
Log.d("UDPinStream","UDP:" + packet_length);
Log.d("UDPinStream","packet:" + packet);
packet.clear();
}
问题出现在以下代码中
int packet_length = in.read(packet.array());
if(packet_length != -1 && packet_length > 0){
Log.d("UDPinStream","UDP:" + packet_length);
Log.d("UDPinStream","packet:" + packet);
packet.clear();
}
虽然它成功地从隧道中读取了数据包(packet_length >0),但Bytebuffer中也没有数据packet bytebuffer的位置没有改变。 java.nio.HeapByteBuffer[pos=0 lim=65535 cap=65535]
【问题讨论】:
标签: java android fileinputstream bytebuffer