I am sniffering packets on ethernet (eth0) in java with the help of jpcap library... So, In my project I have a JpcapCaptor ...
//Open an interface with openDevice(NetworkInterface intrface, int snaplen, boolean promics, int to_ms)
JpcapCaptor captor=JpcapCaptor.openDevice(devices[index], 65535, false, 20);
captor.setFilter("icmp", true);
captor.loopPacket(-1, new PacketPrinter());
Then I have Packet printer which prints a body of sniffered packets ...
public class PacketPrinter implements PacketReceiver {
@Override
public void receivePacket(Packet packet) {
InputStream is = new ByteArrayInputStream(packet.data);
try {
String sstr = IOUtils.toString(is, "UTF-8");
System.out.println("STRING " + sstr);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String ss;
try {
ss = new String(packet.data, "UTF-8");
System.out.println("STRING " + ss);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
But there is a problem... packet.data is a byte[]... And console prints it as
STRING W�xQ�� !"#$%&'()*+,-./01234567
STRING W�xQ�� !"#$%&'()*+,-./01234567
STRING W�xQ�� !"#$%&'()*+,-./01234567
As I understand it is because of problem with encoding??? What is the solution to decide this problem?