当前位置: 首页>>代码示例>>Java>>正文


Java GatewayDevice.getLocalAddress方法代码示例

本文整理汇总了Java中org.bitlet.weupnp.GatewayDevice.getLocalAddress方法的典型用法代码示例。如果您正苦于以下问题:Java GatewayDevice.getLocalAddress方法的具体用法?Java GatewayDevice.getLocalAddress怎么用?Java GatewayDevice.getLocalAddress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.bitlet.weupnp.GatewayDevice的用法示例。


在下文中一共展示了GatewayDevice.getLocalAddress方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: enableNATInboundConnections

import org.bitlet.weupnp.GatewayDevice; //导入方法依赖的package包/类
protected void enableNATInboundConnections() throws Exception {
    GatewayDiscover discover = new GatewayDiscover();
    LOGGER.debug("Looking for gateway devices");
    discover.discover();
    final GatewayDevice natDevice = discover.getValidGateway();
    if (null != natDevice) {
      LOGGER.debug("Gateway device found {} {} ", natDevice.getModelName(), natDevice.getModelDescription());
    } else {
      LOGGER.debug("No valid gateway device found, doing nothing.");
      return;
    }

//    String externalIPAddress = natDevice.getExternalIPAddress();
//    LOGGER.debug("Our external address is {}", externalIPAddress);
//    server1.setListeningAddress(externalIPAddress);

    final int localPortToMap=getListeningPort();
    LOGGER.debug("Querying device to see if a mapping for port {} already exists", localPortToMap);

    PortMappingEntry portMapping = new PortMappingEntry();
    if (natDevice.getSpecificPortMappingEntry(localPortToMap, "TCP", portMapping)) {
      LOGGER.error("Port {} was already mapped by {}. Aborting...", localPortToMap, portMapping.getPortMappingDescription()); //TODO Maybe we should retry with another port number. Do not forget to update the local gossip info with the new port
    } else {
      LOGGER.debug("External port {} is available, sending mapping request", localPortToMap);
      InetAddress localAddress = natDevice.getLocalAddress();
      if (natDevice.addPortMapping(localPortToMap, localPortToMap, localAddress.getHostAddress(), "TCP", getClass().getName())) {
        LOGGER.info("Port mapping successfull");
        Runtime.getRuntime().addShutdownHook(new Thread(){
          public void run() {
            try {
              LOGGER.debug("deleting port mapping {}", localPortToMap);
              natDevice.deletePortMapping(localPortToMap, "TCP");
            } catch (Exception e) {
              e.printStackTrace();
            }
          };
        });
      } else {
        LOGGER.error("Port mapping failed");
      }
    }
  }
 
开发者ID:pmarches,项目名称:peercentrum-core,代码行数:43,代码来源:NetworkServer.java

示例2: test

import org.bitlet.weupnp.GatewayDevice; //导入方法依赖的package包/类
@Test
public void test() throws Exception {
	GatewayDiscover discover = new GatewayDiscover();
	System.out.println("Looking for Gateway Devices");
	discover.discover();
	GatewayDevice d = discover.getValidGateway();

	if (null != d) {
		System.out.println("Gateway device found "+d.getModelName()+" " +d.getModelDescription());
	} else {
		System.out.println("No valid gateway device found.");
		return;
	}

	InetAddress localAddress = d.getLocalAddress();
	System.out.println("Using local address: "+ localAddress);
	String externalIPAddress = d.getExternalIPAddress();
	System.out.println("External address: "+ externalIPAddress);
	PortMappingEntry portMapping = new PortMappingEntry();
	
	System.out.println("Attempting to map port {0}" + SAMPLE_PORT);
	System.out.println("Querying device to see if mapping for port {0} already exists"+	SAMPLE_PORT);

	if (!d.getSpecificPortMappingEntry(SAMPLE_PORT, "TCP", portMapping)) {
		System.out.println("Sending port mapping request");

		if (d.addPortMapping(SAMPLE_PORT, SAMPLE_PORT, localAddress.getHostAddress(), "TCP", "libminiupnpc")) {
			System.out.println("Mapping succesful: waiting {0} seconds before removing mapping." + WAIT_TIME);

			Thread.sleep(1000 * WAIT_TIME);
			d.deletePortMapping(SAMPLE_PORT, "TCP");

			System.out.println("Port mapping removed");
			System.out.println("Test SUCCESSFUL");
		} else {
			System.out.println("Port mapping failed");
			fail();
		}

	} else {
		System.out.println("Port was already mapped. Aborting test.");
		fail();
	}

	System.out.println("Stopping weupnp");
}
 
开发者ID:pmarches,项目名称:peercentrum-core,代码行数:47,代码来源:WEUPnPTest.java


注:本文中的org.bitlet.weupnp.GatewayDevice.getLocalAddress方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。