Java – Get MAC Address of Host

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class MacAddress {

public static void main(String[] args) {
try {
//InetAddress address = InetAddress.getLocalHost();
InetAddress address = InetAddress.getByName("192.168.46.53");

/*
* Get NetworkInterface for the current host and then read the
* hardware address.
*/
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
if (ni != null) {
byte[] mac = ni.getHardwareAddress();
if (mac != null) {
/*
* Extract each array of mac address and convert it to hexa with the
* following format 08-00-27-DC-4A-9E.
*/
for (int i = 0; i < mac.length; i++) {
System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
}
} else {
System.out.println("Address doesn't exist or is not accessible.");
}
} else {
System.out.println("Network Interface for the specified address is not found.");
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
}
}
}

Java – Using Proxy Server with URLConnection

import java.net.URL;
import java.net.URLConnection;
import java.net.HttpURLConnection;

import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ProxyDemo {
	// Set to true if you want to see verbose output.
	private final static boolean bDebug = false;

	/**
	* This function makes an HTTP GET request of the specified URL using a proxy if provided.
	* If successfully, the HTTP response headers are printed out.
	* If the MIME type of the response is text/html, then the number of lines of text
	* is printed as well.
	*
	* @param strURL - A string representing the URL to request, eg, "http://javac.in/"
	* @param strProxy - A string representing either the IP address or host name of the proxy server.
	* @param iProxyPort - An integer that indicates the proxy port or -1 to indicate the default port for the protocol.
	* @return rc is true if the request succeeded and false otherwise.
	*/
	static boolean doURLRequest(String strURL, String strProxy, int iProxyPort) {
		boolean rc = false;

		URL url = null;
		URLConnection c = null;

		try {
			System.out.println("\nHTTP Request: " + strURL);

			URL urlOriginal = new URL(strURL);

			if ((null != strProxy) && (0 < strProxy.length())) {
				URL urlProxy = new URL(urlOriginal.getProtocol(),
				strProxy,
				iProxyPort,// A value of -1 means use the default port for the specified protocol.
				strURL);// The original URL is passed as "the file on the host".

				System.out.println("Using Proxy: " + strProxy);
				if (-1 != iProxyPort) {
					System.out.println("Using Proxy Port: " + iProxyPort);
				}

				url = urlProxy;
			} else {
				url = urlOriginal;
			}

			c = url.openConnection();

			// In this example, we only consider HTTP connections.
			if (c instanceof HttpURLConnection)// instanceof returns true only if the object is not null.
			{

				HttpURLConnection h = (HttpURLConnection) c;
				h.connect();

				String strStatus = h.getResponseMessage() + " (" + h.getResponseCode() + ")";
				System.out.println("HTTP Status: " + strStatus);

				System.out.println("HTTP Response Headers: ");

				// Evidently, index 0 always returns null, so we start with index 1.
				for (int i = 1; ; i++) {
					String strKey = h.getHeaderFieldKey(i);
					if (null == strKey) {
						break;
					}
					System.out.println(i + ": " + strKey + ": " + h.getHeaderField(i));
				}

				// Normally at this point, one would download data from the connection.
				// For example, if the MIME type is html, then download the string and display it.
				String strContentType = h.getContentType();
				if ((null != strContentType) && (0 == strContentType.compareTo("text/html"))) {
					if (bDebug) System.out.println("Received text/html:[");

						int iNumLines = 0;

						try {
							InputStream in = h.getInputStream();
							BufferedReader data = new BufferedReader(new InputStreamReader(in));

							String line = null;
							while((line = data.readLine()) != null) {
								if (bDebug) System.out.println(line);

								iNumLines++;
							}
						} catch(Exception exc2) {
							System.out.println("**** IO failure: " + exc2.toString());
						} finally {
							if (bDebug) System.out.println("]");
							System.out.println("Received text/html has " + iNumLines + " lines");
						}
				}

				h.disconnect();
			} else {
				System.out.println("**** No download: connection was not HTTP");
			}

			rc = true;
		} catch(Exception exc) {
			System.out.println("*** Connection Failure: " + exc.toString());

		} finally {
			// Do cleanup here.
			c = null;
			url = null;

			return rc;
		}
	}

	public static void main(String[] args) {
		// Simple request, not using a proxy server.
		ProxyDemo.doURLRequest("http://javac.in/", null, -1);
		ProxyDemo.doURLRequest("http://javac.in/", null, -1);

		// Request, using a proxy server.
		ProxyDemo.doURLRequest("http://javac.in/", "0.0.0.0", -1); // Change this line to use a valid proxy.
	}
}

Java – Find Hostname of Local Machine

import java.net.*;

class myName {

  public static void main (String args[]) {

    try {
      InetAddress address = InetAddress.getLocalHost();
      System.out.println("Hello. My name is " +  address.getHostName());
    }
    catch (UnknownHostException e) {
      System.out.println("I'm sorry. I don't know my own name.");
    }

  }

}

Page 1 of 212