I want to obtain ip address by using this following classes, and accidentally found out that the ipAddress
from the following code sometimes contains un-printable characters such as DC2
, CAN
...I would like to see if you know what cause this result and how to prevent this? Having this up-printable char makes the string unpredictable and hard to do post-processing. Can anyone please share some experience to me?
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
String ipAddress = inetAddress.getHostAddress();
//this ipAddress contains some up-printable char..
}
}
The ipAddress
string is store in arrayList then send out to my server(in JsonArray). On server side, there is nothing magical, I only read the String from ArrayList then those special characters appears there.
s = JsonArray.getString(i);
All i can think about is....maybe the package sent from client got contaminated. What do you say?
I know a easy way to handle this is to remove the char DC2
and CAN
appended behind the string, however, i would like to see what cause this and all the possibility. Thanks.