Java get REAL loopback address programmatically - java

I am developing a Java application which will listen a local port to communicate with another program. Normally it will listen to, say 127.0.0.1:8808.
One of my client reports that they are using Citrix environment, it is like MS remote desktop environment, multiple users can log in the same machine in the same time.
As you may know, such terminal server users share one loopback address, that means, only the first user run my application can successfully bind to 127.0.0.1:8808, the others will receive resource conflict error.
My client try to fix this by enable "virtual loopback", this setting can automatically assign different loopback address to different users, every user will get 127.0.0.2-127.0.0.255 as the loopback address instead of all 127.0.0.1, that sound like a perfect solution for this situation.
A test shows virtual loopback works well for other programs like chrome and IE, TCPView tool shows any connection to localhost are redirect to 127.0.0.2, except my program.
I am thinking the problem may be because I am using the fixed address 127.0.0.1 in the code, but I don't have an environment to confirm. So my question here is: Is there any Java method I can use to get the REAL loopback address in such situation, so I can bind to the the right address?
Thanks in advance!

Related

Is there a way to automatically discover an SQL database on your network?

I am setting up a MySQL database at my work and am also building an interface so people can interact with it.
The company already has a server that they use to host shared files and this will be used to run the database. I would like to know if it would be safe to hardcode the IP address of the server as it will be turned off and on in the evening and mornings.
If the IP is likely to change then is there a way I could find the IP address of the server automatically in the program, without the user having to do anything extra manually.
First you can use DNS and connect on and FQDN.
You can use a network scanner (for instance nmap) to search for open port 3306

Connecting to localhost from devices outside the network

I am currently doing an project at the university where we have to develop a multiplayer roleplay game with an client-server architecture. My part is to develop a Java Server which responds to client requests.
What do i have to do so my teammembers are able to connect to my localhost server?
If you want clients outside your local network to be able to see your server, you'll need to use something like ngrok. It's designed for exactly this. https://ngrok.com/
If your clients will be connecting from inside your network, they will need your machine's ip address. They can type it into their browser window. If you're running your java server on a port other than 80, then they'll need to add that to the url.
// from inside the network
example: http://192.168.1.125:80
example: http://192.168.1.125:3000
example: http://192.168.1.125:5000
// from outside the network
example: http://yourNgrokName.ngrok.com
Usually all the computers (i.e. macOS, linux, windows) have at least a double interface (localhost) which is not reachable outside and an external interface (wifi or ethernet) that is reachable from outside.
On most computer systems, localhost resolves to the IP address
127.0.0.1, which is the most commonly used IPv4 loopback address, and to the IPv6 loopback address ::1. The name localhost is also a
reserved top-level domain name, set aside to avoid confusion with the
definition as a hostname.
If you want that your team members are able to reach your computer, you have to use the address of this second interface.
But in this case, there could be many problems related to your personal firewall, network topology and etc.
The word outside itself is very generic because, outside could mean:
computers in my home network (small size)
computers in my office network (small-medium size), may be spread across more buildings
all the computers on the internet
Different network problems arises when you want that you personal computer is reachable from outside.

Strange RMI connection exception with Amazon EC2

I know this is something repetead but couldn't find the solution to my problem...
I am trying to implement a simulated distributed system and I struggling a lot with the connectivity through RMI.
I have three different components, grid schedulers, resources managers and clients. The three of them interact among themselves. The behaviour is the following: the user looks up resource manager reference on the registry and then invokes a remote method which will invoke another one in the grid scheduler and eventually one method in the user will be invoked delivering the results. If I execute the whole system in local everything works as expected.
Now the thing is, I am trying to move one of the resource mananger nodes to a EC2 Amazon instance with public address A.A.A.A, the rest of the system will run locally on my laptop with public address B.B.B.B. I allowed TCP traffic on the port 1099 TCP/UDP on the security group policy, also set the java.security.policy to grant all permision in both machines. To create the registry in every component I do:
LocateRegistry.createRegistry(1099);
Then the components will discover themselves by
Naming.lookup(url)
Now, user will look up for ip address A.A.A.A/component the public address of the Amazon instance. I can check that sometimes it gets the reference, sometimes it doesn't (NotBoundException). However, when it does gets the reference and tries to call the method I get:
java.rmi.ConnectException: Connection refused to host: INTERNAL IP OF AMAZON INSTANCE nested exception is: java.net.ConnectException: Connection timed out: connec.
Am I doing something wrong? I tried to set the property
java.rmi.server.hostname
To the public ip A.A.A.A in the Amazon instance and I got same results. I also tried doing the same on my laptop to the public address B.B.B.B and I got "connection refused IP B.B.B.B is not a localhost address"
I also opened the port in the router of my houe just in case that's the problem but it doesn't seem to be...
Am I missing something?
Thanks in advance.
Okay apparently, the issue was that even if the server listens on port 1099 then the port you use to connect the stub is random so I had to open all TCP ports in the security group of AWS. In that way it's working, however even if I turned off my windows firewall and open all the ports in the router it wasn't working my laptop - AWS, just different instances in AWS. But well, I can live with that...
Regarding the hostname you have to set it to the private address returned by
hostname -i
If you're going to deploy it inside AWS or to the public IP if you're connecting different instances such as your laptop and one EC2.
I hope this helps people on the same situation (have seen couple of questions with the same issue but no response).
Have a nice day!

How to host java application in aws?

I know its pretty silly question but I am not able to get it done.
I have a simple chat service. 2 java programs. One is client.java and other is server.java. It is developed using sockets. Now I tested it. It works perfectly well in the localhost.
I have created windows server 2012 EC2 instance on AWS and run my server.java through command prompt in window server. But when I am trying to connect to it from my machine via client.java, I am not able to connect to it.
What might be the reason?
AWS instances are usually not accessible from the outside. They are protected by an internal firewall and have private IP addresses. There is a "public" interface but this won't expose any port other than ssh or windows 53 remote access.
In brief: go to management console:
Go to "Elastic IP", get yourself an "elastic IP" and assign that to
your instance. This is the IP address, by which you may reach your
instance.
Go to "Security Groups". Add a new group or take the default. Add a
rule, opening the port of your java application to the public. Don't forget to assign that group to your instance, once you have created a new group.
That should do the trick ...

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.

Categories

Resources