Tutorial :Java DataStructure for writing 4 pieces of information



Question:

I need to extract two pieces of information about two IP addresses and then write those information plus two addresses. I was thinking of a Set of Pairs for IP addresses, but by which data structure I can write all these information? Thanks


Solution:1

PcapPacketHandler<String> jPacketHandler = new PcapPacketHandler<String>(){              int totalLength = 0;              public void nextPacket(PcapPacket packet, String user) {                          Ip4 ip = new Ip4();                          String sIP;                          String dIP;                          if (packet.hasHeader(ip) == false){                              return;                          }                           totalLength = totalLength+ ip.getPayloadLength();                           sIP = org.jnetpcap.packet.format.FormatUtils.ip(ip.source());                           dIP = org.jnetpcap.packet.format.FormatUtils.ip(ip.destination());                           System.out.println("SIP = "+sIP+"  "+"destIP = "+dIP+" "+"Payload Length = "+ip.getPayloadLength());                           System.out.println("Total Length = "+totalLength);                  }        };            pcap.loop(10, jPacketHandler, "");          pcap.close();  


Solution:2

Even though this isn't a Javascript app, you could use JSON as it provides a concise way to read/store multiple pieces of data together. Check out the JSON Java Documentation for details about classes, and to download the related source.


Solution:3

If you're just writing the information you could always use a Hashmap. Unless you know what you're planning to do with the data, it's hard to say what's best.


Solution:4

Just make a custom class (POJO), and depending on how you want to write it make it Serializable. That way you can clearly name your fields (and getters and setters) making your code easier to read (and extend).


Solution:5

some thing like this...

class BigClass { //<br>     private IPAdreess  addr1; //<br>     private IPAddress  addr2;  //<br>     private SomeInfo   additionalInfo;//<br>       //implement accessors//<br>       //implement equals, hashCode//<br>     }//<br>  

IPAddress, SomeInfo are your user types. In java, InetAddress represents IP address. This may be much more than your custom type.

The selection of suitable data structure of "set" could be decided many factors.. Do you want to retain the order? Do you populate it via multiple threads? How many entries you expect in the set? 100s? A million?

Why not post your code? It may be easier to give feedback with real code..


Solution:6

I don't quite understand what graph you exactly want to plot. What I would do is

  1. Dump all data into an sql database
  2. Run a query to produce input for your chart.
  3. Plot the chart e.g. with JFreeChart or even Excel

I imagine a query along the line

select source_ip, dest_ip, sum(time), sum(sent_bytes) group by source_ip, dest_ip  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »