2

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 CANappended behind the string, however, i would like to see what cause this and all the possibility. Thanks.

bj4947
  • 880
  • 11
  • 32
  • 1
    Looking at the source for `InetAddress` and its derivatives - that's impossible. You should show the part of the code where you believe you get those characters. – RealSkeptic Jul 30 '15 at 19:21
  • please see above changes. :D – bj4947 Jul 30 '15 at 23:04
  • Something in the communications between the client and the server is causing this. Please show the code that writes the data to the server's output stream and reads from the server. My wild guess would be that you are using `DataOutputStream` somewhere. – RealSkeptic Jul 31 '15 at 06:04

0 Answers0