AllExperts > Java 
Search      
Java
Volunteer
Answers to thousands of questions
 Home · More Java Questions · Answer Library  · Encyclopedia ·
More Java Answers
Question Library

Ask a question about Java
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Artemus Harper
Expertise
I have a BS in computer science and am working towards a Masters degree.

Experience
I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. knowledge of Java bytecode and annotations. Basics in c++ and c#

Education/Credentials
BS in Computer Science at Central Washington University

 
   

You are here:  Experts > Computing/Technology > Focus on Java > Java > monitoring system

Java - monitoring system


Expert: Artemus Harper - 3/22/2007

Question
QUESTION: hello Artemus Harper..i hope u can help me..i am a new user java
programming.so lot of thing that i still cant understand. i have been develop a
monitoring system. i use a proxy function to get data that i want for
monitoring. for the proxy, i develop it by java. but for the system
interface, where will show all data captured, i use php. both of this
programming use the same database. this 2 programming doesnt have any
related. it just use the same database.java for proxy, get all data for
monitoring, and php for interface, show all data. so i dont think that will
be a problem. i dont have any problem with the php. i have done part of
the system, where i can monitor user activities. i can know the site
that user enter, the file that user download and the protocol that being
used. but i cant get the user ip address and the site ip address..here
i give u the full coding for your reference..

import java.net.*;
import sun.net.NetworkServer;
import java.io.*;
import java.util.*;
import java.awt.*;


public class FTPMonitoring extends NetworkServer
{
   public static String name;
   public Socket clientsocket;
   public DataOutputStream outbound1;
   public BufferedReader inbound1;
   public BufferedReader inbound2;
   
   public static void main(String args[])
  {
     name="";
       int port=1800;
       try
       {
          port=Integer.parseInt(args[0]);
       }
       
       catch(Exception e){}
     
     try
     {
          FTPMonitoring es=new FTPMonitoring();
           es.startServer(port);
           System.out.println("FTP monitoring start at " +new
Date().toString()+" with port "+ port);
       }

       catch(IOException e)
       {
          System.err.println("fail to start");
       }
   }
  
  public void serviceRequest() throws IOException
  {
      String test1="",urlstring="",req,req1="";
      int j=0;
      
      DB db=new DB();

      try
      {
         db.connect();
          DataInputStream msg =new
DataInputStream(clientSocket.getInputStream());
            String test=msg.readLine();
          byte b[]=new byte[test.indexOf("HTTP/")];
       
          if(test.charAt(0)=='G')
        {
             test.getBytes(4,b.length,b,0);
              req="get";
              req1="GET ";
          }
       
          else
        {
             test.getBytes(5,b.length,b,0);
              req="post";
              req1="POST ";
          }
       
          int i=0,cnt=0;
       
          for(i=0;i<b.length-4;i++)
        {
             urlstring+=(char)b[i];
          }

          URL ur=new URL(urlstring);
          clientsocket= new
Socket(InetAddress.getByName(ur.getHost()),21);
          System.out.println("New Connection \n");
          System.out.println(InetAddress.getByName(ur.getHost()));
          
          outbound1=new
DataOutputStream(clientsocket.getOutputStream());
       
          if(req.equals("post")==false)
        {
             outbound1.writeBytes(req1+ur.getFile()+"\r\n");
          
              while((test1=msg.readLine())!=null)
              {
                 if(test1.equals("") || test1.equals(" ") ||
test1.equals("\r\n\r\n"))
              {
                     outbound1.writeBytes("\r\n\r\n");
                     
                      System.out.println("\nRequest to  " + ur.getHost()
+" has been sent\n\n");
                      System.out.println("URL:" + ur.toString()+"\n\n");
                      System.out.println("Port:"+ ur.getPort()+"\n\n");
                      System.out.println("File name:" +
ur.getFile()+"\n\n");
                      System.out.println("Time:   " + new
Date().toString() + "\n\n");
                      System.out.println("Protocol:"+
ur.getProtocol()+"\n\n");
                      db.addData(ur.toString(),ur.getHost(),
ur.getPort(), ur.getFile(), new Date().toString(), ur.getProtocol());
                      break;
                      
                    }
                 
                  outbound1.writeBytes(test1+"\r\n");
               }
            
               int ch1;
            
              inbound1=new BufferedReader(new
InputStreamReader(clientsocket.getInputStream()));
           
              while((ch1=inbound1.read())!=-1)
           {
                 clientOutput.write(ch1);
                  clientOutput.flush();
              }
          }
       
          else
        {
             outbound1.writeBytes(req1+ur.getFile()+"\r\n");
          
              while((test1=msg.readLine())!=null)
           {
                 if(test1.equals("") || test1.equals(" ") ||
test1.equals("\r\n\r\n"))
              {
                     outbound1.writeBytes("\r\n");
                      int ch3;
                      int total=msg.available();
                      
                      for(int l=0;l<total;l++)
                 {
                         ch3=msg.read();
                          outbound1.write(ch3);
                          outbound1.flush();
                      }
                   
                      outbound1.writeBytes("\r\n");
                      outbound1.flush();
                      System.out.println("Get Request to Host " +
ur.getHost() +" has been sent");
                      break;
                   
                    }
                    
                  outbound1.writeBytes(test1+"\r\n");
               
              }
           
              inbound1=new BufferedReader(new
InputStreamReader(clientsocket.getInputStream()));
              int fin;
           
              while((fin=inbound1.read())!=-1)
           {
                 clientOutput.write(fin);
                  clientOutput.flush();
              }
            }
           
          inbound1.close();
          outbound1.close();
          clientsocket.close();
     }
   
      catch(IOException e)
     {
        System.out.println(e);
          db.disconnect();
       }
  }
}


actually some this coding i cant understand properly, because some part
i get it from source code. here is the database coding:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class DB
{
  
  protected Connection con;
  protected Statement stmt;
  protected ResultSet rs;   
  String query;
  
  public static void main (String [] args) throws Exception
  {
  }
     
  public void connect()
  {
     try
     {
        Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
        con = DriverManager.getConnection ("jdbc:odbc:ftp");
        stmt = con.createStatement ();
              
     //   System.out.println("Connection to database succesfully");
     }

     catch(Exception e)
     {
        System.out.println("Connection to database fail");
     }
  }
     
  
  public void addData(String url,String hostName,Integer port, String
fileName, String date, String protocol)
  {
     try
     {
        //System.out.println(url+" "+hostName+" " +port +" "+fileName+" "
+date);
           
        query = "insert into main(url,hostname,port,filename,date,protocol)
values('"+url+"','"+ hostName+"','"+ port
+"','"+fileName+"','"+date+"','"+protocol+"')";
        stmt.executeUpdate(query);

        System.out.println("Data has been succesfully add to database");
     }
     catch(Exception e)
     {
        System.out.println("error add data");
     }   
  }

  public void disconnect()
  {
     try
     {                           
        con.close ();   
        System.out.println(" Connection has been disconnect");
     }

     catch(Exception e)
     {
        System.out.println("fail to disconnect");
     }
  }
}


i hope u can help me.. :-)"

ANSWER: The user ip address (as the one the user is connecting from) is always the same, so the Socket doesn't bother to tell you that.

To get the user's IP address you can use:
InetAddress localHost = InetAddress.getLocalHost();

you can then call toString() to get a string representation, or getAddress() to get a byte[] representation of it (the size of the byte[] depends if the computer is using IPv4 or IPv6).

To resolve a host's IP address you can use:
InetAddress siteAddress = InetAddress.getByName(hostName);

and then use one of the above techniques to get a representation of the ip address.

---------- FOLLOW-UP ----------

QUESTION: thanks you so much..i get it now..
actually i want to monitor only FTP, without bother any HTTP activites. how can my proxy bypass the HTTP activities? i mean, when user use HTTP service it will bypass the proxy, but when user use FTP service, the connection will be through by proxy. so the user who access HTTP, they will not bothered by proxy function.
ANSWER: Since this only listenes on a certian port this will not be an issue. You can sliently do nothing if an HTTP request comes though, but you are already in the proxy by the time you can realize that this is an HTTP request.

---------- FOLLOW-UP ----------

QUESTION: :-) thanks a lot..u give me an idea..now i have done some part that stuck before..thanks so much..
now i want make a filterring..the proxy will filter by sitename that has been set in database. the proxy will refer to database, if the name of site has exist in database(all site name that exist in database automatically been set to blocked by admin), then user cant access it..i really dont know how to start. so i hope u can guide me step by step.

Answer
Once you got the proxy up and running, you just do a lookup into the data base when a connection to a site occurs. If the site is blocked then you redirect them to an error page (or you could just close the connection).

If you are not carefull you could greatly impact performance. Asking the data base for each request is not a good idea, at a minimum you should cache the results so multiple calls to the same website succeede without having to re ask the database. Or you could just download the entire list ahead of time. The tradeoff however is that the admin cannot make a quick change and have everyone notice that imediatly.

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.