How to get IP address via java or javascript? - java

My computer name is D*******.
When it is not connected to VPN client it gives same ip address from ipconfig and www.jsonip.com too.
But when I connect to VPN client the ip4 address changes to ppp adapter ip address, whereas the www.jsonip.com site still retrieves Ethernet ip4 address.
Is there a javascript or java way to get ip address via resolving the Computer Name?

JAVA check this..
import java.net.InetAddress;
public class GetIPAddress {
public static void main(String[] args) {
try {
InetAddress thisIp = InetAddress.getLocalHost();
System.out.println("IP:" + thisIp.getHostAddress());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Another JAVA sample Code
InetAddress Address = InetAddress.getLocalHost();
System.out.println(Address);
Address = InetAddress.getByName("google.com");
System.out.println(Address);
For JS Try this javaScript code it might help you.
Superuser
http://jsfiddle.net/

Try this.
import java.net.InetAddress;
class IPAddress
{
public static void main(String args[]) throws Exception
{
System.out.println(InetAddress.getLocalHost());
}
}

Related

Connection failed with racer reasoner

I'm trying to use racer (Description Logic reasoner), but i get the following error
com.racersystems.jracer.RacerClientException: Connection refused: connect
at com.racersystems.jracer.RacerClient.openConnection(RacerClient.java:76)
at test.Test.main(Test.java:12)
the code i'm executing is the following :
package test;
import com.racersystems.jracer.*;
public class Test {
public static void main(String[] argv) {
String ip = "127.0.0.1";
int port = 8088;
String filename="\"/jracer-2-0.zip_expanded/jracer-2-0/demo/people+pets.owl\"";
RacerClient racer = new RacerClient(ip,port);
try {
racer.openConnection();
System.out.println(racer.sendRaw("(owl-read-file " + filename + ")"));
System.out.println(racer.sendRaw("(all-atomic-concepts)"));
racer.closeConnection();
}
catch (Exception e) {
e.printStackTrace();
}
}}
I don't know who to solve it ?
Any suggestions ?
You are apparently trying to connect to a service that is running on the same machine as you are running the program ... using the 127.0.0.1 loopback interface.
The fact that it is giving you a "connection refused" response means that the service is not currently running on "this machine" or the port that you have specified.
The solution will be to ensure that the service is running and listening on the specified IP and port; e.g. check the IP address and port, and start or restart the service.

Why I can't connect my sockets from two PCs in Java?

So far I have achieved making server/client relations on the same computer by parallelly running different java classes. As I don't want to overcomplicate this question, I will post my simplified code which works perfectly fine.
Server:
public class Server {
public static final int PORT = 9090;
public static void main (String[] args) {
try {
ServerSocket serverSocket = new ServerSocket (PORT);
Socket client = serverSocket.accept ();
System.out.println ("Client connected!");
} catch (IOException ioException) {
ioException.printStackTrace ();
}
}
}
Client:
public class Client {
public static final int PORT = 9090;
public static final String IP_ADDRESS = "127.0.0.1";
public static void main (String[] args) {
try {
Socket socket = new Socket (IP_ADDRESS,PORT);
} catch (IOException ioException) {
ioException.printStackTrace ();
}
}
}
After I run them, I get the expected output - console in class Server prints "Client connected!".
Like any other curious programmer, I decided to try out the same program on my two laptops. One laptop has client code, while second has server code. Of course, I had to change "127.0.0.1" or "localhost" to ip address my server laptop has by typing on google "what is my IP address". I just copied that new IP address into IP_ADDRESS variable and hoped it would work the same. Unfortunately, it didn't happen. My client laptop looks as if it never connected to server laptop, because server laptop never printed message "Client connected!". What am I missing? It looks so easy, yet it doesn't work. Could someone help me solve this?
P.S. I don't want to share my IP address due to privacy reasons, but it was the first number that pops when any of you google: what is my IP address?
If you are on a local network, you don't have to take your public IP. You need to find your local IP (if you are on linux, a simple "ip a" and you'll have your IP address, if you are on windows )
If you are not on a local network, you could open your router' settings to open the 9090 port but I STRONGLY discourage you to do something like that for security reason.

Get IP of a specific host is not working over a network with Java

I'm not able to get IP of hostname over a network.
I can get public IP but seems not to work over a network because of missing protocol:
public static void main(String[] args) throws UnknownHostException {
String url = "host22.my.network";
getIp(url);
}
public static void getIp(String url) throws UnknownHostException{
try {
InetAddress ip = InetAddress.getByName(new URL(url).getHost());
System.err.println(ip);
}
catch (MalformedURLException e) {
System.err.println(e.getMessage());
}
}
maybe it's missing a protocol prefix
Since #ejp doesn't want to actually answer questions any more, here's what he's saying:
new URL(url).getHost() is wrong. Instead, use
InetAddress ip = InetAddress.getByName(url)
And since you're not actually passing a URL, rename the parameter to hostname.

How to verify the ACE-CORBA service local windows machine

I'm able to create docker container for ACE-TAO service , and able to access it from parent windows machine using port-forwarding concept.
From browser i try to hit the localhost:forward-port and getting "ERR_EMPTY_RESPONSE" and TAO service is running in docker container.
If I want to verify in local, whether its connected properly or not.
How can I write Java code to verify?
The following java code connects to localhost:17500 and prints out a message saying whether or not it could create a tcp connection.
import java.io.*;
import java.net.*;
class TCPClient
{
public static void main(String argv[]) throws Exception
{
try {
Socket clientSocket = new Socket("localhost", 17500);
System.out.println("Could connect");
}
catch (ConnectException e) {
System.out.println("Cannot connect");
}
}
}

Binding Socket to any existing address on specific subnet

I have a java application in which I want to receive a udp broadcast telegram in a known subnet, for example 192.168.x.x (255.255.0.0). If I know the IP of the machine which is running the application, the following works (assuming the IP of the machine is 192.168.10.1)
InetAddress ip = InetAddress.getByName("192.168.10.1");
DatagramSocket socket = new DatagramSocket(2222, ip);
But if the IP is not fix, maybe from a dhcp server, how could I create a socket which is bounded to the ip which the machine gets from the dhcp server. There may be other interfaces on other subnets, so if I will use
DatagramSocket socket = new DatagramSocket(2222);
the socket is not surely bounded to the ip in the subnet with 192.168.x.x.
How could I solve this?
So if I understand your problem is to get the address assigned to an interface. I suppose you have more than one interface and you want to select one.
Also note by using DatagramSocket socket = new DatagramSocket(2222); you are binding to the wildcard address that often mean any and should work in many cases
From Oracle documentation List Network interfaces you can retrieve inetAddress from different interfaces
import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;
public class ListNets {
public static void main(String args[]) throws SocketException {
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets))
displayInterfaceInformation(netint);
}
static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
out.printf("Display name: %s\n", netint.getDisplayName());
out.printf("Name: %s\n", netint.getName());
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
for (InetAddress inetAddress : Collections.list(inetAddresses)) {
out.printf("InetAddress: %s\n", inetAddress);
}
out.printf("\n");
}
}

Categories