The Artima Developer Community
Sponsored Link

Java Answers Forum
Network question

7 replies on 1 page. Most recent reply: Sep 24, 2003 10:53 AM by shon

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
shon

Posts: 42
Nickname: shon
Registered: Apr, 2003

Network question Posted: Sep 23, 2003 10:44 AM
Reply to this message Reply
Advertisement
how to used java code to get local ip adress?
for example :
if my pc ip is 192.168.0.2 in local area network ..

how to return the 192.168.0.2 ???

thanks for answering


zenykx

Posts: 69
Nickname: zenykx
Registered: May, 2003

Re: Network question Posted: Sep 23, 2003 3:33 PM
Reply to this message Reply
You may use
NetworkInterface.getNetworkInterfaces()

and then using NetworkInterface and InetAddress you can find those informations.

shon

Posts: 42
Nickname: shon
Registered: Apr, 2003

Re: Network question Posted: Sep 23, 2003 8:44 PM
Reply to this message Reply
can u give me an example ?
i still familiar of using networkinterface ....
THanks

shon

Posts: 42
Nickname: shon
Registered: Apr, 2003

Re: Network question Posted: Sep 23, 2003 8:50 PM
Reply to this message Reply
can u give me an example ?
i still not familiar of using networkinterface ....
THanks

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Network question Posted: Sep 23, 2003 10:21 PM
Reply to this message Reply
import java.net.*;
import java.util.*;
 
public class Test{
	public static void main(String[] args) {
		Test t = new Test();
		try{
			Enumeration e = NetworkInterface.getNetworkInterfaces();
			while (e.hasMoreElements()) {
			     System.out.println(e.nextElement());
			}
		}catch(Exception e){
		}
	}
}

shon

Posts: 42
Nickname: shon
Registered: Apr, 2003

Re: Network question Posted: Sep 24, 2003 12:15 AM
Reply to this message Reply
this code make it returns three ips...
one is local which is 127.0.0.1
and 192.168.0.2 , and the internet IP:ex : 202.188.0.2

But the ips actually i want ... is only 192.168.0.2 ....
how to make the system return this ip ..?
if i have more than one networkcard install on my pc.. THen how to check which one is connected to my LAN ?

For ex: 192.168.0.1, 192.168.0.2 is both my lan cards....
but the first one is ussed as connect to the boadband modem , the second one is conencted to my LAN ...

how to get 192.168.0.2 using java code?
Please Help ... THAnks

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Network question Posted: Sep 24, 2003 12:36 AM
Reply to this message Reply
Are you searching the web for answers before posting your questions? Or Just posting so that someone else will do the work for you???

http://forum.java.sun.com/thread.jsp?forum=31&thread=388795

import java.net.*;
public class Test{
	public static void main(String[] args) {
	try{
		String ip=InetAddress.getLocalHost().getHostAddress();
		System.out.println(ip);
	}catch(Exception e){
		}
	}
}

shon

Posts: 42
Nickname: shon
Registered: Apr, 2003

Re: Network question Posted: Sep 24, 2003 10:53 AM
Reply to this message Reply
i have try so many time, but can't get what i want, actually i m not so familiar with java.net.*; so get lost . But anyway thanks for your help, sorry if make you furious.

Flat View: This topic has 7 replies on 1 page
Topic: JDBC  Current Date Previous Topic   Next Topic Topic: Network question

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use