I recently started a chat app that works with Firebase Auth & Database,
now for security reasons I want to save their IP's on the Database...not their local one (192.168...), I want to save their external (91.22....). I'll add this aswell to the GDPR but I don't know the code for that.
Is there a short Java Source Code?
like "android.os. ..." Code?
I also tried some else codes but it did not worked aswell.
I appreciate ya' answers :)
Is there a short Java Source Code?
No.
like "android.os. ..." Code?
No. The device does not necessarily know its external IP address, just like a desktop or notebook does not necessarily know its external IP address.
Make a request of your Web service, and have your Web service note what IP address the request comes from.
If you are using a socket you can do socket.getInetAddress().getHostAddress()
If you want the device you get it's own public IP address, you can use this method:
URL whatismyip = new URL("http://checkip.amazonaws.com");
BufferedReader in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
String ip = in.readLine(); //IP as a string
System.out.println(ip);
Related
I am deplyong a web application in the Internet and I am checking whether the user's login is valid by checking its client IP address (e.g. 192.168.2.XXX). Actually, I have found a working code (below). This code was completely working before, but after some time, its output is not the same anymore. Now, this code only gives me 1 IP address which is the server's IP address. What is wrong with my code? How can I get the client's static IP address rather than the server's IP address? I have also tried other solutions such as getRemoteAddr() and request.getHeader("PROXY-CLIENT-IP") but it is not working.
Java:
String ipAddress = request.getHeader("X-FORWARDED-FOR");
if(ipAddress == null)
ipAddress = InetAddress.getLocalHost().getHostAddress();
Your are mixing two levels of abstractions and that is rarely a good thing.
The header X-FORWARDED_FOR is inserted by a load balancer or proxy. If the client reaches the server directly, then this header isn't present and you are executing this code InetAddress.getLocalHost().getHostAddress();. Which does exactly what you say: It is retrieving the IP address of the host where this piece of code is running, i.e. the web server.
See also here: Getting the client IP address: REMOTE_ADDR, HTTP_X_FORWARDED_FOR, what else could be useful?
I'm trying to write a program that will show a DHCP client list in Java. I want to get the IP addresses, MAC addresses and the host names of all the devices connected to my wifi network.
I have a Belkin router. Its homepage has a 'DHCP client list' option which when clicked shows me this table :
That's exactly what I'm looking for. But I want to show all this data in the form of a list in a Java Swing program. I also want to be able to update this list by pressing a refresh button. Is there any way to achieve this?
It should look something like this :
I've written a basic java program that shows all the IP addresses that are online. Here's the code :
public static void main(String[] args) throws IOException {
InetAddress localhost = InetAddress.getLocalHost();
// this code assumes IPv4 is used
byte[] ip = localhost.getAddress();
for (int i = 1; i <= 254; i++)
{
ip[3] = (byte)i;
InetAddress address = InetAddress.getByAddress(ip);
if (address.isReachable(1000))
{
// machine is turned on and can be pinged
System.out.println(address + "is online");
}
else if (!address.getHostAddress().equals(address.getHostName()))
{
// machine is known in a DNS lookup
System.out.println(address + "is in a DNS lookup");
}
else
{
// the host address and host name are equal, meaning the host name could not be resolved
System.out.println(address + " is not online");
}
}
}
But this doesn't serve the purpose and it's really slow. I want to write a Swing program that'll show me the DHCP client list as seen in the image above.
Any help is appreciated.
I would think about three possible alternatives:
1-The one you implemented that is slow but it can work. You need to find a JAVA API to get the MAC addresses of received messages (I don't know if it exists or not). You can also send ARP messages asking "who has this IP address) and obtain the MAC address from the response. Use some Java interface for pcap library: jNetPcap vs Jpcap , http://jnetpcap.com/
2-Create an app that accesses your router web interface using HTTP and sending the appropriate messages with data as if you were using the UI. In this way you can programatically follow the steps a human would go and get the list that you browser shows, parse it and obtain the data.
3-If the router/access point provides a web API, which I doubt, you can use it.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Finding out your website visitor IP address in Java
I want to maintain logs for my website. For that I need to store the public IP of the visitor. How can I do that ? Till know I this was what I have been doing :
URL ip = new URL("http://api.externalip.net/hostname/");
BufferedReader br = new BufferedReader(new InputStreamReader(ip.openStream()));
String publicIP = br.readLine();
but this returns me the IP of the server , I guess where the page is hosted. For example I always get the IP as : 76.xxxxxxxx but when I check from whatismyip I have something like 106.xxxxxxx .
How can I get this IP ? Which is the public IP of the user from which it visits the website.
With every Request to your web application, client's IP is sent too. So all you need to do is to have Filter over Requests. Gain and store any information you like.
I'm programming a simple web browser depending on NanoHTTPD project and it's required to get number of visitors using the IP address.
Are there any ways to get the client IP using NanoHTTPD?
In NanoHTTPD.java, find the private class HTTPSession object.
Inside this is public void run(). Find the following line and add the second line after it.
decodeHeader(hin, pre, parms, header);
header.put("IPAddress", mySocket.getInetAddress().getHostAddress());
Now inside your serve function, you can just reference the IPAddress header to get the client's ip address.
I know the answer is probably too late to help you, but hopefully it'll help others searching for the same thing.
I found in the latest master branch, you can get the client ip address by header "http-client-ip" in the IHTTPSession session object.
I am developing an Android application were I am supposed to get the data from a database in csv/txt file format and later I have to send the files to a wifi printer.
Do anyone know how I might get started doing that?
The answer was finally easy :
Socket client = new Socket(_IP, PORT);
oStream = new PrintStream(client.getOutputStream(), true, "UTF-8");
oStream.println("-------------------------------------------------\r\n");
oStream.println(" NAME : DEMO CLIENT\r\n");
oStream.println(" CODE : 00000234242\r\n");
oStream.println(" ADDRESS : Street 52\r\n");
oStream.println(" Phone : 2310-892345\r\n");
oStream.println("-------------------------------------------------\r\n");
oStream.flush();
oStream.close();
client.close();
You could read a data from database to a file directly. and then you can connect the printer via sockets or wifi. And then pass on to the printer.
There are bunch of projects on github, maybe you can look at them, for example EasyPrinter.
You can do this using sockets. You can get examples in these links
http://examples.javacodegeeks.com/android/core/socket-core/android-socket-example/
Example: Android bi-directional network socket using AsyncTask
and you can google it. (Socket programming via java and android examples)
So first you must get your printer ip and port and send data to printer via a socket.
To be ui friendly, you can create a setting form where you can set available printers ip and port
Here's an Open Source project to print things with a Bixolon Bluetooth or WiFi printer on Android: https://github.com/rocboronat/FewlapsLovesBixolon