Discussion Locked

Help packet sending with Jpcap(netresearch)

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am a beginner in Jpcap.
My problem is as follows:-
i am trying to send a TCP packet but unable to receive it.
The code for sending Tcp packet is as follows.
I am working on Windows XP.
hoping for reply.

import jpcap.*;
import jpcap.packet.*;
import java.net.InetAddress;
import java.lang.Object;

class SendPacket
{

public static void main(String args[]) throws Exception
{
int index=0;

//Obtain the list of network interfaces
NetworkInterface[] devices = JpcapCaptor.getDeviceList();

//open a network interface to send a packet to
JpcapSender sender=JpcapSender.openDevice(devices[index]);

//create a TCP packet with specified port numbers, flags, and other parameters
TCPPacket p=new TCPPacket(12,34,56,78,false,false,false,false,true,true,true,true,10,10);

//specify IPv4 header parameters

p.setIPv4Parameter(0,false,false,false,0,false,false,false,0,65,128,IPPacket.IPPROTO_TCP,InetAddress.getByName("192.168.15.16"),InetAddress.getByName("192.168.15.15"));

//set the data field of the packet
p.data=("ABCD").getBytes();

//create an Ethernet packet (frame)
EthernetPacket ether=new EthernetPacket();
//set frame type as IP
ether.frametype=EthernetPacket.ETHERTYPE_IP;
//set source and destination MAC addresses

String strdst = new String("00:21:9B:D7:98:54");
ether.dst_mac = strdst.getBytes();
String strsrc = new String("00:0C:F1:DE:00:D0");
ether.src_mac = strsrc.getBytes();

//set the datalink frame of the packet p as ether
p.datalink=ether;

//send the packet p
for(int k=0;k>=0;k++)
{
sender.sendPacket(p);
}
sender.close();
}

}

5 replies

by robo_dev

When you say you are 'trying to send but cannot receive'

Do you mean that your send program is not working or your receive program is not working?

Your example seems to indicate that your send program is not right.

Do other WinPcap programs, such as WireShark or Nmap work on your PC?
Do other JPCAP programs work on your PC?
Do you have admin privileges on the PC?

by aniketsharma190

The problem is when i execute the send program on my pc, packets don't reach the destination ,instead they are captured on the same machine(i am using following receive program to capture).I have also verified the IP/MAC address.

I have administrator privileges on my pc.
and i tried wireshark it works on my pc properly.
Jpcap also works.

import jpcap.*;
import jpcap.packet.*;
import java.io.*;
import java.net.InetAddress;

class PacketLengthPrinter implements PacketReceiver
{
static int i=0;
public void receivePacket(Packet pack)
{

// ports
/*
TCPPacket p=(TCPPacket)pack;
int src_port=p.src_port;
int dst_port=p.dst_port;
System.out.println("Source port::\t"+src_port);
System.out.println("Dest PORT::\t"+dst_port);

// ip addr
InetAddress src_ip=p.src_ip;
InetAddress dst_ip=p.dst_ip;
System.out.println("Source ip::\t"+src_ip);
System.out.println("Dest IP::\t"+dst_ip);

//identification
int ident=p.ident;
System.out.println("IDENTIFICATION::"+ident);

i++;
*/
System.out.println(""+pack);
}
}

class ReceivePacket
{
public static void main(String args[])
{
NetworkInterface devices[]=new NetworkInterface[10];
devices=JpcapCaptor.getDeviceList();
byte macaddr[]=new byte[10];

int i;
System.out.println("NAMES:");
for(i=0;i

by shruthimelu

i've the same problem..Could you please specify what changes you made in the receiving code to make it work?

by noonz

Hi everyone,

I am trying to capture packets on a windows XP machine installed on a VMware workstation.

Following is the code that works fine on my windows 7 laptop but does not print anything on the current machine. I do not get any compilation or run time errors. I would really apprecicate any help. thanks

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import jpcap.packet.*;
import jpcap.*;

class RecvHttp implements PacketReceiver {
jpcap.NetworkInterface[] devices = JpcapCaptor.getDeviceList();
JpcapSender sender;
int index=0;
public void rcvPacket(String[] args) throws java.io.IOException {

if (args.length < 1) {
System.out.println("Usage: java RecvHttp ");
for (int i = 0; i < devices.length; i++)
System.out.println(i + ":" + devices[i].name + "("
+ devices[i].description + ")");
System.exit(0);
}
index = Integer.parseInt(args[0]);
JpcapCaptor jpcap = JpcapCaptor.openDevice(devices[index], 1028, false, 1);
jpcap.loopPacket(-1, new RecvHttp());
System.out.println("Test");

}

@Override
public void receivePacket(Packet p) {
if (p instanceof TCPPacket){
TCPPacket tcp = (TCPPacket) p;
if (tcp.src_port==80 || tcp.dst_port == 80)
try {

System.out.println(new String(tcp.data,"ASCII"));

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
public static void main(String args[]) throws IOException{
RecvHttp rcv = new RecvHttp();
rcv.rcvPacket(args);
}

}

This discussion is closed to new replies.