Results 1 to 4 of 4
- 11-30-2010, 04:01 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 16
- Rep Power
- 0
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
- 11-30-2010, 04:10 PM #2
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)
- 11-30-2010, 05:04 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 16
- Rep Power
- 0
Ok please ignore this whole thread. I was being an idiot. Got it working :-)
- 11-30-2010, 05:14 PM #4
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
What classes and methods to use?
By chyrl in forum Advanced JavaReplies: 21Last Post: 06-20-2010, 12:45 PM -
using methods between classes
By soccer_kid_6 in forum New To JavaReplies: 2Last Post: 04-18-2010, 03:14 AM -
Classes and Methods help
By border9 in forum New To JavaReplies: 5Last Post: 01-30-2009, 06:51 PM -
[SOLVED] Using Code Throughout 5 Classes-Stuck
By Bascotie in forum New To JavaReplies: 46Last Post: 06-06-2008, 05:51 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks