Search code examples
androidsocketsinetaddress

InetAddress.getByName () adds / to the ip


I'm having a weird problem. I'm creating a socket and giving it the IP 192.168.43.255. When I use InetAddress.getByName(IP) it adds / to the ip as shown in the log below. Why this is happening ??

here is my code

public class ServerCom extends AsyncTask<String, Void , String>{

private int port=9999;
private String IP="192.168.43.255";
private BufferedReader input;
private Socket socket;
private DataOutputStream toSer;
private InetAddress serverAddr;
private String LocationID;
File file;FileWriter writer;

@Override
protected String doInBackground(String... RSS) {
    // TODO Auto-generated method stub
    Log.d("s","async");

    SandboxView.Locate=false;
    try {
            serverAddr = InetAddress.getByName(IP);
            socket = new Socket(serverAddr, port);
            toSer = new DataOutputStream(socket.getOutputStream());
            input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (UnknownHostException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    // send result to server

    try {
        toSer.writeBytes(RSS[0]+"\n");
        //get the response from server
        LocationID=input.readLine();

        toSer.close();
        socket.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   

    return LocationID;
}
  protected void onPostExecute(String result)
  {     
            super.onPostExecute(result);
            //dismiss progressdialog.
            //update ui
            MainActivity.LocationID=LocationID;
            SandboxView.Localization=MainActivity.CH2.getLocation(LocationID);
            try {
                writer = new FileWriter(file, true);
                writer.append("room :"+LocationID+"\n");
                writer.append(SandboxView.Localization.x+" "+SandboxView.Localization.y+"\n");
                writer.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            MainActivity.view.invalidate();

            SandboxView.Locate=true;
  }
@Override
protected void onPreExecute() {
        super.onPreExecute();

}

}

05-16 17:53:16.331: W/System.err(3495): java.net.ConnectException: /192.168.43.255:9999 - Network is unreachable

Solution

  • This isn't really adding a / to the IP address, but the format of the address output by InetAddress.toString() uses a / to separate the hostname from the host address. See here. So, the / isn't really being added to the address, it is just shown in the logging.