Results 1 to 4 of 4
  1. #1
    Ben1 is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default Generally fairly stuck with classes and methods

    Hey all,

    I haven't properly touched Java in a while and I'm pretty stuck getting back into the basics - classes and method calling etc etc.

    I want to make a program that captures msn packets (done) and compares aspects of them against predefined information - so sender, receiver and message content.

    I have so far:

    Main class:
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package msgmonitor;
    
    import java.io.IOException;
    import jpcap.JpcapCaptor;
    import jpcap.NetworkInterface;
    import jpcap.packet.Packet;
    import msgMonitor.CapturePackets;
    import java.util.Scanner;
    
    /**
     *
     * @author Ben
     */
    public class IMsafe {
    
        public static void main(String[] args) throws IOException {
            NetworkInterface[] devices = JpcapCaptor.getDeviceList();
    
    //for each network interface
            for (int i = 0; i < devices.length; i++) {
                //print out its name and description
                System.out.println(i + ": " + devices[i].name + "("
                        + devices[i].description + ")");
                //print out its datalink name and description
                System.out.println(" datalink: "
                        + devices[i].datalink_name + "(" + devices[i].datalink_description
                        + ")");
    
            }
    
            //What NIC to capture from. - Allows for multiple NIC's or VM NIC's
                System.out.println("Please select the Network Inface you would like to capture from: ");
                Scanner scan = new Scanner(System.in);
                String NICtemp = scan.next();
    
                int NIC = Integer.parseInt(NICtemp);
                int index = NIC;
    
    //        //sets which NIC to capture from
            JpcapCaptor captor = JpcapCaptor.openDevice(devices[index], -1, false, 20);
    
            System.out.println("Now monitoring...");
            System.out.println("______________________");
    
    
            Packet tempPacket = captor.getPacket();
            for (index = 0; index < 10; index++) {
                if (tempPacket != null) {
                    new Thread(new CapturePackets(captor, tempPacket)).start();
                    tempPacket = captor.getPacket();
                    index = 0;
                } else {
                    tempPacket = captor.getPacket();
                    index = 0;
                }
            }
        }
    }
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package msgMonitor;
    
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import jpcap.JpcapCaptor;
    import jpcap.packet.Packet;
    import msgmonitor.KnownSender;
    
    //captor.close();
    /**
     *
     * @author Ben
     */
    public class CapturePackets implements Runnable {
    
        private JpcapCaptor captor;
        private Packet tempPacket;
        public String payload;
    
        public CapturePackets(JpcapCaptor captor, Packet tempPacket) {
            this.captor = captor;
            this.tempPacket = tempPacket;
        }
    
        public void run() {
    
            //tempPacket = the variable used to hold the raw data straight off the NIC
            //packetString = the byte array used to hold the converted "data" from tempPacket
            //payload = the data as a string - final var for this stage.
    
            byte[] packetString = tempPacket.data;
            String payloadTemp = new String(packetString);
    
            //For Windows Live Messenger 2009
            if (payloadTemp.contains("MSG")
                    && payloadTemp.contains("Content-Type: text/plain")) {
                payload = payloadTemp;
    
                System.out.println(payload);
                System.out.println("______________________");
    
                System.out.println(payloadTemp);
            }
    
            //For Windows Live Messenger 2009
            if (payloadTemp.contains("Message-Type: Text")
                    && payloadTemp.contains("Content-Type: text/plain")) {
                payload = payloadTemp;
    
                //System.out.println(payload);
                //System.out.println("______________________");
            }
    
            if (payload == null) {
                return;
            } else {
            }
            //}
            //Breaking up the payload
    
            //public void splitPacket(String to, String from, String message) {
    
            //Get the message destination
            String to, from, message;
            int start = payload.indexOf("To: 1:");
            if (start == -1) {
                try {
                    BufferedWriter out = new BufferedWriter(new FileWriter("Error File.txt", true));
                    out.write("There was an error processing the data. The packet data is... \r\n");
                    out.write(payload + "\r\n");
                    out.write("______________________ \r\n");
                    out.close();
                } catch (IOException e) {
                }
            }
    
            int end = payload.indexOf("\r\n", start);
            if (start == -1) {
                try {
                    BufferedWriter out = new BufferedWriter(new FileWriter("Error File.txt", true));
                    out.write("There was an error processing the data. The packet data is... \r\n");
                    out.write(payload + "\r\n");
                    out.write("______________________ \r\n");
                    out.close();
                } catch (IOException e) {
                }
            }
            to = payload.substring(start + 6, end);
            System.out.println("To: " + to);
    
    
            //Get the message source
            start = payload.indexOf("From: 1:");
            if (start == -1) {
                try {
                    BufferedWriter out = new BufferedWriter(new FileWriter("Error File.txt", true));
                    out.write("There was an error processing the data. The packet data is... \r\n");
                    out.write(payload + "\r\n");
                    out.write("______________________ \r\n");
                    out.close();
                } catch (IOException e) {
                }
            }
            end = payload.indexOf(";", start);
            if (start == -1) {
                try {
                    BufferedWriter out = new BufferedWriter(new FileWriter("Error File.txt", true));
                    out.write("There was an error processing the data. The packet data is... \r\n");
                    out.write(payload + "\r\n");
                    out.write("______________________ \r\n");
                    out.close();
                } catch (IOException e) {
                }
            }
            from = payload.substring(start + 8, end);
            System.out.println("From: " + from);
    
            //Get the message
            start = payload.indexOf("X-MMS-IM-Format");
            if (start == -1) {
                try {
                    BufferedWriter out = new BufferedWriter(new FileWriter("Error File.txt", true));
                    out.write("There was an error processing the data. The packet data is... \r\n");
                    out.write(payload + "\r\n");
                    out.write("______________________ \r\n");
                    out.close();
                } catch (IOException e) {
                }
            }
    
            start = payload.indexOf("\r\n\r\n", start);
            if (start == -1) {
                try {
                    BufferedWriter out = new BufferedWriter(new FileWriter("Error File.txt", true));
                    out.write("There was an error processing the data. The packet data is... \r\n");
                    out.write(payload + "\r\n");
                    out.write("______________________ \r\n");
                    out.close();
                } catch (IOException e) {
                }
            }
            start += 4;
            message = payload.substring(start);
            System.out.println("Message: " + message);
            System.out.println("______________________");
    
    
            //***
            //set setters
            KnownSender knownSender = new KnownSender();
    
    //        knownSender.setTo(to);
            knownSender.setFrom(from);
            
            KnownSender knownsenderObject = new KnownSender();
            KnownSender.test();
    
        }
    }
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package msgmonitor;
    
    public class KnownSender {
    
        String fromTemp;
        //get From
    
        public String getFrom() {
            return fromTemp;
        }
    
        public void setFrom(String from) {
            this.fromTemp = from;
        }
        //set getters
        String from = getFrom();
    
        public void test() {
            System.out.println("The sender of this message is: " + fromTemp);
        }
    }

    I have the program capturing packets and printing them out in formatted way. But I want to pass the "to" and "from" into the last class detailed for analysis. But at the moment I'm getting "non-static method test() cannot be referenced from a static context".

    Any help getting me out of this confusion would be greatly appreciated. If I've confused anyone let me know and I'll try and clarify.

    Thanks
    Ben

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default

    Honestly, you might want to start quite a bit smaller. Here's a good beginner tutorial, give it a read-through: Trail: Learning the Java Language (The Java™ Tutorials)

  3. #3
    Ben1 is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    Ok please ignore this whole thread. I was being an idiot. Got it working :-)

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default

    Cool, glad you got it working. Don't worry, being an idiot sometimes is part of being a programmer. You might consider posting your solution (or part of it, or a description of it) so people with similar problems in the future can benefit from what you did.

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. What classes and methods to use?
    By chyrl in forum Advanced Java
    Replies: 21
    Last Post: 06-20-2010, 01:45 PM
  3. using methods between classes
    By soccer_kid_6 in forum New To Java
    Replies: 2
    Last Post: 04-18-2010, 04:14 AM
  4. Classes and Methods help
    By border9 in forum New To Java
    Replies: 5
    Last Post: 01-30-2009, 07:51 PM
  5. [SOLVED] Using Code Throughout 5 Classes-Stuck
    By Bascotie in forum New To Java
    Replies: 46
    Last Post: 06-06-2008, 06:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •