下面列出了java.net.Inet4Address#getLocalHost ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public ErrorSpecIPv4(){
classNum = 6;
cType = 1;
length = 12;
bytes = new byte[length];
try{
errorNodeAddress = (Inet4Address) Inet4Address.getLocalHost();
}catch(UnknownHostException e){
}
flags = 0;
errorCode = 0;
errorValue = 0;
}
/**
*
*/
public SessionIPv4(){
classNum = 1;
cType = 1;
length = 12;
bytes = new byte[length];
try{
destAddress = (Inet4Address) Inet4Address.getLocalHost();
}catch(UnknownHostException e){
}
protocolId = 0;
flags = 0;
destPort = 0;
}
/**
* Constructs a new test instance manager
*/
public TestInstanceManager() {
final HardwareDescription hd = HardwareDescriptionFactory.construct(1, 1L, 1L);
final InstanceTypeDescription itd = InstanceTypeDescriptionFactory.construct(INSTANCE_TYPE, hd, 1);
instanceMap.put(INSTANCE_TYPE, itd);
this.allocatedResources = new ArrayList<AllocatedResource>();
try {
final InstanceConnectionInfo ici = new InstanceConnectionInfo(Inet4Address.getLocalHost(), 1, 1);
final NetworkTopology nt = new NetworkTopology();
final TestInstance ti = new TestInstance(INSTANCE_TYPE, ici, nt.getRootNode(), nt, hd);
this.allocatedResources.add(new AllocatedResource(ti, INSTANCE_TYPE, new AllocationID()));
} catch (UnknownHostException e) {
throw new RuntimeException(StringUtils.stringifyException(e));
}
}
@Before
public void setup() throws UnknownHostException {
final OsService osService = mock(OsService.class);
final OsStats osStats = mock(OsStats.class);
final MonitorService monitorService = mock(MonitorService.class);
when(monitorService.osService()).thenReturn(osService);
when(osService.stats()).thenReturn(osStats);
DiscoveryNode discoveryNode = newNode("node_name", "node_id");
postgresAddress = new TransportAddress(Inet4Address.getLocalHost(), 5432);
resolver = new NodeStatsContextFieldResolver(
() -> discoveryNode,
monitorService,
() -> null,
() -> new HttpStats(20L, 30L),
mock(ThreadPool.class),
new ExtendedNodeInfo(),
() -> new ConnectionStats(2L, 4L),
() -> postgresAddress,
() -> 12L,
() -> 1L
);
}
@EventListener
public void handleWebSocketConnectListener(SessionConnectedEvent event) {
InetAddress localHost;
try {
localHost = Inet4Address.getLocalHost();
LOGGER.info("Received a new web socket connection from:" + localHost.getHostAddress() + ":" + serverPort);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
public static String parseAddress(InetSocketAddress socketAddress) {
int port = socketAddress.getPort();
InetAddress localAddress = socketAddress.getAddress();
if (!isSiteLocalAddress(localAddress)) {
try {
localAddress = Inet4Address.getLocalHost();
} catch (UnknownHostException e) {
throw new RemotingException("No local address found", e);
}
}
return String.format("%s:%d", localAddress.getHostAddress(), port);
}
@EventListener
public void handleWebSocketConnectListener(SessionConnectedEvent event) {
InetAddress localHost;
try {
localHost = Inet4Address.getLocalHost();
LOGGER.info("Received a new web socket connection from:" + localHost.getHostAddress() + ":" + serverPort);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
/**
* Test method for
* {@link org.jvoicexml.implementation.marc.MarcFeedback#run()}.
*
* @exception Exception
* test failed
* @exception JVoiceXMLEvent
* test failed
*/
@Test
public void testRun() throws Exception, JVoiceXMLEvent {
final MarcSynthesizedOutput output = new MarcSynthesizedOutput();
output.connect(null);
output.addListener(this);
final SpeakableText speakable = new SpeakableSsmlText("test",
Locale.US);
final String sessionId = UUID.randomUUID().toString();
output.queueSpeakable(speakable, sessionId, null);
final MarcFeedback feedback = new MarcFeedback(output, 4011);
final DatagramSocket server = new DatagramSocket(4012);
feedback.start();
Thread.sleep(1000);
final String msg = "<event id=\"JVoiceXMLTrack:end\"/>";
final byte[] buf = msg.getBytes();
final InetAddress address = Inet4Address.getLocalHost();
final DatagramPacket packet = new DatagramPacket(buf, buf.length,
address, 4011);
server.send(packet);
final String msg2 = "<event id=\"SpeechCommand:end\"/>";
final byte[] buf2 = msg2.getBytes();
final DatagramPacket packet2 = new DatagramPacket(buf2, buf2.length,
address, 4011);
server.send(packet2);
synchronized (lock) {
lock.wait();
}
Assert.assertTrue(event instanceof OutputEndedEvent);
final OutputEndedEvent endedEvent = (OutputEndedEvent) event;
Assert.assertEquals(speakable, endedEvent.getSpeakable());
}
/**
* Creates a full URL so that the request can be sent
* @param req the request
* @param entityURL the partial URL (e.g. /thing/blah)
* @return a full URL (e.g. http://server/thing/blah)
*/
private String makeFullExternalURL(HttpServletRequest req, String entityURL) {
if (entityURL.startsWith("/")) {
// http client can only deal in complete URLs - e.g. "http://localhost:8080/thing"
String serverName = "localhost"; // req.getServerName();
try {
InetAddress i4 = Inet4Address.getLocalHost();
serverName = i4.getHostAddress();
} catch (UnknownHostException e) {
// could not get address, try the fallback
serverName = "localhost";
}
int serverPort = req.getLocalPort(); // getServerPort();
String protocol = req.getScheme();
if (protocol == null || "".equals(protocol)) {
protocol = "http";
}
StringBuilder sb = new StringBuilder(); // the server URL
sb.append(protocol);
sb.append("://");
sb.append(serverName);
if (serverPort > 0) {
sb.append(":");
sb.append(serverPort);
}
// look up the server URL using a service?
entityURL = sb.toString() + entityURL;
}
return entityURL;
}
/**
* Get instances of InetAddress by calling InetAddress.getLocalHost(),
* InetAddres4.getLocalHost() and Inet6Address.getLocalHost() and then test
* their properties.
* @param response HttpServletResponse used to return failure message
* @throws IOException
* @throws AssertionFailedException If an assertion fails
*/
private static void testGetLocalHost(HttpServletResponse response)
throws IOException, AssertionFailedException {
InetAddress localHost = InetAddress.getLocalHost();
testLocalHost(localHost, response);
testLocalHost4((Inet4Address) localHost, response);
localHost = Inet4Address.getLocalHost();
testLocalHost(localHost, response);
localHost = Inet6Address.getLocalHost();
testLocalHost(localHost, response);
}
public ResvConfirmIPv4(){
classNum = 15;
cType = 1;
length = 8;
bytes = new byte[length];
try{
receiverAddress = (Inet4Address) Inet4Address.getLocalHost();
}catch(UnknownHostException e){
}
}
public RSVPHopIPv4(){
classNum = 3;
cType = 1;
length = 12;
bytes = new byte[length];
try{
next_previousHopAddress = (Inet4Address) Inet4Address.getLocalHost();
}catch(UnknownHostException e){
}
logicalInterfaceHandle = 0;
}
public FilterSpecIPv4(){
classNum = 10;
cType = 1;
length = 12;
bytes = new byte[length];
try{
srcAddress = (Inet4Address) Inet4Address.getLocalHost();
}catch(UnknownHostException e){
}
srcPort = 0;
}
/**
* Creates a full URL so that the request can be sent
* @param req the request
* @param entityURL the partial URL (e.g. /thing/blah)
* @return a full URL (e.g. http://server/thing/blah)
*/
private String makeFullExternalURL(HttpServletRequest req, String entityURL) {
if (entityURL.startsWith("/")) {
// http client can only deal in complete URLs - e.g. "http://localhost:8080/thing"
String serverName = "localhost"; // req.getServerName();
try {
InetAddress i4 = Inet4Address.getLocalHost();
serverName = i4.getHostAddress();
} catch (UnknownHostException e) {
// could not get address, try the fallback
serverName = "localhost";
}
int serverPort = req.getLocalPort(); // getServerPort();
String protocol = req.getScheme();
if (protocol == null || "".equals(protocol)) {
protocol = "http";
}
StringBuilder sb = new StringBuilder(); // the server URL
sb.append(protocol);
sb.append("://");
sb.append(serverName);
if (serverPort > 0) {
sb.append(":");
sb.append(serverPort);
}
// look up the server URL using a service?
entityURL = sb.toString() + entityURL;
}
return entityURL;
}
@Override
protected void prepareForTest() throws Throwable {
clientTracingFilterInterceptor = new ClientTracingFilterInterceptor();
serverSocketAddress = new InetSocketAddress(Inet4Address.getLocalHost(), 9999);
serverAddr = Addresses$.MODULE$.newInetAddress(serverSocketAddress);
}
public ConnectionImpl(int port) throws IOException {
this(Inet4Address.getLocalHost(), port);
}