0

我有一个奇怪的问题。我正在创建一个套接字并为其分配 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
4

1 回答 1

2

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

于 2014-05-16T16:05:41.963 回答