public static String get() { try { final InetAddress addr = InetAddress.getLocalHost(); return addr.getHostName(); } catch (UnknownHostException e) { e.printStackTrace(); return "hostname-unknown"; } } }
private String getHost() { try { return InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); return "127.0.0.1"; } } }
/** * Get IP of the System. * * @return IP of the System */ public static String getIP() { InetAddress ip = null; try { ip = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } return ip.getHostName() + "@" + ip.getHostAddress(); }
inet = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace();
public InetSocketAddress toInetSocketAddress() { try { return new InetSocketAddress(InetAddress.getByAddress(sin_addr()), sin_port()); } catch (UnknownHostException e) { e.printStackTrace(); } return null; } }
/** * 域名解析 */ public static Map<String, Object> getDomainIp(String _dormain) { Map<String, Object> map = new HashMap<String, Object>(); long start = 0; long end = 0; String time = null; InetAddress[] remoteInet = null; try { start = System.currentTimeMillis(); remoteInet = InetAddress.getAllByName(_dormain); if (remoteInet != null) { end = System.currentTimeMillis(); time = (end - start) + ""; } } catch (UnknownHostException e) { end = System.currentTimeMillis(); time = (end - start) + ""; remoteInet = null; e.printStackTrace(); } finally { map.put("remoteInet", remoteInet); map.put("useTime", time); } return map; }
/** * 建立远程连接并发送命令 * * @param command * @param server * @param port */ private static void doSend(String command, String server, int port) { Socket socket = null; try { socket = new Socket(server, port); OutputStream os = socket.getOutputStream(); BufferedOutputStream out = new BufferedOutputStream(os); out.write(command.getBytes()); out.write('\r'); out.flush(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (socket != null && !socket.isClosed()) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
private Collection<InetSocketAddress> getServerAddresses(byte size) { ArrayList<InetSocketAddress> list = new ArrayList<InetSocketAddress>( size); while (size > 0) { try { list.add(new InetSocketAddress(InetAddress.getByAddress(new byte[]{10, 10, 10, size}), 1234 + size)); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } --size; } return list; }
inet = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace();
return read(socket.getInputStream()); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace();
@Test public void testSend() throws Exception { String host = "239.239.239.99";//多播地址 int port = 9998; String message = "test-multicastSocket"; try { InetAddress group = InetAddress.getByName(host); MulticastSocket s = new MulticastSocket(); //加入多播组 s.joinGroup(new InetSocketAddress(host, port), Utils.getLocalNetworkInterface()); DatagramPacket dp = new DatagramPacket(message.getBytes(), message.length(), group, port); s.send(dp); s.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
@Override public List<Node> getNodes(ByteArray key) { logger.debug("Giving pref to localhost ! "); List<Node> nodes = null; List<Node> reorderedNodes = new ArrayList<Node>(); try { nodes = super.getNodes(key); if(nodes == null) { return null; } String currentHost = InetAddress.getLocalHost().getHostName(); for(Node n: nodes) { if(currentHost.contains(n.getHost()) || n.getHost().contains(currentHost)) { logger.debug("Found localhost ! "); reorderedNodes.add(n); nodes.remove(n); break; } } reorderedNodes.addAll(nodes); nodes = reorderedNodes; } catch(VoldemortException e) { pipelineData.setFatalError(e); return null; } catch(UnknownHostException e) { e.printStackTrace(); return null; } return nodes; }
@Override public List<Node> getNodes(ByteArray key, Operation op) { List<Node> nodes = null; List<Node> reorderedNodes = new ArrayList<Node>(); try { nodes = super.getNodes(key, op); String currentHost = InetAddress.getLocalHost().getHostName(); for(Node n: nodes) { if(currentHost.contains(n.getHost()) || n.getHost().contains(currentHost)) { reorderedNodes.add(n); nodes.remove(n); break; } } reorderedNodes.addAll(nodes); nodes = reorderedNodes; } catch(VoldemortException e) { pipelineData.setFatalError(e); return null; } catch(UnknownHostException e) { e.printStackTrace(); return null; } return nodes; } }
public InetSocketAddress toInetSocketAddress() { try { Inet6Address address = Inet6Address.getByAddress(null, sin6_addr(), (int)sin6_scope_id()); return new InetSocketAddress(address, sin6_port()); } catch (UnknownHostException e) { e.printStackTrace(); } return null; } }
private List<Node> getTestNodes() { try { currentHost = InetAddress.getLocalHost().getHostName(); } catch(UnknownHostException e) { e.printStackTrace(); } return ImmutableList.of(node(0, "some-node-1", 2, 7, 14), node(1, "some-node-2", 1, 10, 13), node(2, currentHost, 3, 5, 17), node(3, "some-node-3", 0, 11, 16), node(4, "some-node-4", 6, 9, 15), node(5, "some-node-5", 4, 8, 12)); }
@Test public void testParameters() { SimpleFileNameFormat format = new SimpleFileNameFormat() .withName("$TIME.$HOST.$COMPONENT.$TASK.$NUM.txt") .withPath("/mypath") .withTimeFormat("yyyy-MM-dd HH:mm:ss"); Map<String, Object> topoConf = new HashMap(); format.prepare(null, createTopologyContext(topoConf)); long now = System.currentTimeMillis(); String path = format.getPath(); String name = format.getName(1, now); Assert.assertEquals("/mypath", path); String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now); String host = null; try { host = Utils.localHostname(); } catch (UnknownHostException e) { e.printStackTrace(); } Assert.assertEquals(time + "." + host + ".Xcom.7.1.txt", name); }
private static EventFactory honeycombEventFactory(String honeycombKey, String dataset, String testId) { HoneyClient honeyClient = LibHoney.create( LibHoney.options() .setWriteKey(honeycombKey) .setDataset(dataset) .build() ); if (honeycombKey == null || dataset == null) { return honeyClient.buildEventFactory().build(); } String hostName = ""; try { hostName = InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { e.printStackTrace(); } EventFactory.Builder builder = honeyClient.buildEventFactory() .addField("junit", "4") .addField("testId", testId) .addField("cpuCores", Runtime.getRuntime().availableProcessors()) .addField("hostname", hostName); for (Map.Entry entry : System.getProperties().entrySet()) { builder.addField(entry.getKey().toString(), entry.getValue()); } builder.addField("DB", System.getenv().get("DB")); builder.addField("SPRING_PROFILE", System.getenv().get("SPRING_PROFILE")); builder.addField("JAVA_HOME", System.getenv().get("JAVA_HOME")); return builder.build(); }
@Test public void testParameters() { SimpleFileNameFormat format = new SimpleFileNameFormat() .withName("$TIME.$HOST.$PARTITION.$NUM.txt") .withPath("/mypath") .withTimeFormat("yyyy-MM-dd HH:mm:ss"); format.prepare(null, 3, 5); long now = System.currentTimeMillis(); String path = format.getPath(); String name = format.getName(1, now); Assert.assertEquals("/mypath", path); String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now); String host = null; try { host = Utils.localHostname(); } catch (UnknownHostException e) { e.printStackTrace(); } Assert.assertEquals(time + "." + host + ".3.1.txt", name); }
public void run() { // Packets, Packet size,The target,Whether parsing IP // 包数,包大小,目标,是否解析IP Ping ping = new Ping(4, 32, "www.baidu.com", true); ping.start(); showLog(TAG, "Ping: " + ping.toString()); // target // 目标,可指定解析服务器 DnsResolve dns = null; try { // Add DNS service // 添加DNS服务器 dns = new DnsResolve("www.baidu.com", InetAddress.getByName("202.96.128.166")); dns.start(); showLog(TAG, "DnsResolve: " + dns.toString()); } catch (UnknownHostException e) { e.printStackTrace(); } // target port // 目标,端口 Telnet telnet = new Telnet("www.baidu.com", 80); telnet.start(); showLog(TAG, "Telnet: " + telnet.toString()); // target // 目标 TraceRoute traceRoute = new TraceRoute("www.baidu.com"); traceRoute.start(); showLog(TAG, "\n\nTraceRoute: " + traceRoute.toString()); } };
InetAddress.getByName("localhost"), 9300)); } catch (UnknownHostException e) { e.printStackTrace();