VpnService中的套接字创建冻结了线程

时间:2015-02-24 15:00:58

标签: java android sockets vpn firewall

我正在尝试为Android实现无防火墙。我已经完成了关于SO的所有相关问题,但没有找到答案:

这里我正在配置我的vpn。

private void configure() {
    // If the old interface has exactly the same parameters, use it!
    if (mInterface != null) {
        Log.i(TAG, "Using the previous interface");
        return;
    }

    // Configure a builder while parsing the parameters.
    Builder builder = new Builder();
    builder.setMtu(1500);
    builder.addAddress("192.168.178.90", 24);
    //builder.addAddress("10.0.2.0", 32);
    //builder.addDnsServer("8.8.8.8");
    builder.addRoute("0.0.0.0", 0);  // to intercept packets
    try {
        mInterface.close();
    } catch (Exception e) {
        // ignore
    }
    mInterface = builder.establish();
}

然后我尝试将数据发送到目标地址

byte[] data= new byte[pdata.data.capacity()];
pdata.data.get(data);


Socket s = new Socket(pdata.destAddr,pdata.destPort);

if(shouldBeBlocked(pdata.destAddr)) {
    sendResult("blocked: "+ pdata.destAddr.toString()+":"+pdata.destPort);
} else {
    sendResult(pdata.destAddr.toString()+":"+pdata.destPort);
    if (protect(s)) { 
        ...

但是线程在Socket的构造函数上冻结,然后像这样rasinig IOException:

java.net.ConnectException: failed to connect to /173.194.71.100 (port 443): connect failed: ETIMEDOUT (Connection timed out)

1 个答案:

答案 0 :(得分:0)

也许尝试builder.setMtu(1492);处于更安全的一面。见http://en.wikipedia.org/wiki/Maximum_transmission_unit