I have a server socket in my android app and the following lines (listening/waiting for a connection):
rclient = serverSocket.accept();
String stt=rclient.getInetAddress().getHostAddress();
I get this : ::1
Can anyone kindly tell me whats my mistake here ?
I have also tried getRemoteSocketAddress, However, that also gives some crap output./::1/::0142342 ...
thanks.
You are getting correct outputs. It's not clear why you think they're "crap output". They are in fact the correct IP addresses. ::1 is a valid IP address, it is the IPv6 address assigned to the loopback adapter.
Related
IPAddress are returning different on LAN net and ZONG 4G.
I want to know the IP addresses of clients in java application so i can restrict the users for login and other roles.
But problem is that when i run below code on LAN net it returns correct IPV4 address, But if I connect Zong 4G Device it only returns 192.168.10.100 on any computer.
How to get IP Address of client?
My code:
InetAddress address = InetAddress.getLocalHost();
String ip = address.getHostAddress();
String host = address.getHostName();
System.out.println("IP Address = " + ip);
System.out.println("host= " + host);
192.168.10.100 mean that your client is behind the NAT. There is thing called UPnP that may help you (I'm not expert and not really sure).
Alternative approach is to connect to server and ask it what it thing your IP is. It also have limitation, clients behind same NAT will have same IP.
In general building security based on IP address is bad idea.
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 have been using the following code on a server.
public SocketServer(int port,String inetAddress) throws IOException {
this.port = port;
this.ia = InetAddress.getByName(inetAddress);
log.info(String.format("Internet Address %s using port %d for provided IP Address %s", this.ia.toString() ,this.port ,inetAddress.toString()));
s = new ServerSocket(port,50,this.ia );
}
This works fine on my local server but on production it is providing wrong address.
Production server do contain following type of IPs:-
Private IP
VPN IP
Public IP
I am providing private IP and expecting the server to connect using that private IP but instead it is connecting using the VPN IP.
One more thing i though to do was to use InetAddress.getByAddress() but i am unable to convert my IP in string to a byte array.
Can anyone suggest me any solution in this regard?
If I am not mistaken this might be a problem related to DNS. InetAddress.getByName(String host) will return the first IP address assigned to a certain domain name.
I.e. if in your /etc/hosts file you have something like this
192.168.1.1 sandbox1
192.168.1.2 sandbox1
The code
InetAddress.getByName("sandbox1")
will always give you 192.168.1.1
Hope this helps!
Do java.net.InetSocketAddress and java.net.SocketAddress support IPv6?
Because the two classes that i have research and try it couldnt able me to view ipv6 connection it only able to view ipv4 connection anyone there have try and able to do the editing of the code?
A partial answer to your question, even though it may come too late to be of immediate use =).
I had the following lines of code attempt to connect to two different IP addresses:
// Defined port must be 13; that is NIST's default listening port.
SocketAddress socketAddress = new InetSocketAddress(currentIpAddress, 13);
socket = new Socket();
socket.connect(socketAddress, nistServerTimeoutInMilliseconds);
The first address works fine : '206.246.122.250'
The second address throws a 'java.net.SocketException: Protocol family unavailable' on the third line. That address is '2610:20:6F15:15::27'.
I thought at first that the problem was that IPv6 is not supported by InetSocketAddress in Java 1.7, but I found out that there are actually two classes that inherit from the InetAddress class in Java: Inet4Address and Inet6Address
http://docs.oracle.com/javase/6/docs/api/java/net/Inet4Address.html
http://docs.oracle.com/javase/7/docs/api/java/net/Inet6Address.html
This explains why my code, when trying to construct an Inet4Address using an IPv6 format was failing, and it may explain your issue as well.
Hope this helps =)
P.S. InetAddress and InetSocketAddress aren't the same class, but by testing for the kind of IP address you're dealing with, there may be a way to get around the problem.
i have got an error in this line:
new ServerSocket(2106, 50, InetAddress.getByName("83.4.200.1"));
Error log:
Exception in thread "main" java.net.BindException: Cannot assign requested address: JVM_Bind
83.4.200.1 is my ip, when i put there 127.0.0.1 or 192.168.1.2 with same port, everything is working perfect. I have checked all ports by writing netstat -a -n, but 2106 isnt there.
Thanks a lot for reading this, i hope that u can help me with my problem
Your routers address is 83.4.200.1. It's important to note that this isn't the address that your computer responds to, but rather the internal network address 192.168.1.2. If you want to connect to your program from outside the router, you needs to set up port forwarding for 2106 on the router.
1. If you want to access this Server with IP: "83.4.200.1" through Internet, then it must
be your static ip, rather than an dynamic one.
2. Try to run this code with a private ip address or public ip address which is assigned to your pc in LAN (ie. Without internet..JUST WITH WIRELESS CONNECTION).
3. Private ip or Public ip has No meaning until and unless you are on INTERNET.. TILL THEN YOU CAN USE BOTH, AS ITS LAN.
4. Private ip ranges
Class A : 10.0.0.0 - 10.0.0.255
Class B : 172.16.0.0 - 172.31.255.255
Class C : 192.168.0.0 - 192.168.255.255
5. Public is given by your service provider, which will be anyone OUT of the private ip range. If your ip is not static, there is hardly or none of your chances to access the server over internet, there are sites that gives static ip out of your dynamic ips.
83.4.200.1 is my ip
It is the IP address of your router.
It isn't an IP address of the host you are running your code in, so you can't bind to it. You need to bind to a local address of that host, and arrange port forwarding from the router to your host. Most usually the bind-address is best omitted altogether, just specifying a port, in which case the socket will listen on all local IP addresses.