Using IP 127.0.0.1 for network communication - java

I have created a list of my friends' names. By clicking on their names one frame should get opened for me and a second one on the machine where the friend has the same application running.Just like the chat window in a messenger application. I use the IP address 127.0.0.1 fort this. Will this wor?
this is my list action performed:
private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {
ChatFrame frame = new ChatFrame(client);
frame.setVisible(true);
}

OK, let me see if I got this straight: you're creating something akin to an IM, right?
And you're using a connection on a port at 127.0.0.1?
As we all know the 127.0.0.1 is a loopback address, it will only point to one's machine. No matter what machine.
IF you're trying to create an IM, you need a server to handle the messages, OR know the remote address you're trying to connect (and a server there could be made to provide such address, that's how Yahoo Messenger does).

Related

How to discover the IP address of a Tomcat server on a network?

I have a Android application which consumes a webservice on a local network. There's a config screen where the user inform the server IP address, which is running Apache Tomcat.
I'm looking for a way to auto-detect the server based on the current connected wi-fi network.
i.e: The smartphone's IP is 10.1.1.90 and the server IP is 10.1.1.254.
Is there a way to achieve this? I'm thinking on using ping, but I don't know if is a good ideia.
The way I understand it, you need to discover IP of your tomcat server and connect it using your client.
I am assuming , both the server and client is in your control.
One simple way can be to use jGroups Cluster.
You can make your tomcat discoverable
Client can discover it using the name of the cluster you have provided .Refer the JChannel API that Jgroups uses
I simulated it making following server class
public class TomcatServer {
JChannel channel;
private void start() throws Exception {
channel = new JChannel(); // use the default config, udp.xml
channel.connect("TomcatCluster");
}
public static void main(String[] args) throws Exception {
new TomcatServer().start();
}
}
The simulated client class
public class MobileApp extends ReceiverAdapter {
JChannel channel;
private void start() throws Exception {
channel = new JChannel(); // use the default config, udp.xml
channel.setReceiver(this);
channel.connect("TomcatCluster");
channel.close();
}
public static void main(String args[]) throws Exception {
new MobileApp().start();
}
The client will provide you following information
GMS: address=MACHINENAME-47879, cluster=TomcatCluster, physical address=xxxxx:0:xxx:xxxx:xxxx:xxxx:xxx:xxxx:xxxx
** view: [MACHINENAME-31239|1] [MACHINENAME-31239, MACHINENAME-47879]
Where MACHINENAME-47879 is the client machine and port & MACHINENAME-31239 is the tomcat server name and port
Do you want to detect "a tomcat server" or "your tomcat server" ?
I mean, do you have any way to custom your server ? If it's the case, then you could create a very simple test page on your server (say a "Hello" JSP page), which your Android application could look for.
If your Android gets a "Hello" result with a GET request on http://<tomcat_ip>/hello.jsp, then you may assume that the tomcat is online.
If you can't add this test page, then you can test any page which the server is supposed to serve. (even a 404 page which sometimes is not configured well, and shows the tomcat version...)
Tomcat response headers can contain the xpoweredBy field that would advertise Tomcat if enabled. However it is most often disabled due security considerations, and even disabled by default. You however could re-enable it if you need to auto-detect exactly your Tomcat servers. From the other side, indeed, if you can place a web page on your server, you can simply place a marking page with the agreed signature.
If the server IP is unknown, I would propose the following ways to detect the server on the network:
The most straightforward way is to do the breadcast ping (ping -b broadcast_address where breadcast address can be computed here, for instance). All network devices that are configured so would reply, then verify as explained above which one is the server. However pinging broadcast address requires a rooted phone. Also the router may not support.
Your DHCP service (most likely your router) can often be configured to issue always the same IP address for the same MAC address of your server network card.
If the server is a desktop computer or laptop, it could show its address as QR code on display. It is possible for a smartphone to scan the code from the screen, and this is way easier than to enter IP address through the touchscreen. QR code can also include auto-generated password for extra security.
If there is wireless router with the possible login where both server and client are connected, the internal pages of that router often contain the relevant IP addresses. You would need to implement logging into the router and doing some screen scrapping.
I made an Android app which used a local server in the WLAN. I made the terminal (the phone) broadcast it's own IP address, which the server then picked up.
I used MultiCast class on the phone, which added the ip-address of itself to the payload. The server always has a thread in multicast read class that obains the payload of the packet (which is the terminals ip-address). Set the terminal in datagram read state and send the servers ip-address to terminal.
Maybe are better ways, but a great way to get the ip-addresses of unknown terminals in the network.
The way i had resolved this problem is with the use of enumerations.
public String getLocalIpAddress()
{
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (Exception ex) {
}
return null;
}
}

Java RMI connection to localhost at home network can't find correct remote module

I have been working on this project where two modules on different machines need to be in communication through RMI.
I start both client and server modules on my laptop. RMI seems to work correctly when i am at work and connected to work network, but when i am home, connected to my home network it does not work. It says remote object could not be found.
Here is the method i use at CLIENT side to get the reference to remote object
public static MyRMIApp getRemoteApp() throws RemoteException, NotBoundException, AccessException {
Registry registry = LocateRegistry.getRegistry("localhost", 28999); // tried 127.0.0.1 instead of localhost here, still not working
MyRMIApp app = (MyRMIApp) registry.lookup("COM");
return app;
}
Digging up a bit with some debugging, when i check the object value returned from getRemoteApp method, it shows me the end point is 67.215.65.132. Which is openDNS i am using to connect to internet. Shouldn't that be 127.0.0.1 ?
Then i used my mobile internet and tried again. It seems to be working but end-point is not 127.0.0.1 again it is the address assigned to me, which is 192.168.x.x
So can anybody please tell me what is wrong i am doing here ? I really would appreciate the help.
Oh and this is the piece of code at SERVER side
//Somwhere up top
private final static MyRMIApp rmiApp = new RMIServer();
//Down below
MyRMIApp stub = (MyRMIApp) UnicastRemoteObject.exportObject(rmiApp, 0);
Registry registry = LocateRegistry.createRegistry(28999);
registry.rebind("COM", stub);
See item A.1 of the RMI FAQ: specifically, 'The appropriate workaround is to set the system property java.rmi.server.hostname when starting the server.'

Get an unique id for a connected network device

I am connected to a WLAN, where a special hardware device is connected to as well. I communicate to that device via a socket, since I know its IP.
Is there a was to identify that hardware device in the network by an id? I found out in Java it is not possible to obtain the MAC-address of a connected device. Is there any other alternative?
Thanks,
best regards
Mac addresses should be unique. Maybe you can get needed information from the ARP table.
Command "arp -a" works on Windows and Linux.
But there is a problems:
This is not portable way
The ARP table is quite variable
If the device is behind a router, then this does not work.
In Java you can call NetworkInterface.getHardwareAddress() that will return hardware MAC address
Enumeration<NetworkInterface> enumNicList = NetworkInterface.getNetworkInterfaces();
while(enumNicList.hasMoreElements())
{
NetworkInterface oNic = enumNicList.nextElement();
byte[] baMacAddress = oNic.getHardwareAddress();
String sMacAddress = new BigInteger(1, baMacAddress).toString(16);
System.out.println(sMacAddress);
}
If you don't have any control of the responses of the device, and the device doesn't contain any identifying API calls and such, then just use the IP address and make that IP statically assigned to that device via your router. Then you can either create your own table of IP <-> device list, or even scrape the IP table off your router.
Come to think of it, you could probably get the MAC address the same way - scrape the DHCP table off your router's configuration screen.

How to get System IP using Java?

I was trying to make a jFrame which had a button & a text-area/label, the motive being able to retrieve my systems IP Address, the problem is, when I use this code
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
InetAddress ownIP=InetAddress.getLocalHost();
jTextField1.setText(ownIP.getHostAddress());
}
catch (Exception e)
{
jTextField1.setText(e.getMessage());
}
}
But then this gives me back the loop back IP Address, 127.0.0.1 :(
I have static IP configured on my system, but then too that IP does not show up
I use NetBeans IDE 7.0 & Ubuntu 11.04
You can obtain all IP addresses for your system. Use the NetworkInterface.getNetworkInterfaces() method to retrieve all of the network interfaces. For each of the returned interfaces, use the getInetAddresses() method to retrieve all of the associated addresses.
FWIW, InetAddress.getLocalHost().getHostAddress() gives me my real ip address.
FYI, I ran it from a unit test within Eclipse on a macbook.

After Port Forwarding, how to get my external IP in Java?

I set up a static IP and did port forwarding on my notebook, and now I have a static IP address, but it's relatively static, every time I re-start the machine, I get another address, and since I have a "static" IP I can now do Paypal IPN messaging. But how can I get this static IP from my Java program ? One way I can think of is to visit : http://portforward.com/ and on that page it tells me what my external IP is, so I can extract it with Java code, is there any other way that I can do in my Java code to get this info ?
The best solution is probably dynamic DNS. Essentially, you run a program on your computer (or router) that notifies a DNS server when your IP changes. Then, you can just tell PayPal the domain name.
There is a public service you can call with your script to retrieve your external IP address. Bear in mind they have changed the link location once. If you control your own server, you should probably write your own PHP script to simply return the address of the caller to the script.
http://www.whatismyip.com/faq/automation.asp - follow the developers link they provide
import java.net.*;
import java.io.*;
URL myExternalIP = new URL("PUT THE LINK HERE");
BufferedReader in = new BufferedReader(new InputStreamReader(
myExternalIP.openStream()));
String ip = in.readLine(); //you get the IP as a String
System.out.println(ip);
Your own server script is a simple one-liner. Create a file called whatismyip.php and here's the content.
<? echo $_SERVER['REMOTE_ADDR']?>
I see three ways of doing this:
As you discovered, querying an external server what IP you're apparently connecting from. This is simple but you require such a service to be available and, usually, that no transparent proxy messes with your results.
IGD, a sub-protocol of UPnP can give you the result very easily if your port forwarding devices supports it (many do). Google "IGD JAVA" for libraries and samples.
Register for a dynamic DNS service and then lookup your own DNS name.
You can use the NetworkInterface class:
Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces();
while(ifcs.hasMoreElements()){
NetworkInterface ifc = ifcs.nextElement();
System.out.println("Adresses of: "+ifc.getDisplayName());
Enumeration<InetAddress> adresses = ifc.getInetAddresses();
while(adresses.hasMoreElements()){
System.out.println(adresses.nextElement().getHostAddress());
}
}
This snippet will show you all of the interfaces and the IPs bound to them. You will need to look through the list to find the appropriate interface (see also NetworkInterface.getByName(String name)) And then look at the addresses for that interface. Once you have found the appropriate InetAdress you can use that to get the string or byte representation.

Categories

Resources