private static boolean isLocalhostWithNonLoopbackIpAddress(String ipAddress) { for (NetworkInterface networkInterface : localInterfaces) { Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses(); while (inetAddressEnumeration.hasMoreElements()) { InetAddress address = inetAddressEnumeration.nextElement(); if (!address.isLoopbackAddress() && ipAddress.equalsIgnoreCase(address.getHostAddress())) { return true; } } } return false; }
@Override OutputStream openNewOutputStream() throws IOException { final Socket socket = socketFactory.createSocket(); // Prevent automatic closing of the connection during periods of inactivity. socket.setKeepAlive(true); // Important not to cache `InetAddress` in case the host moved to a new IP address. socket.connect(new InetSocketAddress(InetAddress.getByName(host), port), connectionTimeoutMs); return new BufferedOutputStream(socket.getOutputStream(), sendBufferSize); } }
@Override public void run() { try (Socket socket = new Socket(InetAddress.getLocalHost(), serverPort)) { OutputStream outputStream = socket.getOutputStream(); PrintWriter writer = new PrintWriter(outputStream); sendLogRequests(writer, socket.getInputStream()); } catch (IOException e) { LOGGER.error("error sending requests", e); throw new RuntimeException(e); } }
private InetAddress getLocalHost() { try { return InetAddress.getLocalHost(); } catch (UnknownHostException ex) { return InetAddress.getLoopbackAddress(); } }
public String getLocalHostName() { try { if (null == m_localHost) { m_localHost = InetAddress.getLocalHost(); } return m_localHost.getHostName(); } catch (UnknownHostException e) { return m_local.getHostName(); } }
private static String generateId() { String host; try { host = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException ex) { host = "unknown"; } return host + '-' + UUID.randomUUID(); }
LOG.info("connecting to {} {}", host, port); Socket sock; InetSocketAddress hostaddress= host != null ? new InetSocketAddress(host, port) : new InetSocketAddress(InetAddress.getByName(null), port); if (secure) { LOG.info("using secure socket"); sock = new Socket(); sock.connect(hostaddress, timeout); sock.setSoTimeout(timeout); BufferedReader reader = null; try { new BufferedReader( new InputStreamReader(sock.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while((line = reader.readLine()) != null) { sb.append(line + "\n"); sock.close(); if (reader != null) { reader.close();
InetAddress address=InetAddress.getLocalHost(); Socket s1=null; String line=null; s1=new Socket(address, 4445); // You can use static final constant PORT_NUM br= new BufferedReader(new InputStreamReader(System.in)); is=new BufferedReader(new InputStreamReader(s1.getInputStream())); os= new PrintWriter(s1.getOutputStream()); line=br.readLine(); while(line.compareTo("QUIT")!=0){ os.println(line); os.flush(); response=is.readLine(); System.out.println("Server Response : "+response);
if (interfaceName != null) { if (interfaceName.trim().length() > 0) { bindInterface = InetAddress.getByName(interfaceName); serverSocket = new ServerSocket(port, -1, bindInterface); Log.debug("Flash cross domain is listening on " + interfaceName + " on port " + port); BufferedReader in = null; try { clientSocket = serverSocket.accept(); clientSocket.setSoTimeout(10000); // 10 second timeout out = new PrintWriter(clientSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); out.write(CROSS_DOMAIN_TEXT + XMPPServer.getInstance().getConnectionManager().getClientListenerPort() + CROSS_DOMAIN_END_TEXT+"\u0000"); out.flush(); out.close(); in.close();
port = NumbersUtil.parseInt(args[1], port); InetAddress addr = InetAddress.getByName(hostname); try(Socket socket = new Socket(addr, port); PrintWriter out = new PrintWriter(socket.getOutputStream(), true);) java.io.BufferedReader stdin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); String validationWord = "cloudnine"; String validationInput = ""; while (!validationWord.equals(validationInput)) { System.out.print("Enter Validation Word:"); validationInput = stdin.readLine(); System.out.println(); String input = stdin.readLine(); if (input != null) { out.println(input);
GroovyClientConnection(Script script, boolean autoOutput,Socket socket) throws IOException { this.script = script; this.autoOutputFlag = autoOutput; this.socket = socket; reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); writer = new PrintWriter(socket.getOutputStream()); new Thread(this, "Groovy client connection - " + socket.getInetAddress().getHostAddress()).start(); } public void run() {
int read = 0; final String client = mSocket.getInetAddress().getHostAddress(); BufferedReader bReader = new BufferedReader(new InputStreamReader(byteArrayInputStream)); StringBuilder builder = new StringBuilder(); String line = null; boolean headersProcessed = false; while((line = bReader.readLine()) != null){ if(headersProcessed == false){ headers.add(line); mWriter = new BufferedOutputStream(mSocket.getOutputStream()); mWriter.close(); } else{ mServerReader = mServer.getInputStream(); mServerWriter = mServer.getOutputStream();
SocketAddress address = new InetSocketAddress( InetAddress.getByName(hostname), port ); sock.connect(address, SOCKET_TIMEOUT); sock.setSoTimeout(SOCKET_TIMEOUT); reader = new ReaderGuacamoleReader(new InputStreamReader(sock.getInputStream(), "UTF-8")); writer = new WriterGuacamoleWriter(new OutputStreamWriter(sock.getOutputStream(), "UTF-8"));
public TcpConnector connect() { if (isClosed()) { log.infof("Connect socket <-> %s:%d", host, port); try { socket = new Socket(InetAddress.getByName(host), port); socket.setTcpNoDelay(true); reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); writer = new OutputStreamWriter(socket.getOutputStream()); } catch (UnknownHostException e) { log.warnf("Unknown host '%s:%d'", host, port); throw Lang.wrapThrow(e); } catch (IOException e) { log.warnf("IOError '%s:%d'", host, port); throw Lang.wrapThrow(e); } } return this; }
LOG.info("Setting environments for the MXNet scheduler"); dmlcPsRootUri = applicationMasterHostname; Socket schedulerReservedSocket = new Socket(); try { Utilities.getReservePort(schedulerReservedSocket, InetAddress.getByName(applicationMasterHostname).getHostAddress(), reservePortBegin, reservePortEnd); } catch (IOException e) { LOG.error("Can not get available port"); dmlcPsRootPort = schedulerReservedSocket.getLocalPort(); List<String> schedulerEnv = new ArrayList<>(20); Map<String, String> userEnv = new HashMap<>(); Utilities.getReservePort(schedulerReservedSocket, InetAddress.getByName(applicationMasterHostname).getHostAddress(), reservePortBegin, reservePortEnd); } catch (IOException e) { LOG.error("Can not get available port"); InetAddress address = null; try { address = InetAddress.getByName(applicationMasterHostname); dmlcPsRootUri = address.getHostAddress(); } catch (UnknownHostException e) { LOG.info("acquire host ip failed " + e); Utilities.getReservePort(schedulerReservedSocket, InetAddress.getByName(applicationMasterHostname).getHostAddress(), reservePortBegin, reservePortEnd); } catch (IOException e) { LOG.error("Can not get available port");
private Inet6Address getLocalIPv6Address() { try { Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { NetworkInterface netInterface = e.nextElement(); Enumeration<InetAddress> ee = netInterface.getInetAddresses(); while (ee.hasMoreElements()) { InetAddress addr = ee.nextElement(); if (addr instanceof Inet6Address && (!addr.isLoopbackAddress()) && (!addr.isAnyLocalAddress())) { InetSocketAddress socketAddress = new InetSocketAddress(addr, 0); ServerSocket sock = new ServerSocket(); sock.bind(socketAddress); sock.close(); ActorSystem as = AkkaUtils.createActorSystem( new Configuration(), new Some<scala.Tuple2<String, Object>>(new scala.Tuple2<String, Object>(addr.getHostAddress(), port))); as.shutdown();
bootstrap.setOption("child.tcpNoDelay", true); bootstrap.setOption("child.keepAlive", true); Channel listenChannel = bootstrap.bind(new InetSocketAddress(port)); allChannels.add(listenChannel); logger.info("Started server on port {}", port); Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface ifc = interfaces.nextElement(); if (!ifc.isLoopback()) { for (InterfaceAddress ifadr : ifc.getInterfaceAddresses()) { InetAddress adr = ifadr.getAddress(); logger.info("Listening on network interface \"{}\", hostname \"{}\" ({})", ifc.getDisplayName(), adr.getCanonicalHostName(), adr.getHostAddress());
private String getDefaultAddress() { Enumeration<NetworkInterface> nets; try { nets = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { return null; } NetworkInterface netinf; while (nets.hasMoreElements()) { netinf = nets.nextElement(); Enumeration<InetAddress> addresses = netinf.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); if (!address.isAnyLocalAddress() && !address.isMulticastAddress() && !(address instanceof Inet6Address)) { return address.getHostAddress(); } } } return null; }
LOG.info("Reserved available port: " + reservedSocket.getLocalPort()); this.lightLDALocalPort = reservedSocket.getLocalPort(); InetAddress address = null; try { address = InetAddress.getByName(envs.get(ApplicationConstants.Environment.NM_HOST.toString())); } catch (UnknownHostException e) { LOG.info("acquire host ip failed " + e); reportFailedAndExit(); String ipPortStr = this.index + " " + address.getHostAddress() + ":" + this.lightLDALocalPort; this.lightLDAEndpoint = address.getHostAddress() + ":" + this.lightLDALocalPort; LOG.info("lightLDA ip port string is: " + ipPortStr); amClient.reportLightLDAIpPort(containerId, ipPortStr); InetAddress address = null; try { address = InetAddress.getByName(envs.get(ApplicationConstants.Environment.NM_HOST.toString())); } catch (UnknownHostException e) { LOG.info("acquire host ip failed " + e); reportFailedAndExit(); String ipPortStr = address.getHostAddress() + " " + reservedSocket.getLocalPort(); LOG.info("lightGBM ip port string is: " + ipPortStr); amClient.reportLightGbmIpPort(containerId, ipPortStr); Utilities.getReservePort(boardReservedSocket, InetAddress.getByName(localHost).getHostAddress(), reservePortBegin, reservePortEnd); } catch (IOException e) { LOG.error("Can not get available port");
private static InetAddress getLocalAddress0() { InetAddress localAddress = null; try { localAddress = InetAddress.getLocalHost(); Optional<InetAddress> addressOp = toValidAddress(localAddress); if (addressOp.isPresent()) { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); if (null == interfaces) { return localAddress; while (interfaces.hasMoreElements()) { try { NetworkInterface network = interfaces.nextElement(); Enumeration<InetAddress> addresses = network.getInetAddresses(); while (addresses.hasMoreElements()) { try { Optional<InetAddress> addressOp = toValidAddress(addresses.nextElement());