How to get visitors IP Address?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mjohns2839
    New Member
    • Sep 2007
    • 10

    #1

    How to get visitors IP Address?

    I am using Virtual.php and I have to write a program similar to this that reports the IP address of the user to the server. This is as much as I have so far.

    import java.lang
    import java.net
    import javax.net
    import javax.net.ssl
    import javax.security. auth.kerberos
    import org.ietf.jgss

    public class InetAddress extends Object implements Serializable

    Help I'm at a roadblock.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi. Welcome to The Scripts!

    I am going to split your post into it's own thread in the Java forum, where the Java Experts are more likely to find it.

    Moderator

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by mjohns2839
      I am using Virtual.php and I have to write a program similar to this that reports the IP address of the user to the server. This is as much as I have so far.

      import java.lang
      import java.net
      import javax.net
      import javax.net.ssl
      import javax.security. auth.kerberos
      import org.ietf.jgss

      public class InetAddress extends Object implements Serializable

      Help I'm at a roadblock.
      You don't have much at all. Google some more and post again when you've done a bit of real work on it.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        When a ServerSocket accepts a connection request from a client the accept
        method returns a connected Socket. Read the API documentation for what such
        a Socket can tell you about its connected peer.

        kind regards,

        Jos

        Comment

        • mjohns2839
          New Member
          • Sep 2007
          • 10

          #5
          Originally posted by JosAH
          When a ServerSocket accepts a connection request from a client the accept
          method returns a connected Socket. Read the API documentation for what such
          a Socket can tell you about its connected peer.

          kind regards,

          Jos
          I probably can find this documentation at sun.com.

          Thanks.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by mjohns2839
            I probably can find this documentation at sun.com.

            Thanks.
            Yup, also check the 'Index' article in the Java Articles section; it contains links
            to the docs you want/need.

            kind regards,

            Jos

            Comment

            • mjohns2839
              New Member
              • Sep 2007
              • 10

              #7
              import java.lang:
              import java.net:
              import javax.net:
              import javax.net.ssl:
              import javax.security. auth.kerberos:
              import org.ietf.jgss:

              public class InetAddress extends Object implements Serializable

              Can anyone tell me why I am having a problem with these import statements?
              1) cannot find symbol
              symbol : class lang
              location: package java

              2) cannot find symbol
              symbol : class net
              location: package java

              3) package javax does not exist

              4) cannot find symbol
              symbol : class ssl
              location: package javax.net

              5) cannot find symbol
              symbol : class keberos
              location: package javax.security. auth

              6) package org.ietf does not exist

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                You import classes, not packages; either individual classes in a package or all
                of the classes in a package.

                kind regards,

                Jos

                Comment

                • mjohns2839
                  New Member
                  • Sep 2007
                  • 10

                  #9
                  Originally posted by JosAH
                  You import classes, not packages; either individual classes in a package or all
                  of the classes in a package.

                  kind regards,

                  Jos
                  Thats what I thought.

                  Thanks

                  Comment

                  • mjohns2839
                    New Member
                    • Sep 2007
                    • 10

                    #10
                    Originally posted by mjohns2839
                    Thats what I thought.

                    Thanks
                    import java.io.*;
                    import java.net.*;
                    import java.lang.*;
                    import javax.net.*;
                    import javax.net.ssl.* ;
                    import javax.security. auth.kerberos.* ;
                    import org.ietf.jgss.* ;

                    Comment

                    • mjohns2839
                      New Member
                      • Sep 2007
                      • 10

                      #11
                      How to get visitors IP Address

                      Thanks to your help I have gotten this far. Could I get help with the
                      "To Do Logic for getAddress and logfile". I don't know if this is process is possible or not. Also I am not sure about the code for the server accepting multiple threads.

                      Thanks


                      /*
                      * InetAddressApp. java
                      *
                      *October 2, 2007, 10:46 AM
                      *
                      */
                      package inetaddressapp;

                      import java.io.*;
                      import java.net.*;
                      import java.lang.*;
                      import javax.net.*;
                      import javax.net.ssl.* ;
                      import javax.security. auth.kerberos.* ;
                      import org.ietf.jgss.* ;

                      /**
                      *
                      * @author mj
                      */
                      public class InetAddressApp {

                      /** Creates a new instance of InetAddressApp */
                      public InetAddressApp( ) {
                      }
                      /**
                      * @param args the command line arguments
                      */
                      public static void main(String[] args) {
                      ServerSocket serverSocket = null; /**serverSocket variable created.*/
                      try { /**program begins by creating a new ServerSocket object to listen on a specific port ie. 4444.*/
                      serverSocket = new ServerSocket(44 44);
                      } catch (IOException e) {
                      System.out.prin tln("Could not listen on port: 4444 another process may be using this port");
                      System.exit(-1);
                      }
                      /**the server successfully binds to its port, the ServerSocket object is created*/
                      /**and the server continues to the next step -- accepting a connection from a client.*/

                      Socket clientSocket = null;
                      try {
                      clientSocket = serverSocket.ac cept();//the accept method waits until there is a request and gives a connection.
                      } catch (IOException e) {
                      System.out.prin tln("Accept failed: 4444");
                      System.exit(-1);
                      }
                      while (true){
                      accept a connection ; /**the server can service multiple connections using threads on the same port.*/
                      create a thread to deal with the client ;
                      end while

                      public byte [] getAddress (){/**this method will get the IPAddress of the client and print to logfile.*/
                      }/**To Do Logic for getAddress and logfile*/

                      /**The new thread reads from and writes to the client connection as necessary.
                      * After the server successfully establishes a connection with a client, it communicates
                      * with the client using this code:*/

                      PrintWriter out = new PrintWriter(
                      clientSocket.ge tOutputStream() , true);
                      BufferedReader in = new BufferedReader(
                      new InputStreamRead er(
                      clientSocket.ge tInputStream()) );
                      String inputLine, outputLine;
                      while ((inputLine = in.readLine()) != null) {
                      outputLine = inputLine;
                      out.println(out putLine);
                      if outputLine.equa ls("Bye."))
                      break;
                      }
                      out.close();
                      in.close();
                      clientSocket.cl ose();
                      serverSocket.cl ose();
                      Last edited by mjohns2839; Oct 2 '07, 06:17 PM. Reason: Not sure about the server accepting multiple threads code.

                      Comment

                      • mjohns2839
                        New Member
                        • Sep 2007
                        • 10

                        #12
                        I don't think I meant to use Serversocket as a variable.

                        Comment

                        • mjohns2839
                          New Member
                          • Sep 2007
                          • 10

                          #13
                          Jos thanks, this is shaping up. I'll post the new improved version by next week.
                          I like the project with the array list where can I see the final code?


                          package inetaddressapp;

                          import java.io.*;
                          import java.net.*;
                          import java.lang.*;
                          import javax.net.*;
                          import javax.net.ssl.* ;
                          import javax.security. auth.kerberos.* ;
                          import org.ietf.jgss.* ;

                          public class ServerSocket extends object {
                          /*ServerSocket serverSocket = null; /**serverSocket variable created.*/

                          try { /**program begins by creating a new ServerSocket object to listen on a specific port ie. 4444.*/
                          serverSocket = new ServerSocket(44 44);
                          catch (IOException e) {
                          System.out.prin tln("Could not listen on port: 4444 another process may be using this port");
                          System.exit(-1);
                          }
                          /**the server successfully binds to its port, the ServerSocket object is created*/
                          /**and the server continues to the next step -- accepting a connection from a client.*/

                          Socket clientSocket = null;
                          try {
                          clientSocket = serverSocket.ac cept();//the accept method waits until there is a request and gives a connection.
                          } catch (IOException e) {
                          System.out.prin tln("Accept failed: 4444");
                          System.exit(-1);
                          }
                          while (true){
                          accept a connection ; /**the server can service multiple connections using threads on the same port.*/
                          create a thread to deal with the client ;
                          end while

                          public byte [] getInetAddress (){/**this method will get the IPAddress of the client and print to logfile.*/
                          /**To Do Logic for get Address*/{
                          }
                          /**The new thread reads from and writes to the client connection as necessary.
                          * After the server successfully establishes a connection with a client, it communicates
                          * with the client using this code:*/

                          PrintWriter out = new PrintWriter(
                          clientSocket.ge tOutputStream() , true);
                          BufferedReader in = new BufferedReader(
                          new InputStreamRead er(
                          clientSocket.ge tInputStream()) );
                          String inputLine, outputLine;
                          while ((inputLine = in.readLine()) != null) {
                          outputLine = inputLine;
                          out.println(out putLine);
                          if outputLine.equa ls("Bye."))
                          break;
                          }
                          out.close();
                          in.close();
                          clientSocket.cl ose();
                          serverSocket.cl ose();


                          }
                          }
                          }

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by mjohns2839
                            Jos thanks, this is shaping up. I'll post the new improved version by next week.
                            Well, you're welcome of course and I think you've got the idea: create a server
                            socket for a certain port. Repeatedly accept new connection requests and start
                            a new thread for it. Every single thread deals with a single client.

                            There are two termination conditions:

                            1) a client terminates the communication in which case a single thread deals
                            with it and quits.

                            2) the entire server must be taken down. The original thread (where the accepts
                            are done) should terminate.

                            For 2) there's a decision to make:

                            1) terminate all client connections immediately or

                            2) allow those last clients to keep on communicating, just no new client connections
                            are accepted anymore.

                            Good luck with it and

                            kind regards,

                            Jos

                            Comment

                            • mjohns2839
                              New Member
                              • Sep 2007
                              • 10

                              #15
                              This sounds like what I intend to do in the To Do section of handling the getInetAddress method.

                              Thanks.

                              Comment

                              Working...