Aniket_Sharma 0 Newbie Poster

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.

//code for sending
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();
}

}
//Code for receiving packet

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("\nSource port::\t"+src_port);
		System.out.println("Dest PORT::\t"+dst_port);
		
		// ip address
		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);
			
		//Data
		System.out.println("DATA::");
		for(int cnt=0;cnt<pack.data.length;cnt++)
		{
			byte b=pack.data[cnt];
			System.out.print(""+b);
		}	
		
	}
}

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<devices.length;i++)
		{
			System.out.println((i+1)+"."+devices[i].name);
		}
		System.out.println("DATA LINK NAMES:");
		for(i=0;i<devices.length;i++)
		{
			System.out.println((i+1)+"."+devices[i].datalink_name);
		}
		System.out.println("MAC ADDRESSES:");
		for(i=0;i<devices.length;i++)
		{
			System.out.println((i+1)+".");
			for(byte b:devices[i].mac_address)
			{
				System.out.print(Integer.toHexString(b&0xff)+":");
			}
		}
		System.out.println("IP ADDRESS:SUBNET MASK");
		for(i=0;i<devices.length;i++)
		{
			System.out.println((i+1)+".");
			for(NetworkInterfaceAddress a:devices[i].addresses)
			{
				System.out.print("\t"+a.address+":"+a.subnet);
			}
		}
		System.out.println();
		int index;
		index=0;
		JpcapCaptor captor;
		try
		{
			captor=JpcapCaptor.openDevice(devices[index],65535,false,20);
			System.out.println("NAME::\t"+devices[index].name+"\topened successfuly");
			
			 //print out its MAC address
			System.out.print(" MAC address:");
			for (byte b : devices[index].mac_address)
			System.out.print(Integer.toHexString(b&0xff) + ":");
			System.out.println();
			
			
			System.out.println("USING LOOPPACKET::\n\n");
			//captor.setFilter("ip and tcp", true);
			captor.loopPacket(-1,new PacketLengthPrinter());
			captor.close();
		}
		catch(IOException ioe)
		{
			System.out.println("Unable to open network Device::"+devices[index].name);
		}			
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.