import java.net.NetworkInterface; //導入方法依賴的package包/類
/**
* @return the maximum transfer unit of the network interface binded to the
* localhost.
*/
public static int getMaximumTransferUnit() {
try {
return NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).getMTU();
} catch (Throwable throwable) {
try {
/*
* We failed to get the NetworkInterface, we're gonna have to
* cycle through them manually and choose the lowest one to make
* sure we never exceed any hardware limitations
*/
boolean foundDevice = false;
int lowestMaximumTransferUnit = Integer.MAX_VALUE;
for (Enumeration networkInterfaces = NetworkInterface
.getNetworkInterfaces(); networkInterfaces.hasMoreElements(); ) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
int maximumTransferUnit = networkInterface.getMTU();
if (maximumTransferUnit < lowestMaximumTransferUnit
&& maximumTransferUnit >= RakNet.MINIMUM_TRANSFER_UNIT) {
lowestMaximumTransferUnit = maximumTransferUnit;
foundDevice = true;
}
}
// This is a serious error and will cause startup to fail
if (foundDevice == false) {
throw new IOException("Failed to locate a network interface with an MTU higher than the minimum ("
+ RakNet.MINIMUM_TRANSFER_UNIT + ")");
}
return lowestMaximumTransferUnit;
} catch (Throwable throwable2) {
throwable2.printStackTrace();
return -1;
}
}
}