当前位置: 首页>>代码示例>>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;未经允许,请勿转载。