Java RMI - Implementing Local to Implementing Remote - java

I understand and have coded up some RMI code and it works. I can create a RMIServer object and a RMIClient object and the client can send messages to the server and the server receives it perfectly.
My question is how do I communicate between two different computers in different parts of the world?

When you connect to the RMI server you provide an IP or name of the machine hosting the server. You will simply provide the IP/name of the RMI server in place of the one you're currently using.

To get your ip, if you have windows go to start ->run-> write cmd. now in the console write ipconfig your ip is ADDRESS IP.
Remember, if you have dynamic ip (unless your supplier get you a static ip) this change when restart your pc

Related

how to detect main server ip address from client system using java?

Is it possible to get main server system's(which is connected to LAN) ip address from client system using java?
If yes, how can i do using code?
Generally it is impossible whether using java or any other programming language. This is the reason that LANs exist. The internal IP address is not sent outside the local network and therefore cannot be detected.
However if client and server are developed especially to complete this task to can send the IP on application level. For example to send the IP as a HTTP header if connection is done over HTTP.

Socket communication on Android localhost

I'm developing multiple applications which need to communicate with each other via socket. First I need them to be able to communicate on localhost. So when I'm running both of them at the same time on my phone they can communicate (I know that there are easier ways to do this on the same phone but in the future they will run on separate phones).
My code for socket communication is very similar to this: link
Difference is that my apps are running this as foreground services.
I've set the ip for the server on the client to 127.0.0.1 but they just won't connect (not in the emulator and not on real phone). What am I missing?
UPDATE:
I've found an easy way to get the device own IP address, so instead of localhost I use this (with www.google.com domain): answer to "java InetAddress.getLocalHost(); returns 127.0.0.1 … how to get REAL IP?"
Maybe it's not too nice but it works.
You'll need to set the local IP address of each one, so they can communicate within your LAN. So use an address like 192.168.1.X. Both if you're running your devices as virtual or physical, you may easily know the local IP address they have accessing your router's web interface and seeing their bound IPs.

Java RMI example that's not for localhost

I'm trying to write a program for Client/Server communication using RMI and this subject is new to me. I've looked at ton of examples to see if I can try to understand it and they all seem to be designed using local host. My program won't be run on localhost, it will be connecting to Server which is a whole different machine.
If anyone can show me just a simple example of how to establish a connection between two different machines using RMI that would be awesome. I don't need to see how its done for localhost, I've seen like million of those.
Thanks
You only have to change the client.
Change its Naming.lookup() string, from "localhost" to the server's hostname or IP address. The server's Naming string in the bind() or rebind() call does not change from "localhost", because the server and its Registry are always on the same host.
If you are using Registry instead of Naming, again you only have to change the client's LocateRegistry.getRegistry() call.
Let us assume we are connecting two Systems , A(server) with IP address 192.168.1.2 and B (client) with IP address 192.168.1.3 .
You should start the registry on server,i.e., system A then the Server Program should bind the Object like
Naming.rebind("rmi://192.168.1.2/myObject",obj);
Then compile the Client Program on system B which has the lookup function as
myInterface objHandle = (myInterface)Naming.lookup("rmi://192.168.1.2/myObject");
The main catch is that the two systems have to be on the same network for communicating, you might have to create your own network.

Java TCP/IP Socket internet connection problems

Im using TCP/IP sockets in java to try and create a client-server application. The program works fine when run locally and also over the local area network, but when I use the internet IP address the clients connection is refused.
I used this website to get my IP address and have added a firewall entry to unblock the port im using (port 4445).
I am almost certain the problem lies in some sort of security measure that is blocking the port. Does it matter that I'm running the client and server on the same PC but using the IP address from the previously mentioned website?
If I could get a list of ways to test the port is in fact open, or a list of things to try in order to get my program running, that would be great!
That website may very likely give you the IP address of the gateway through which your PC is connecting to the internet, and if the gateway is out of your control (which is most of the cases as far as I know) there's nothing you can do to use that IP address to test your program. Here's some advice:
Try http://aws.amazon.com, once registered you have one-year free access to a micro-server (which can be accessed publicly through DNS/Elastic IP.)
If your PC have a public IP address, you don't need that website to find out what it is. Just check your network adapter control panel.
Where is the server has been located? If your server is located in some commercial hosting, there is possibility that the ports you use are blocked. Also if you use modem with router or just router in your local network you should check nat table.

About the Spring RMI running in Linux system

When I packed my RMI applications and moved to Linux system and ran it.
The log shows that RMI services are running on server 127.0.0.1(Which was printed by method of InetAddress.getLocalhost()).
The configuration in Host file is "127.0.0.1 localhost.localdomain localhost", so I think RMI server was defalutly got the Localhost as servering IP.
After that, my RMI client try to invoke the RMI server method with its real IP(172.16.7.155) which caused a exception "Refused to connect 127.0.0.1".
There're two ways to reslove this problem. The one is modify Host file and reflect the localhost to real IP(172.16.7.155), but I can not modify it because other applications are using localhost domain.
Another way is to reset the method of fecthing IP address at RMI Server, i.e. instead the InetAddress.getLocalhost(), is there any configuration for this method?
You can probably fix this by starting your java rmi server process with the system property "-Djava.rmi.server.hostname=172.16.7.155" (or whatever the public ip of the box is). (details here)

Categories

Resources