本文整理汇总了Java中java.net.InetAddress.getByName方法的典型用法代码示例。如果您正苦于以下问题:Java InetAddress.getByName方法的具体用法?Java InetAddress.getByName怎么用?Java InetAddress.getByName使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.InetAddress
的用法示例。
在下文中一共展示了InetAddress.getByName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import java.net.InetAddress; //导入方法依赖的package包/类
public static void main(String[] args) {
if (args.length != 4) {
throw new RuntimeException(
"Test failed. usage: java JMXInterfaceBindingTest <BIND_ADDRESS> <JMX_PORT> <RMI_PORT> {true|false}");
}
int jmxPort = parsePortFromString(args[1]);
int rmiPort = parsePortFromString(args[2]);
boolean useSSL = Boolean.parseBoolean(args[3]);
String strBindAddr = args[0];
System.out.println(
"DEBUG: Running test for triplet (hostname,jmxPort,rmiPort) = ("
+ strBindAddr + "," + jmxPort + "," + rmiPort + "), useSSL = " + useSSL);
InetAddress bindAddress;
try {
bindAddress = InetAddress.getByName(args[0]);
} catch (UnknownHostException e) {
throw new RuntimeException("Test failed. Unknown ip: " + args[0]);
}
JMXAgentInterfaceBinding test = new JMXAgentInterfaceBinding(bindAddress,
jmxPort, rmiPort, useSSL);
test.startEndpoint(); // Expect for main test to terminate process
}
示例2: startClient
import java.net.InetAddress; //导入方法依赖的package包/类
public void startClient()
{
try // connect to server and get streams
{
// make connection to server
connection = new Socket(
InetAddress.getByName(ticTacToeHost), 12345);
// get streams for input and output
input = new Scanner(connection.getInputStream());
output = new Formatter(connection.getOutputStream());
}
catch (IOException ioException)
{
ioException.printStackTrace();
}
// create and start worker thread for this client
ExecutorService worker = Executors.newFixedThreadPool(1);
worker.execute(this); // execute client
}
示例3: write
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Writes the message to the stream.
*
* @param out
* Output stream to which message should be written.
*/
public void write(OutputStream out) throws SocksException, IOException {
if (data == null) {
Socks5Message msg;
if (addrType == SOCKS_ATYP_DOMAINNAME) {
msg = new Socks5Message(command, host, port);
} else {
if (ip == null) {
try {
ip = InetAddress.getByName(host);
} catch (final UnknownHostException uh_ex) {
throw new SocksException(
SocksProxyBase.SOCKS_JUST_ERROR);
}
}
msg = new Socks5Message(command, ip, port);
}
data = msg.data;
}
out.write(data);
}
示例4: extractRemoteIp
import java.net.InetAddress; //导入方法依赖的package包/类
private String extractRemoteIp(HttpServletRequest request) {
String forwardedHeader = request.getHeader("x-forwarded-for");
if (forwardedHeader != null) {
String[] addresses = forwardedHeader.split("[,]");
for (String address : addresses) {
try {
InetAddress inetAddress = InetAddress.getByName(address);
if (!inetAddress.isSiteLocalAddress()) {
return inetAddress.getHostAddress();
}
} catch (UnknownHostException e) {
LOGGER.debug("Failed to resolve IP for address: {}", address);
}
}
}
return request.getRemoteAddr();
}
示例5: getLocalHost
import java.net.InetAddress; //导入方法依赖的package包/类
public static InetAddress getLocalHost() {
String addr = prp.getProperty( "jcifs.smb.client.laddr" );
if (addr != null) {
try {
return InetAddress.getByName( addr );
} catch( UnknownHostException uhe ) {
if( log.level > 0 ) {
log.println( "Ignoring jcifs.smb.client.laddr address: " + addr );
uhe.printStackTrace( log );
}
}
}
return null;
}
示例6: testCaching
import java.net.InetAddress; //导入方法依赖的package包/类
@Test
public void testCaching() {
Configuration conf = new Configuration();
conf.setClass(
CommonConfigurationKeysPublic.NET_TOPOLOGY_NODE_SWITCH_MAPPING_IMPL_KEY,
MyResolver.class, DNSToSwitchMapping.class);
RackResolver.init(conf);
try {
InetAddress iaddr = InetAddress.getByName("host1");
MyResolver.resolvedHost1 = iaddr.getHostAddress();
} catch (UnknownHostException e) {
// Ignore if not found
}
Node node = RackResolver.resolve("host1");
Assert.assertEquals("/rack1", node.getNetworkLocation());
node = RackResolver.resolve("host1");
Assert.assertEquals("/rack1", node.getNetworkLocation());
node = RackResolver.resolve(invalidHost);
Assert.assertEquals(NetworkTopology.DEFAULT_RACK, node.getNetworkLocation());
}
示例7: HttpProxyCacheServer
import java.net.InetAddress; //导入方法依赖的package包/类
private HttpProxyCacheServer(Config config) {
this.config = checkNotNull(config);
try {
InetAddress inetAddress = InetAddress.getByName(PROXY_HOST);
this.serverSocket = new ServerSocket(0, 8, inetAddress);
this.port = serverSocket.getLocalPort();
IgnoreHostProxySelector.install(PROXY_HOST, port);
CountDownLatch startSignal = new CountDownLatch(1);
this.waitConnectionThread = new Thread(new WaitRequestsRunnable(startSignal));
this.waitConnectionThread.start();
startSignal.await(); // freeze thread, wait for server starts
this.pinger = new Pinger(PROXY_HOST, port);
LOG.info("Proxy cache server started. Is it alive? " + isAlive());
} catch (IOException | InterruptedException e) {
socketProcessor.shutdown();
throw new IllegalStateException("Error starting local proxy server", e);
}
}
示例8: testDecrementIPv4
import java.net.InetAddress; //导入方法依赖的package包/类
public void testDecrementIPv4() throws UnknownHostException {
InetAddress address660 = InetAddress.getByName("172.24.66.0");
InetAddress address66255 = InetAddress.getByName("172.24.66.255");
InetAddress address670 = InetAddress.getByName("172.24.67.0");
InetAddress address = address670;
address = InetAddresses.decrement(address);
assertEquals(address66255, address);
for (int i = 0; i < 255; i++) {
address = InetAddresses.decrement(address);
}
assertEquals(address660, address);
InetAddress address0000 = InetAddress.getByName("0.0.0.0");
address = address0000;
try {
address = InetAddresses.decrement(address);
fail();
} catch (IllegalArgumentException expected) {}
}
示例9: scaniprange2fun
import java.net.InetAddress; //导入方法依赖的package包/类
public void scaniprange2fun() {
int ipthreadnums = Integer.parseInt(ipthreads.getText().trim());
String striii;
//System.out.println(tip2);
InetAddress tipordomain2 = null;
//System.out.println(ipordomain.getText().trim());
try {
tipordomain2 = InetAddress.getByName(ipordomain.getText().trim());
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
//e1.printStackTrace();
}
String tip2 = tipordomain2.getHostAddress();
//System.out.print(tip2);
for(int iii=1;iii<=254;iii++) {
striii = Integer.toString(iii);
Lsaipscan.Scaniprange2.allipp.offer(tip2.split("\\.")[0]+"."+tip2.split("\\.")[1]+"."+tip2.split("\\.")[2]+"."+striii);
//System.out.print(tip2.split("\\.")[0]+tip2.split("\\.")[1]+"."+tip2.split("\\.")[2]+striii);
}
iparea.append("----------------Total ip nums: 254------------------\n");
ExecutorService executor = Executors.newFixedThreadPool(ipthreadnums);
for (int i = 0; i < ipthreadnums; i++) {
Scaniprange2 scaniprange2 = new Scaniprange2();
executor.execute(scaniprange2);
}
executor.shutdown();
try {
while (!executor.isTerminated()) {
Thread.sleep(1000);
}
} catch (Exception e2) {
//e2.printStackTrace();
}
iparea.append("----------------Over!!!------------------\n");
}
示例10: startThread
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Creates a new Thread object from this class and starts running
*/
public void startThread()
{
if (0 == this.rconPassword.length())
{
this.logWarning("No rcon password set in \'" + this.server.getSettingsFilename() + "\', rcon disabled!");
}
else if (0 < this.rconPort && 65535 >= this.rconPort)
{
if (!this.running)
{
try
{
this.serverSocket = new ServerSocket(this.rconPort, 0, InetAddress.getByName(this.hostname));
this.serverSocket.setSoTimeout(500);
super.startThread();
}
catch (IOException ioexception)
{
this.logWarning("Unable to initialise rcon on " + this.hostname + ":" + this.rconPort + " : " + ioexception.getMessage());
}
}
}
else
{
this.logWarning("Invalid rcon port " + this.rconPort + " found in \'" + this.server.getSettingsFilename() + "\', rcon disabled!");
}
}
示例11: setGroup
import java.net.InetAddress; //导入方法依赖的package包/类
public static void setGroup(String group) {
try {
TiandeMulticastSocket.group = InetAddress.getByName(group);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
示例12: unicast
import java.net.InetAddress; //导入方法依赖的package包/类
private void unicast(String msg, String host) {
if (logger.isInfoEnabled()) {
logger.info("Send unicast message: " + msg + " to " + host + ":" + mutilcastPort);
}
try {
byte[] data = (msg + "\n").getBytes();
DatagramPacket hi = new DatagramPacket(data, data.length, InetAddress.getByName(host), mutilcastPort);
mutilcastSocket.send(hi);
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
示例13: getAddressByName
import java.net.InetAddress; //导入方法依赖的package包/类
static
InetAddress getAddressByName(String host) {
try {
return InetAddress.getByName(host);
} catch(Exception e) {
if (e instanceof InterruptedIOException || e instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
LogLog.error("Could not find address of ["+host+"].", e);
return null;
}
}
示例14: testIPChange
import java.net.InetAddress; //导入方法依赖的package包/类
@Test
public void testIPChange() throws Exception
{
UUID uuid = UUID.fromString("abd86e8a-cf47-11e7-abc4-cec278b6b50a");
byte d1[] = ("[{ \"service\":\"DATABASE\", \"serverid\":\""+uuid+"\", \"data\":{\"hostname\": \"d1\"}}]").getBytes();
InetAddress a1 = InetAddress.getByName("192.168.1.10");
InetAddress a2 = InetAddress.getByName("192.168.1.20");
totest.addServiceListener((serverid, service, jsondata, up) -> {
System.out.println(serverid + ", " + service + ", " + jsondata + ", " + up);
if (up) {
activeuuid = serverid;
activeip = (InetAddress)jsondata.get("ip");
} else {
activeuuid = null;
activeip = null;
}
});
// message with a1 address
totest.processNetworkData(a1, d1, d1.length);
Assert.assertEquals(uuid, activeuuid);
Assert.assertEquals(a1, activeip);
// message with a2 addrses
totest.processNetworkData(a2, d1, d1.length);
Assert.assertEquals(uuid, activeuuid);
Assert.assertEquals(a2, activeip);
// now check a timeout
Thread.sleep(200);
Assert.assertEquals(uuid, activeuuid);
Assert.assertEquals(a2, activeip);
totest.checkForTimeouts();
Assert.assertEquals(null, activeuuid);
Assert.assertEquals(null, activeip);
}
示例15: createServerSocket
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Create a server socket.
* @param serviceUrl jmx service url
* @return server socket
* @throws IOException if an I/O error occurs when creating the socket
*/
@Override
public ServerSocket createServerSocket(final JMXServiceURL serviceUrl) throws IOException {
final InetAddress host = InetAddress.getByName(serviceUrl.getHost());
final SSLServerSocket baseSslServerSocket = (SSLServerSocket) sslContext.getServerSocketFactory()
.createServerSocket(serviceUrl.getPort(), 0, host);
LOGGER.log(Level.FINE, "Created server socket");
return baseSslServerSocket;
}