开发者

Java DataStructure for writing 4 pieces of information

开发者 https://www.devze.com 2023-01-10 19:41 出处:网络
I need to extract two pieces of information about two IP addresses and then write those informatio开发者_如何学Cn plus two addresses.

I need to extract two pieces of information about two IP addresses and then write those informatio开发者_如何学Cn 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


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();


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.


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.


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).


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..


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
0

精彩评论

暂无评论...
验证码 换一张
取 消