當前位置: 首頁>>代碼示例>>Java>>正文


Java DatagramSocket.getLocalAddress方法代碼示例

本文整理匯總了Java中java.net.DatagramSocket.getLocalAddress方法的典型用法代碼示例。如果您正苦於以下問題:Java DatagramSocket.getLocalAddress方法的具體用法?Java DatagramSocket.getLocalAddress怎麽用?Java DatagramSocket.getLocalAddress使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.net.DatagramSocket的用法示例。


在下文中一共展示了DatagramSocket.getLocalAddress方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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 "";
    }
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:22,代碼來源:Network.java

示例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();
}
 
開發者ID:drytoastman,項目名稱:scorekeeperfrontend,代碼行數:28,代碼來源:Network.java

示例3: UdpMultiClientServer

import java.net.DatagramSocket; //導入方法依賴的package包/類
/**
 * 
 * @param socket
 */
public UdpMultiClientServer(DatagramSocket socket) {
	super(DatagramSocket.class);
	this.socket = socket;
	this.port = socket.getLocalPort();
	this.address = socket.getLocalAddress();
	this.isRunning = new AtomicBoolean(false);
	this.name = "UdpMultiClientServer [" + address + ":" + socket.getLocalPort() + "]";
}
 
開發者ID:rtr-nettest,項目名稱:open-rmbt,代碼行數:13,代碼來源:UdpMultiClientServer.java

示例4: setLocalPort

import java.net.DatagramSocket; //導入方法依賴的package包/類
/**
 * {@inheritDoc}
 */
@Override
public void setLocalPort(int port) {
	if (isConnected() || !isOpened()) return;
	
	disconnect();
	
	try {
		socket = new DatagramSocket(port, socket.getLocalAddress());
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
開發者ID:Flash3388,項目名稱:FlashLib,代碼行數:16,代碼來源:UDPCommInterface.java

示例5: getInfo

import java.net.DatagramSocket; //導入方法依賴的package包/類
String getInfo(DatagramSocket soc) {
    if (soc == null) {
        return null;
    }

    return "localPort: " + soc.getLocalPort()
            + "; localAddress: " + soc.getLocalAddress()
            + "; remotePort: " + soc.getPort()
            + "; remoteAddress: " + soc.getInetAddress()
            + "; isClosed: " + soc.isClosed()
            + "; isBound: " + soc.isBound();
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:13,代碼來源:ReuseAddressTest.java


注:本文中的java.net.DatagramSocket.getLocalAddress方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。