Java Code Examples for java.net.Inet4Address#getLocalHost()

The following examples show how to use java.net.Inet4Address#getLocalHost() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ErrorSpecIPv4.java    From netphony-network-protocols with Apache License 2.0 6 votes vote down vote up
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;
	
}
 
Example 2
Source File: SessionIPv4.java    From netphony-network-protocols with Apache License 2.0 6 votes vote down vote up
/**
 * 
 */

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;
	
}
 
Example 3
Source File: TestInstanceManager.java    From stratosphere with Apache License 2.0 6 votes vote down vote up
/**
 * 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));
	}
}
 
Example 4
Source File: NodeStatsContextFieldResolverTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@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
    );
}
 
Example 5
Source File: WebSocketEventListener.java    From springboot-websocket-demo with Apache License 2.0 5 votes vote down vote up
@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);
    }

}
 
Example 6
Source File: RemotingUtil.java    From enode with MIT License 5 votes vote down vote up
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);
}
 
Example 7
Source File: WebSocketEventListener.java    From code with Apache License 2.0 5 votes vote down vote up
@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);
    }

}
 
Example 8
Source File: TestMarcFeedback.java    From JVoiceXML with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 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());
}
 
Example 9
Source File: EntityBatchHandler.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 10
Source File: TestInetAddressServlet.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 11
Source File: ResvConfirmIPv4.java    From netphony-network-protocols with Apache License 2.0 5 votes vote down vote up
public ResvConfirmIPv4(){
	
	classNum = 15;
	cType = 1;
	length = 8;
	bytes = new byte[length];
	try{
		receiverAddress = (Inet4Address) Inet4Address.getLocalHost();
	}catch(UnknownHostException e){
		
	}

	
}
 
Example 12
Source File: RSVPHopIPv4.java    From netphony-network-protocols with Apache License 2.0 5 votes vote down vote up
public RSVPHopIPv4(){
	
	classNum = 3;
	cType = 1;
	length = 12;
	bytes = new byte[length];
	try{
		next_previousHopAddress = (Inet4Address) Inet4Address.getLocalHost();
	}catch(UnknownHostException e){
		
	}
	logicalInterfaceHandle = 0;
	
}
 
Example 13
Source File: FilterSpecIPv4.java    From netphony-network-protocols with Apache License 2.0 5 votes vote down vote up
public FilterSpecIPv4(){
	
	classNum = 10;
	cType = 1;
	length = 12;
	bytes = new byte[length];
	try{
		srcAddress = (Inet4Address) Inet4Address.getLocalHost();
	}catch(UnknownHostException e){
		
	}
	srcPort = 0;
	
}
 
Example 14
Source File: EntityBatchHandler.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 15
Source File: ClientTracingFilterInterceptorTest.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Override
protected void prepareForTest() throws Throwable {
    clientTracingFilterInterceptor = new ClientTracingFilterInterceptor();
    serverSocketAddress = new InetSocketAddress(Inet4Address.getLocalHost(), 9999);
    serverAddr = Addresses$.MODULE$.newInetAddress(serverSocketAddress);
}
 
Example 16
Source File: ConnectionImpl.java    From deerlet-redis-client with Apache License 2.0 4 votes vote down vote up
public ConnectionImpl(int port) throws IOException {
	this(Inet4Address.getLocalHost(), port);
}