package javawhois; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.logging.Level; import java.util.logging.Logger; /** * @web http://java-buddy.blogspot.com/ */ public class JavaWhoIs { private final static String WHO ="google.com"; private final static String WHOIS_HOST = "whois.internic.net"; private final static int WHOIS_PORT = 43; public static void main(String[] args) { int c; Socket socket = null; String query = "=" + WHO + "\r\n"; byte buf[] = query.getBytes(); try { socket = new Socket(WHOIS_HOST, WHOIS_PORT); InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream(); out.write(buf); out.flush(); while ((c = in.read()) != -1) { System.out.print((char) c); } System.out.print("\nDone\n"); } catch (IOException ex) { Logger.getLogger(JavaWhoIs.class.getName()).log(Level.SEVERE, null, ex); System.out.print(ex.getMessage()); } finally { if(socket != null){ try { socket.close(); } catch (IOException ex) { Logger.getLogger(JavaWhoIs.class.getName()).log(Level.SEVERE, null, ex); } } } } }
Wednesday, November 11, 2015
Query InterNIC server's whois
Last example show Java example using WhoisClient class to query whois. Alternatively, we can query InterNIC server's whois without WhoisClient class.
Sunday, November 8, 2015
Java example using WhoisClient class
The WhoisClient class implements the client side of the Internet Whois Protocol defined in RFC 954. To query a host you create a WhoisClient instance, connect to the host, query the host, and finally disconnect from the host. If the whois service you want to query is on a non-standard port, connect to the host at that port.
~ org.apache.commons.net.whois.WhoisClient
Example:
To use WhoisClient class in your program, you have to include library commons-net-3.3 in your code, it can be download here: https://commons.apache.org/proper/commons-net/index.html
This video show how to download and add library commons-net-3.3 to NetBeans IDE.
Next:
- query InterNIC server's whois without WhoisClient class.
~ org.apache.commons.net.whois.WhoisClient
Example:
package javawhoisclient; import java.io.IOException; import org.apache.commons.net.whois.WhoisClient; /** * * @web http://java-buddy.blogspot.com/ */ public class JavaWhoisClient { public static void main(String[] args) { WhoisClient whois; whois = new WhoisClient(); try { whois.connect(WhoisClient.DEFAULT_HOST); System.out.println(whois.query("=google.com")); whois.disconnect(); } catch (IOException e) { System.err.println("Error I/O exception: " + e.getMessage()); return; } } }
To use WhoisClient class in your program, you have to include library commons-net-3.3 in your code, it can be download here: https://commons.apache.org/proper/commons-net/index.html
This video show how to download and add library commons-net-3.3 to NetBeans IDE.
Next:
- query InterNIC server's whois without WhoisClient class.
Install Netbeans 8.1 0n Ubuntu 15.10
Netbeans IDE 8.1 just released.
If you missed the setting JDK path, you still run edit netbeans.conf, to specify netbeans_jdkhome, or run netbeans with --jdkhome option. (ref: http://wiki.netbeans.org/FaqRunningOnJre)
netbeans.conf to set netbeans_jdkhome:
run netbeans with --jdkhome option:
This video show how to download and install Netbeans IDE 8.1 on Ubuntu-GNOME 15.10
To install Ubuntu 8.1 on Ubuntu:
To install Ubuntu 8.1 on Ubuntu:
- Visit https://netbeans.org/ to download installer-file.
- chmod +x <installer-file-name>.
- run it with command:
./<installer-file-name> to run the installer.
The only special is to select correct path to JDK, 1:40 in the above video.
My JDK was installed with ppa:webupd8team, it's on "/usr/lib/jvm/java-8-oracle".
If you missed the setting JDK path, you still run edit netbeans.conf, to specify netbeans_jdkhome, or run netbeans with --jdkhome option. (ref: http://wiki.netbeans.org/FaqRunningOnJre)
netbeans.conf to set netbeans_jdkhome:
run netbeans with --jdkhome option:
Thursday, November 5, 2015
Java I/O, NIO and NIO.2
Java I/O, NIO and NIO.2
Java I/O, NIO, and NIO.2 is a power-packed book that accelerates your mastery of Java's various I/O APIs. In this book, you'll learn about classic I/O APIs (File, RandomAccessFile, the stream classes and related types, and the reader/writer classes). Next, you'll learn about NIO's buffer, channel, selector, regular expression, charset, and formatter APIs. Finally, you'll discover NIO.2's offerings in terms of an improved file system interface, asynchronous I/O, and the completion of socket channel functionality.
After reading and using thi book, you'll gain the accelerated knowledge and skill level to really build applications with efficient data access, especially for today's cloud computing streaming data needs.
What you’ll learn
- How to set permissions and more with the classic File class
- How to build a flat file database with RandomAccessFile
- Get to know the byte array, file, filter, and other kinds of streams
- Master serialization and externalization
- Discover character streams and their associated writers/readers
- Tour the buffer APIs
- Work with channels to transfer buffers to and from I/O services
- Find out about selectors and readiness selection
- Master regular expressions
- Discover charsets and their association with Java's String< class
- Take advantage of the formatter API to create formatted output
- How to customize the formatter API
- Explore the improved file system interface
- Discover asynchronous I/O and its association with futures and completion handlers
- Encounter socket channel improvements, including multicasting
This book is for those experienced with Java, beyond the fundamentals.
Table of Contents
Part 1: Getting Started with I/O
Chapter 1: I/O Basics and APIs
Part 2: Classic I/O APIs
Chapter 2: File
Chapter 3: RandomAccessFile
Chapter 4: Streams
Chapter 5: Writers and Readers
Part 3: New I/O APIs
Chapter 6: Buffers
Chapter 7: Channels
Chapter 8: Selectors
Chapter 9: Regular Expressions
Chapter 10: Charsets
Chapter 11: Formatter and Scanner
Part 4: More New I/O APIs
Chapter 12: File System APIs
Chapter 13: Asynchronous I/O
Chapter 14: Additional NIO.2 APIs
Appendix A: Answers to Review Exercises