Need to access API (https://localhost:8080/er/heartbeats) from another VM - java

I am working on task where I need to access URL https://localhost:8080/er/heartbeat which gave data from heartbeat.xml from other VM.
Need help in this.

You cannot access "localhost" (127.0.0.1, etc) from another machine. The 127.x.x.x net range is for host-local use only. The packets sent to this range are not (should not be) routed outside of this machine / virtual machine.
You need to use an external IP address for this machine or a DNS name that resolves to an external IP address. (It could be a private IP address, provided that the other VM knows how to route packets to it.)
References:
How to view the localhost of another computer in the same network?
The Wikipedia page on localhost.

If you are in the same network you just need to know the IP of your machine and access it like:
https://other.machine.ip:8080/er/heartbeat

Related

Which ip address should I use to connect 2 computers in my home using datagram sockets in java?

I am trying to connect 2 computer in my home using DatagramSockets (or even Sockets) in java. What exactly should I do? Which IP Address should I use to connect them?
If your machine is using DHCP then it's not upto you to decide which ip address you will use. Your machine will be assigned some dynamic ip address. To see that use ifconfig on Linux box and ipconfig on win machine. Once you have their IP address you can use these to connect your machine. Chances are high that your machine has dynamic ip's.
Assuming you're a windows user:
to get your LAN IP address open a command prompt and type ipconfig.
A bunch of stuff will show up, you are looking for the line that says
IPv4 address.....: 192.168.#.#
It should be noted however, that this is a so "dynamic" ip address, that can be changed whenever you disconnect and reconnect from your router.
I recommend that you either make your ip static (look this up on google, there are lots of tutorials) or use your computers hostname instead. To obtain your hostname you simply type hostname in the commandprompt.
in your code you can get your ip address by doing this in your client code:
String ip = Inet4Address.getByName("<your servers hostname>").getHostAddress();
I hope this helps, although questions like these belong on Super User, as they really don't have much to do with coding.

How does DNS work with Java Sockets?

My question feels kind of basic, and yet it has made me curious for a while:
Does using the name of a server instead of its IP address work when using a Java Socket?
For example, if I am the manager of a certain server with the address "bogusserver.com" and use this address instead of the actual IP of the server when opening the Socket with a 'new Socket("bogusserver.com", 8080);' will it actually open the socket normally?
If it does, how exactly does the Socket class solve the address? Does it use the DNS server registered at the computer running the code?
I know that using the "localhost" address the Socket will open normally, but then, everything works with localhost :P
You just need to read the javadoc. A Socket is constructed using an InetAddress.
The InetAddress javadoc says:
Host name-to-IP address resolution is accomplished through the use of a combination of local machine configuration information and network naming services such as the Domain Name System (DNS) and Network Information Service(NIS). The particular naming services(s) being used is by default the local machine configured one. For any host name, its corresponding IP address is returned.
Does using the name of a server instead of its IP address work when using a Java Socket?
DNS maybe used by the OS to translate the hostname into an IP address. It doesn't matter if you use TCP, UDP or ICMP.
If it does, how exactly does the Socket class solve the address?
It passes it to the OS to do the lookup.
Does it use the DNS server registered at the computer running the code?
The OS has registered DNS servers and they may be contacted if there isn't a hosts entry for that hostname.

Python and java static IP

I have a Python server. Each time I change the network the IP changes. I want it to have a static IP over all networks so it can receive data without customizing the code every time.
I want to do the same for a Java server too.
You may talk with your ISP to rent a static IP address, although I would recommend using name instead of IP address. Means, trust a DNS server to translate the IP to the name and and thus even if the IP is changing, it will give the new IP each time. How? well there are free online name services (such as "no-ip") that you can register your IP each time, to a fixed name address.

Static and Dynamic Ip address problems

Let's say we are hosting on a Personal Computer and Server Program is written in Java.
1.If we host server using static IP-address then does that means we can change machine and replace it with other ones, restart machine as many times we want and our Client will still be able to communicate with us after system is back?
2.if we host server using Dynamic IP-address then just by restarting once the machine we have to tell every single client out there that new IP-address is this one?
3.Will Dynamic IP-addresses change even if we don't restart the machine ?
Yes, a static ip address means that it is fixed. There is more to the whole system, of course, since there is resolution of an IP to a MAC address. Nonetheless, a static IP (assuming it is only on the network once) means that any machine with that IP will respond. So a client can always connect to the IP address.
Note: not a very friendly way to go.
Just because something has a "dynamic" IP address does not necessarily mean it changes every single time. Using DHCP it is possible to assign the same IP address to the same MAC address each time. It is a much better approach than hardcoding an IP address to a machine.
In addition, you really should not have your clients use an IP address to connect. They should look up the machine by a name in DNS. You can coordinate the DNS lookup to the DHCP, so machines do not need to do anything but resolve the hostname.
However, as your question stands, if a machine uses DHCP and does not receive the same IP address each time, and you have your clients connecting by IP address, then on each new assignment the clients would need the new IP address.
Whether a machine's IP address updates even when it does not restart is a policy that is controlled by the DHCP server. In general, there is a renewal time for an IP address. Without going into great deal, the client during the DHCP conversation may request the same IP address, but it is up to the server as to whether to hand out the same one or not. Conversely, most servers are configured to hand out the same IP address to a given MAC address as long as the DHCP cache is current. It is completely a policy decision.
At the end of the day, however, it seems like you are attempting to tie clients to a server's IP address, and this is not a good strategy. It is a better strategy to use DNS and have the clients resolve a hostname. If I am incorrectly inferring your intent, I apologize.
NOTE: I have used DHCP as the way to give out dynamic IP addresses. You might use some other strategy, but I think the concept is the same.
1: Yes, if the server has a static IP clients will always be able to count on the server being at that IP address.
2: Most likely, yes. In many networks the DHCP server will give the same IP back to a machine that has rebooted but you certainly should not count on that.
3: Yes - dynamically assigned IP addresses have a "TTL" - a time to live. This may be a long time (weeks or more) but they still could expire and change. Most often the machine will get the same IP back but, again, you should not count on that.

Java Socket Programming

Java Socket Program did not work for WAN
I have written a TCP IP socket program which works fine in my LAN.
One of my friend is at bangalore He ran the server and I ran the
client with the host name of my friend's IP. In this case my
socket program did not work.
You said that your program is attempting to connect to host 192.168.1.107 port 46216.
192 prefix specifies it is a class C address and is private. Making your program connect to that will force it to remain on the local network searching for that node. You will need to find the IP address of your router (you can use http://whatismyip.org/ to find this out). Then go into your router settings and forward port 46216 to 192.168.1.107 (your node), or even better, your MAC address which is not subject to change (in case your router is running DHCP).
on a side note, it isn't good to hardcode IP addresses. Simply use a textfield to avoid having to redistribute the client when your IP is changed, as it is likely you have a dynamic IP from your ISP.
Your friend running the server is most likely behind either a firewall or NAT. Make sure you are using the external IP address and if necessary port forwarding the packets to the correct IP.
The IP address you gave seems to be a local address, rather than a public internet address. If you are looking for 192.x.x.x, you will not make it out to "the internet", but will be confined behind your router.
WhatIsMyIP is a good way of getting a public IP address, and THAT is the address you should use in your connection. Also, be sure to forward any ports that will be used by your program, because otherwise your router will likely filter the traffic and still create an issue.
You could use an implementation of STUNT or other NAT Traversal protocol.
The ip of computer on thih u deployed your server program is not in your reach.
192.x.x.x ip means (class C) it is for local subnet.
You need to have change your ip address of your net-adapter so that your router could route it through internet.

Categories