Showing posts with label Java Networking. Show all posts
Showing posts with label Java Networking. Show all posts

Wednesday, September 28, 2011

Get Mac Address in Java JDK 1.6

package org.best.example;

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

public class app{

   public static void main(String[] args){

    InetAddress ip;
    try {

        ip = InetAddress.getLocalHost();
       

        NetworkInterface network = NetworkInterface.getByInetAddress(ip);

        byte[] mac = network.getHardwareAddress();

        System.out.print("Current MAC address : ");

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));       
        }
        System.out.println(sb.toString());

    } catch (UnknownHostException e) {

        e.printStackTrace();

    } catch (SocketException e){

        e.printStackTrace();

    }

   }

}

Tuesday, September 27, 2011

Get IP Address in Java

package org.best.example;

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

public class app{

   public static void main(String[] args){

    InetAddress ip;
    try {

        ip = InetAddress.getLocalHost();
        System.out.println("Current IP address : " + ip.getHostAddress());

    } catch (UnknownHostException e) {

        e.printStackTrace();

    } catch (SocketException e){

        e.printStackTrace();

    }

   }

}

Monday, September 26, 2011

How to get MAC address in Java before JDK 1.6

package org.best.example;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class app{

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

       String command = "ipconfig /all";
       Process p = Runtime.getRuntime().exec(command);

       BufferedReader inn = new BufferedReader(new InputStreamReader(p.getInputStream()));
       Pattern pattern = Pattern.compile(".*Physical Addres.*: (.*)");

       while (true) {
            String line = inn.readLine();

        if (line == null)
            break;

        Matcher mm = pattern.matcher(line);
        if (mm.matches()) {
            System.out.println(mm.group(1));
        }
    }       
   }
}

Sunday, June 5, 2011

How to Send a POST Request with Parameters From a Java Class

This example shows how to sent a POST request to a server with attached parameters. Two parameters are sent in the example code below, width and height. We use the URL and URLConnection classes to open the connection to the destination. Then the output stream is retrieved by calling getOutputStream() on the URLConnection object. With the output stream we can write the parameters and then start reading the response from the server using the input stream which we get by calling getInputStream() on the same URLConnection object. We assume there will only be character based content returned from the server so we use the BufferedWriter to read the response line by line.

package org.best.example;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Main {
  
    /**
     * Extends the size of an array.
     */

    public void sendPostRequest() {
      
        //Build parameter string
        String data = "width=50&height=100";
        try {
          
            // Send the request
            URL url = new URL("http://www.somesite.com");
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
          
            //write parameters
            writer.write(data);
            writer.flush();
          
            // Get the response
            StringBuffer answer = new StringBuffer();
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                answer.append(line);
            }
            writer.close();
            reader.close();
          
            //Output the response
            System.out.println(answer.toString());
          
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    /**
     * Starts the program
     *
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        new Main().sendPostRequest();
    }
}