• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

class InetAddress

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
KouKi
Asked:
KouKi
  • 7
  • 4
  • 3
2 Solutions
 
CEHJCommented:
>>but I need my IP to the Net    

Why, as it happens?
0
 
KouKiAuthor Commented:


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
 
DrWarezzCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
DrWarezzCommented:
>"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
 
CEHJCommented:
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
 
KouKiAuthor Commented:
> 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
 
CEHJCommented:
>>I didn't get this

What router are you using?
0
 
KouKiAuthor Commented:
a topcom router,
i know how to port forwarding,
but i dont understand the link with how i can get my IP ...
0
 
CEHJCommented:
If the router is also the gateway, give the gateway:x as the address and get it to port-forward to server:x
0
 
CEHJCommented:
Sorry - i'm losing concentration - forget that!
0
 
CEHJCommented:
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
 
KouKiAuthor Commented:
Thanks,
I have no idea on how i can do this, but i'll try to get further information!
See you !
0
 
DrWarezzCommented:
:)
0
 
CEHJCommented:
:-)
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 7
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now