本文整理匯總了Java中java.net.DatagramSocket.connect方法的典型用法代碼示例。如果您正苦於以下問題:Java DatagramSocket.connect方法的具體用法?Java DatagramSocket.connect怎麽用?Java DatagramSocket.connect使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.net.DatagramSocket
的用法示例。
在下文中一共展示了DatagramSocket.connect方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getIp
import java.net.DatagramSocket; //導入方法依賴的package包/類
public static String getIp() {
IOException e;
try {
DatagramSocket socket = new DatagramSocket();
try {
socket.connect(InetAddress.getByName("114.114.114.114"), 53);
InetAddress local = socket.getLocalAddress();
socket.close();
return local.getHostAddress();
} catch (IOException e2) {
e = e2;
DatagramSocket datagramSocket = socket;
e.printStackTrace();
return "";
}
} catch (IOException e3) {
e = e3;
e.printStackTrace();
return "";
}
}
示例2: getPrimaryAddress
import java.net.DatagramSocket; //導入方法依賴的package包/類
/**
* For multihomed machines (i.e. anything with Docker Machine), use the
* routing table default route to find what should be the primary network
* facing interface.
* @return an InetAddress for the primary address
*/
public static InetAddress getPrimaryAddress()
{
InetAddress ret;
try {
DatagramSocket s = new DatagramSocket();
s.connect(InetAddress.getByAddress(new byte[]{1,1,1,1}), 0);
ret = s.getLocalAddress();
s.close();
return ret;
} catch (SocketException | UnknownHostException se) {
log.info("get by route failed: " + se);
}
try {
return InetAddress.getLocalHost();
} catch (UnknownHostException ex) {
log.info("getLocalHost failed: " + ex);
}
return InetAddress.getLoopbackAddress();
}
示例3: determineLocalIp
import java.net.DatagramSocket; //導入方法依賴的package包/類
static String determineLocalIp() {
try {
DatagramSocket s = new DatagramSocket();
s.connect(InetAddress.getByName("192.168.1.1"), 80);
return s.getLocalAddress().getHostAddress();
} catch (IOException e) {
Log.e("determineLocalIp()", e.getMessage() + "");
// dont do anything; there should be a connectivity change going
return null;
}
}