Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7586151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:21:16+00:00 2026-05-30T19:21:16+00:00

Recently, I’m using Jnetpcap to send/receive raw packet over network. Jnetpcap provides sending packets

  • 0

Recently, I’m using Jnetpcap to send/receive raw packet over network.

Jnetpcap provides sending packets by Pcap.sendPacket(). This method gets raw buffer or bytes to send.

On the other hand, there is org.jnetpcap.protocol.* classes which wraps protocol headers, and we can use them to decode captured packets.

When I use below code to make a Ip4 packet, It causes NullPointerException:

import org.jnetpcap.protocol.network.Ip4;

public class Test {

    public static void main(String[] args) {

        Ip4 ip4 = new Ip4();

        ip4.ttl(10);

    }
}

Error:

Exception in thread "main" java.lang.NullPointerException
    at org.jnetpcap.nio.JBuffer.check(Unknown Source)
    at org.jnetpcap.nio.JBuffer.setUByte(Unknown Source)
    at org.jnetpcap.protocol.network.Ip4.ttl(Unknown Source)
    at jaeger.Test.main(Test.java:17)

How can I build that packet and then send it by Pcap.sendPacket()?

Note: I’m really not interested in preparing packets byte by byte… C/C++ libpcap and Jpcap have working functionality, but I want use Jnetpcap!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:21:18+00:00Added an answer on May 30, 2026 at 7:21 pm

    1) The code throws an exception because the wrapper classes only work using previously allocated buffers, due the fact that the primary purpose of the library is to analyze buffers of captured packets. So, it’s necessary to allocate a buffer before using them.

    2) The packets must be builded supplying all necessary bytes. But code can be written so that only a few bytes are really necessary (see above).

    3) sendPacket expects an entire packet, a full Ethernet frame. So, Ethernet, IP, TCP headers and Payload must be written to a buffer.

    4) The main idea, to allow you to use wrapper classes, is to allocate a buffer and then left the library scan it to discover headers, but a minimum of information (bytes) must be provided.

    JMemoryPacket packet = new JMemoryPacket(packetSize);
    packet.order(ByteOrder.BIG_ENDIAN); 
    

    Ethernet frame needs a protocol type (0x0800) at position 12:

    packet.setUShort(12, 0x0800);
    packet.scan(JProtocol.ETHERNET_ID); 
    

    After scan, an Ethernet instance can be retrieved and setters used:

    Ethernet ethernet = packet.getHeader( new Ethernet() );  
    ethernet.destination(...);
    ...
    

    IP4 header needs version (0x04) and size (0x05) at position 14:

    packet.setUByte(14, 0x40 | 0x05);
    packet.scan(JProtocol.ETHERNET_ID);  
    
    Ip4 ip4 = packet.getHeader( new Ip4() );
    ip4.type(0x06); //TCP
    ip4.length( packetSize - ethernet.size() );
    ip4.ttl(...);  
    ...
    

    TCP header needs size (0x50):

    packet.setUByte(46, 0x50);
    packet.scan(JProtocol.ETHERNET_ID);  
    
    Tcp tcp = packet.getHeader( new Tcp() );  
    tcp.seq(...); 
    ...
    

    So, Payload:

    Payload payload = packet.getHeader( new Payload() );
    payload.set...(...);
    ...
    

    And finally:

    pcap.sendPacket( ByteBuffer.wrap( packet.getByteArray(0, packet.size() )  );
    

    5) All necessary bytes can be written at one time to avoid so many calls to the scan method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I'm doing some work on RTMP streaming, that is using Flowplayer to integrate
recently I started using storyboard and I've the following situation: I want to set
Recently I had a problem using (pipe |-) when I wanted to communicate between
Recently I was trying to determine the time needed to calculate a waveform using
Recently, I like using CSS-Table Layouts more and more. When I had another issue
Recently I have migrated my project from SQLServer 2000 to MySQL 5.2 using MySQL
Recently I'm using IBM Websphere Server for some estimates. Include: - WebSphere AS Community
Recently Jeff has posted regarding his trouble with database deadlocks related to reading. Multiversion
Recently I have been investigating the possibilities of caching in ASP.NET. I rolled my
Recently, I started changing some of our applications to support MS SQL Server as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.