Solved

class InetAddress

Posted on 2004-10-25
187 Views
Last Modified: 2010-03-31
hi, I have a little problem when I'm trying to check my hostname,
here is the code

server = new ServerSocket(4000,1);

System.out.println(server.getInetAddress().getHostName());  
// 0.0.0.0 <- ?

connection = server.accept();

System.out.println("Connected on "+ connection.getInetAddress().getHostName());
// Connected on SAPPHIRE

I need to have in my application my IP as well but I don't know how to get it.
I tried this

System.out.println("nom "+InetAddress.getLocalHost());

But it gives me my local hostname & IP,     //rockstar/192.168.1.4
but I need my IP to the Net     //217.136.10.198 for example

Thanks :-)
0
Question by:KouKi
    14 Comments
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>but I need my IP to the Net    

    Why, as it happens?
    0
     

    Author Comment

    by:KouKi


    client = new Socket(ip, 4000);

    So that the user can connect to my server !!
    He has to know my IP,
    and since I got a router, my hostname is my local address
    0
     
    LVL 9

    Accepted Solution

    by:
    Ah, because you're on a network, you're retrieving your network IP, rather than your internet one.

    I had this same problem ages ago -- and I came up with some code that logged on to: www.whatismyip.com, and extracted my IP from that page.

    But, if you have a dedicated server somewhere, that you can run a java app on, then create a server socket on that, connect to it from your machine, and have the server send you your IP  ;)
     
    there's bound to be an easier way I'm sure.

    gL,
    [r.D]
    0
     
    LVL 9

    Expert Comment

    by:DrWarezz
    >"client = new Socket(ip, 4000);

    So that the user can connect to my server !!
    He has to know my IP,
    and since I got a router, my hostname is my local address"

    If the user has to connect to YOUR machine, how are you going to 'tell' the user what YOUR IP is anyways?

    [r.D]
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    This is not really a Java issue. What you need to do is to configure your network so it will allow this. Get it to do port forwarding to a port of your choice on the server
    0
     

    Author Comment

    by:KouKi
    > If the user has to connect to YOUR machine, how are you going to 'tell' the user what YOUR IP is anyways?

    The servers are gonna put their ip's in a database (<- and that's why i need it),
    then those will appear in a list and the clients have to double click on it ...

    >But, if you have a dedicated server somewhere, that you can run a java app on, then create a server socket on that, connect to it from your machine, and have the server send you your IP  ;)

    Thanks that should work

    > Get it to do port forwarding to a port of your choice on the server

    I didn't get this :-( ... how do you mean ?
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>I didn't get this

    What router are you using?
    0
     

    Author Comment

    by:KouKi
    a topcom router,
    i know how to port forwarding,
    but i dont understand the link with how i can get my IP ...
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    If the router is also the gateway, give the gateway:x as the address and get it to port-forward to server:x
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Sorry - i'm losing concentration - forget that!
    0
     
    LVL 86

    Assisted Solution

    by:CEHJ
    What i was really trying to get at was not port forwarding, but NAT in relation to a particular port. My last comment could be read substituting 'NAT' for 'port-forward'
    0
     

    Author Comment

    by:KouKi
    Thanks,
    I have no idea on how i can do this, but i'll try to get further information!
    See you !
    0
     
    LVL 9

    Expert Comment

    by:DrWarezz
    :)
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    :-)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    Title # Comments Views Activity
    ShiftLeft challenge 21 42
    Help refactoring a java code 8 37
    count8 challlenge 13 35
    pairstar challenge 2 12
    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now