private String detectAgentUrl( HttpServer pServer, JolokiaServerConfig pConfig, String pContextPath) { serverAddress = pServer.getAddress(); InetAddress realAddress; int port; if (serverAddress != null) { realAddress = serverAddress.getAddress(); if (realAddress.isAnyLocalAddress()) { try { realAddress = NetworkUtil.getLocalAddress(); } catch (IOException e) { try { realAddress = InetAddress.getLocalHost(); } catch (UnknownHostException e1) { // Ok, ok. We take the original one realAddress = serverAddress.getAddress(); } } } port = serverAddress.getPort(); } else { realAddress = pConfig.getAddress(); port = pConfig.getPort(); } return String.format( "%s://%s:%d%s", pConfig.getProtocol(), realAddress.getHostAddress(), port, pContextPath); }
/** * Attempts to log a client in using the authentication server. Authentication server needs to run * on the same host as the file server. * * @throws IOException Error reading from socket. */ private void login() throws IOException { // set up required variables DatagramSocket clientSocket = new DatagramSocket(); InetAddress authServerIP = InetAddress .getLocalHost(); // because authentication server runs on same host as file server byte[] dataToSend; byte[] receivedData = new byte[BUFFER_SIZE]; // get username and password String userName = inFromClient.readLine().trim(); // get username String password = inFromClient.readLine().trim(); // get password dataToSend = new String(userName + " " + password).getBytes(); // send the username and password for processing by authentication server DatagramPacket packetToSend = new DatagramPacket(dataToSend, dataToSend.length, authServerIP, AUTHENTICATION_PORT); clientSocket.send(packetToSend); // receive the response from the authentication server DatagramPacket receivedPacket = new DatagramPacket(receivedData, receivedData.length); clientSocket.receive(receivedPacket); String receivedString = new String(receivedPacket.getData()).trim(); receivedData = receivedString.getBytes(); if (receivedString.equals("yes")) { outToClient.writeBytes(receivedString); // successful login } else { outToClient.writeBytes("no"); // unsuccessful login } }
public static void main(String args[]) throws UnknownHostException { ChatClient client = null; InetAddress address; if (args.length != 2) System.out.println("Usage: java ChatClient host port"); else { address = InetAddress.getByName(args[0]); client = new ChatClient(address, Integer.parseInt(args[1])); } }
/** @return */ public static String getIPv4Address() { String ipv4address = null; try { final List<NetworkInterface> networkinterfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (final NetworkInterface networkinterface : networkinterfaces) { final List<InetAddress> addresses = Collections.list(networkinterface.getInetAddresses()); for (final InetAddress address : addresses) { if ((address == null) || address.isLoopbackAddress()) { continue; } if (address instanceof Inet4Address) { ipv4address = address.getHostAddress().toString(); break; } } } } catch (Exception x) { DBG.m(x); } return ipv4address; }