javanetwork-programmingwinpcapjpcap

Unable to receive packet using jpcap in WiFi network


I want to send and receive packets in WiFi network. using the following codes

To send packet.

int id = Integer.parseInt(text_device.getText().trim());
JpcapCaptor jpcap = JpcapCaptor.openDevice(devices[id], 65535, true, 20);
JpcapSender sender = jpcap.getJpcapSenderInstance();
TCPPacket packet = new TCPPacket(210, 210, 0l, 12l, true, true, true, true, true, true, true, true, 1, 2);
packet.setIPv4Parameter(0, true, true, true, 0, true, true, true, 0, 1010101, 100, IPPacket.IPPROTO_TCP, InetAddress.getLocalHost(), InetAddress.getByName(text_ip.getText().trim()));
packet.data = "Sample Data".getBytes();
Date date = new Date();
packet.usec = date.getTime();
packet.sec = date.getTime();
EthernetPacket ether = new EthernetPacket();
ether.src_mac = new byte[]{(byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 1};
ether.dst_mac = new byte[]{(byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255};
ether.frametype = EthernetPacket.ETHERTYPE_IP;
packet.datalink = ether;
sender.sendPacket(packet);

To receive packet, i write this code in public void receivePacket(Packet packet)

TCPPacket tcp = (TCPPacket) packet;
String src = tcp.src_ip.getHostAddress();
int src_port = tcp.src_port;
String dst = tcp.dst_ip.getHostAddress();
int dst_port = tcp.dst_port;
String data = new String(tcp.data);
System.out.println("" + data + "");

It works when i connect two machines with LAN, but unable to receive packet when i connect with WiFi.

Can anyone suggest me, whats going wrong??


Solution

  • I suspect that the answer is related to this quote from the JPCAP FAQ:

    Q: Can I capture packets from a WiFi (802.11) network interface?

    The answer depends on what Operating System you are running and which WiFi devices your system has. On Windows, you may not be able to capture packets from some WiFi devices. Linux or BSD may have higher probability of supporting packet capturing using WiFi devices.

    You may have better success on the Google discussion group for JPcap if you share your precise adapter type and windows version details.