Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

How can I get the gateway's IP of my PC in java?

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have searched the Internet that somebody told me run a thread and then input the command "ipconfig /all" and get the outputstream of it ,and analysis the outputstream meanwhile get the gateway. But I don't know how to realize it? Could you give me some hit?

I truly hope you could provide me a easier method ,whick I think, like using some jars?

thanks a lot
jun
 
ken jun
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally I couldn't find there is any jar to finish my job. So I have to run a thread , input the "ipconfig /all" here is my code, run properly in xp but something wrong in win7 and vista , I have to test the code in win7 again later.
 
Marshal
Posts: 25215
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is what you want different from this?
 
Sheriff
Posts: 21866
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The gateway is definitely not the same as the IP address, which is what that method will return. If your IP address is 192.168.0.10, the gateway is usually 192.168.0.1 or 192.168.0.254. It's the IP of the first machine to contact when going outside of your own subnet.
 
ken jun
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Is what you want different from this?



Of course . Your code merely can get the IP addr of the local PC. But I want to get the IP addr of the gateway. anyway thanks for you reply
 
ken jun
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:The gateway is definitely not the same as the IP address, which is what that method will return. If your IP address is 192.168.0.10, the gateway is usually 192.168.0.1 or 192.168.0.254. It's the IP of the first machine to contact when going outside of your own subnet.



I know what you mean, but the gateway can regard as a PC(not right but in some way means the same), then it hava its own IP address . What I want is to get the IP addr of it.
I am using java to achieve ARP, I have already finished it . Then I want to write a method to check if the input IP addr is in the same network segment with the local PC's IP addr, if not then return the gateway's MAC, so firstly I have to affirm the gateway's IP then using ARP to get gateway's MAC. So this is the reason why I write this post .

anyway thanks for your reply
 
Rob Spoor
Sheriff
Posts: 21866
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ken jun wrote:

Rob Spoor wrote:The gateway is definitely not the same as the IP address, which is what that method will return. If your IP address is 192.168.0.10, the gateway is usually 192.168.0.1 or 192.168.0.254. It's the IP of the first machine to contact when going outside of your own subnet.



I know what you mean, but the gateway can regard as a PC(not right but in some way means the same), then it hava its own IP address . What I want is to get the IP addr of it.


I know, I was actually replying to Paul.

Since this information seems (or is?) unavailable from Java there are two options: use JNI (although I don't know the native API call), or call an external program. You're already doing that, you just need to improve the parsing of the output. And read When Runtime.exec() won't - right now you're ignoring the process' error stream which may cause waitFor() to hang. A simple solution is to use ProcessBuilder instead of Runtime.exec, and redirect the error stream.
 
ken jun
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:

ken jun wrote:

Rob Spoor wrote:The gateway is definitely not the same as the IP address, which is what that method will return. If your IP address is 192.168.0.10, the gateway is usually 192.168.0.1 or 192.168.0.254. It's the IP of the first machine to contact when going outside of your own subnet.



I know what you mean, but the gateway can regard as a PC(not right but in some way means the same), then it hava its own IP address . What I want is to get the IP addr of it.


I know, I was actually replying to Paul.

Since this information seems (or is?) unavailable from Java there are two options: use JNI (although I don't know the native API call), or call an external program. You're already doing that, you just need to improve the parsing of the output. And read When Runtime.exec() won't - right now you're ignoring the process' error stream which may cause waitFor() to hang. A simple solution is to use ProcessBuilder instead of Runtime.exec, and redirect the error stream.



thanks a lot I will try it later and find some information about JNI, which seems so important when doing some low-level job using JAVA by the way can you provide me some sites about JNI.
 
Rancher
Posts: 4686
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be careful, getting the information is restricted by some popular operating systems, including Android and OS-X. And also many Linux distributions. Once upon a time, the official command to find this was "netstat -r" but these days it varies. Sometimes "route" will give it, but nothing is guaranteed.

Obviously your use of "cmd" in the command is Windows specific.


 
ken jun
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pat Farrell wrote:Be careful, getting the information is restricted by some popular operating systems, including Android and OS-X. And also many Linux distributions. Once upon a time, the official command to find this was "netstat -r" but these days it varies. Sometimes "route" will give it, but nothing is guaranteed.

Obviously your use of "cmd" in the command is Windows specific.




thanks I just think about windows maybe I will try Linux later
 
ken jun
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
reply again....haha

I really want to know whether there is a jar can get what the cmd with inputing "ipconfig" get .......I really don't want to use the method that run a thread that open the cmd and then input then get the output......

If the answer is no, that I want to write one... Will I use the API of windows? I hope you can give me some hits..thanks

jun
 
mooooooo ..... tiny ad ....
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!