java - VpnService.java中的 'protect'方法到底是做什么的

标签 java android sockets vpn

我正在浏览 ToyVpn 项目中的。


 // Create a DatagramChannel as the VPN tunnel.
 tunnel =;

 // Protect the tunnel before connecting to avoid loopback.
 if (!protect(tunnel.socket())) {
      throw new IllegalStateException("Cannot protect the tunnel");

我知道第一行创建了一个数据报/UDP channel 来连接到远程服务器。但我不明白“保护”方法到底在做什么。



它是来自 library 的函数如您在 the source code 中所见,它是在 类中导入的


public boolean protect (int socket)

Added in API level 14

Protect a socket from VPN connections. After protecting, data sent through this socket will go directly to the underlying network, so its traffic will not be forwarded through the VPN. This method is useful if some connections need to be kept outside of VPN. For example, a VPN tunnel should protect itself if its destination is covered by VPN routes. Otherwise its outgoing packets will be sent back to the VPN interface and cause an infinite loop. This method will fail if the application is not prepared or is revoked.

The socket is NOT closed by this method.

Returns true on success.

关于java - VpnService.java中的 'protect'方法到底是做什么的,我们在Stack Overflow上找到一个类似的问题:


Java HW StackOverflowError 与从书中复制的 Max Subarray 伪代码

android - 如何在布局代码中轻松访问 anko 组件

c - 原始套接字中的错误文件描述符

java - 在java中处理异常而不导致服务器套接字崩溃?

java - 将 arraylist 中的每个值显示到每个 textView Android

JAVAFX:在 FXML 之间传递数据不起作用

android - 如何加速我的 Android 应用程序?

不滚动的Android ListView?

c - 没有目标端口的 UNIX sendto()

java - Websphere ScheduledExecutorService 线程