3 Replies Latest reply: Nov 26, 2009 1:09 PM by DrClap RSS

    How to continuously monitor ip

    807580
      Hi, I'm trying to build an web application that will continuously monitor the ip of the website and receive if any data comes in.
      I have written java code that will do the task. But i'm not able to call that java code in jsp page.
      Here is my code.

      index.jsp

      <%@page import = "GPRS.KKMultiServer"%>
      <%@page import = "GPRS.KKMultiServerThread"%>
      <%@page import = "GPRS.KnockKnockProtocol"%>

      <%@page contentType="text/html" pageEncoding="UTF-8"%>
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd">

      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>JSP Page</title>
      </head>
      <body>
      <h1>Hello World!</h1>
      <jsp:useBean id="gprs_id" class="GPRS.KKMultiServer" />
      <%
      gprs_id.start();
      %>
      </body>
      </html>


      KKMultiServer.java

      package GPRS;

      import java.net.*;
      import java.io.*;
      import java.util.logging.Level;
      import java.util.logging.Logger;

      public class KKMultiServer {
      public void start()throws IOException
      {
      ServerSocket serverSocket = null;
      boolean listening = true;

      try {
      serverSocket = new ServerSocket(8444);
      } catch (IOException e) {
      System.err.println("Could not listen on port: 4444.");
      System.exit(-1);
      }

      while (listening)
           new KKMultiServerThread(serverSocket.accept()).start();

      serverSocket.close();
      }
      public static void main(String[] args) {
      KKMultiServer kk = new KKMultiServer() ;
      try {
      kk.start();
      } catch (IOException ex) {
      System.err.println(ex);
      }
      }
      }


      KKMultiServerThread.java

      package GPRS;

      import java.net.*;
      import java.io.*;

      public class KKMultiServerThread extends Thread {
      private Socket socket = null;

      public KKMultiServerThread(Socket socket) {
           super("KKMultiServerThread");
           this.socket = socket;
      }

      public void run() {

           try {
           PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
           BufferedReader in = new BufferedReader(
                          new InputStreamReader(
                          socket.getInputStream()));

           String inputLine, outputLine;
           KnockKnockProtocol kkp = new KnockKnockProtocol();
           outputLine = kkp.processInput(null);
           out.println(outputLine);

           while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
      outputLine = kkp.processInput(inputLine);
                out.println(outputLine);
                if (outputLine.equals("Bye"))
                break;
           }
           out.close();
           in.close();
           socket.close();

           } catch (IOException e) {
           e.printStackTrace();
           }
      }
      }


      KnockKnockProtocol.java

      package GPRS;

      import java.net.*;
      import java.io.*;

      public class KnockKnockProtocol {
      private static final int WAITING = 0;
      private static final int SENTKNOCKKNOCK = 1;
      private static final int SENTCLUE = 2;
      private static final int ANOTHER = 3;

      private static final int NUMJOKES = 5;

      private int state = WAITING;
      private int currentJoke = 0;

      private String[] clues = { "Turnip", "Little Old Lady", "Atch", "Who", "Who" };
      private String[] answers = { "Turnip the heat, it's cold in here!",
      "I didn't know you could yodel!",
      "Bless you!",
      "Is there an owl in here?",
      "Is there an echo in here?" };

      public String processInput(String theInput) {
      String theOutput = null;

      if (state == WAITING) {
      theOutput = "Knock! Knock!";
      state = SENTKNOCKKNOCK;
      } else if (state == SENTKNOCKKNOCK) {
      if (theInput.equalsIgnoreCase("Who's there?")) {
      theOutput = clues[currentJoke];
      state = SENTCLUE;
      } else {
      theOutput = "You're supposed to say \"Who's there?\"! " +
                     "Try again. Knock! Knock!";
      }
      } else if (state == SENTCLUE) {
      if (theInput.equalsIgnoreCase(clues[currentJoke] + " who?")) {
      theOutput = answers[currentJoke] + " Want another? (y/n)";
      state = ANOTHER;
      } else {
      theOutput = "You're supposed to say \"" +
                     clues[currentJoke] +
                     " who?\"" +
                     "! Try again. Knock! Knock!";
      state = SENTKNOCKKNOCK;
      }
      } else if (state == ANOTHER) {
      if (theInput.equalsIgnoreCase("y")) {
      theOutput = "Knock! Knock!";
      if (currentJoke == (NUMJOKES - 1))
      currentJoke = 0;
      else
      currentJoke++;
      state = SENTKNOCKKNOCK;
      } else {
      theOutput = "Bye.";
      state = WAITING;
      }
      }
      return theOutput;
      }
      }

      I'm new to JSP. please any one help me....
        • 1. Re: How to continuously monitor ip
          807580
          Eh? Why does your web app need to open more ports? Copying-and-pasting tutorial code all over the place without having a clue what it does isn't going to magically make your app do whatever it is you're trying to do. What are you trying to do?
          • 2. Re: How to continuously monitor ip
            796440
            YogishGS wrote:
            continuously monitor the ip of the website and receive if any data comes in.
            What does this even mean? If you just mean listen to all traffic coming to your webserver, then that's your webserver doing that.

            If you mean intercept or monitor traffic for some other app, then you can't do that in Java.

            If you mean something else, then please clarify.
            • 3. Re: How to continuously monitor ip
              DrClap
              YogishGS wrote:
              I'm new to JSP. please any one help me....
              Well, you wouldn't run a socket server in a JSP. That isn't what it's for.