InetAddress.getByName()添加/到ip

时间:2014-05-16 15:07:22

标签: android sockets inetaddress

我有一个奇怪的问题。我正在创建一个套接字并给它IP 192.168.43.255。当我使用InetAddress.getByName(IP)时,它会添加/到ip,如下面的日志所示。为什么会发生这种情况?

这是我的代码

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

1 个答案:

答案 0 :(得分:1)

这并不是真正添加/到IP地址,但InetAddress.toString()输出的地址格式使用/来将主机名与主机地址分开。见here。因此,/并没有真正添加到地址中,它只是在日志记录中显示。