Making Http Simple Server With Java

by Son Rokhaniawan Perdata, S.T | 12:16 AM in , | comments (2)

Praktikum's task Network and Apparatus Mathematics moves secondly be make one http simple server at localhost. With utilize SocketServer's function, therefore gets to be made one server which will wait “panggilan” on port 80 (HTTP). And will send html's code while called.

SocketServer works in one looping that perpetual to wait marks sense connection to go to port 80 on localhost. There is 4 constructor on SocketServer for example:

  1. public ServerSocket(int port) throws BindException, IOException
  2. public ServerSocket(int port, int queueLength) throws BindException, IOException
  3. public ServerSocket(int port, int queueLength, InetAddress bindAddress) throws IOException
  4. public ServerSocket( ) throws IOException // Java 1.4

n Java, to make one server gets to be done by trick as follows:

  1. One serverSocket a new one is made deep one given port (in this case port 80) utilizing constructor of one SocketServer.
  2. Then serverSocket waits until mark sense coming connection through port has already is determined previous utilize accept's function().
  3. Then depends from server type, well utilize getInputStream's function(), getOutputStream's function(), or both of ala function goes together to get input and output stream in order to gets communication with client.
  4. Server and client gets communication / get interaction until its time hang up connection.
  5. Server, Client or both hangs up connection
  6. Server returns to to step 2 and waiting until marks sense succeeding connections.

And attachment source's following code that I takes from praktikum's module meagrely modifies to be able to qualify as one http server.

Serverku.java

[+/-] Read More...

Perusal and File Writing with Java

by Son Rokhaniawan Perdata, S.T | 12:03 AM in , | comments (0)

A few days ago, my friend asks for to help her works PR its Programming. PR's problem terminologicals i am goodly difficult also, since I newbie in programming. Programming languages who may be utilized is C,C++, Java. And since i am java person, therefore I choose to utilize Java.hehe.. Since I really be intend to study Java language.

Problem PR is we make program who will read one input file “indata.txt”, one that meaty word which will at test do correspond to order, if accords therefore that diwrite will into one file “legalwords.txt”, conversely if incorrectly will at write into file “badwords.txt”. Among word one by another word in file “indata.txt” came to pieces by one tabulator, on my program, I utilize tabulator tab(\t). Order that is utilized for mengetest to say is

a. first syllable may not as numeral as.
b. say has less than 8 characters.
c. say just may lap over of letter (a..z, A..Z), number (0..9), atau simbol underscore(’_').

Since I newbie in Java,and was wonted utilizes it, requiring time rather long time to think up syntax syntax that is used. On progam I, its program path is as follows:

a. Make one BufferedReader for FileReader(”indata.txt”). Make PrintWritter for FileWriter legalwords.txt and badwords.txt.
b. Then by use of StringTokenizer to account word amount in file indata.txt.
c. After knows total token / says, therefore then with looping to check about word / token, what appropriate ruling or not.
d. Then afters is sorted, therefore word suitably at write goes to legalwords.txt, and unsuitably diwrite goes to badwords.txt

And this is result running programs my version:
1. Input File

Input File

2. Legal Words

Input File

3. Bad Words

Input File

On Compile NetBeans

Input File

And this following is source code programs what do I make. Its correction appeal if available fault.

DOWNLOAD


[+/-] Read More...

Tips Java : Getting Address's IP and Host Name

by Son Rokhaniawan Perdata, S.T | 11:35 PM in , | comments (0)

Java provides InetAddress's function to get host's internet protocol address and also name a computer. host's internet protocol address and also name that is gotten don't cling to host's internet protocol address and name local computer only, but can also be utilized for mengecek host's internet protocol address and name at Internet, obviously by condition of computer we most link with Internet. This logistic purpose also relates hand in glove with setting DNS on computer that we utilizes.

Following is sample program that points out fungsionalitas that:
  1. Make one project is Netbeans new

  2. Add one JFrame Form

  3. Provede with 2 tags, 2 numbers TextField (txtIPAddress dan txtNamaHost) and 3 Button (btnCheckIPAddress, btnCheckHostName dan btnCheckLokal). See example on pictured following:

  4. Following code cannikin type on btnCheckHostName, event actionPerformed

    1. private void btnCheckHostNameActionPerformed(java.awt.event.ActionEvent evt) {

    2. try {

    3. String strHostName = InetAddress.getByName(txtIPAddress.getText()).getHostName();

    4. JOptionPane.showMessageDialog(null, "Host name dari IP Address '" + txtIPAddress.getText() +"' =
      "+ strHostName);

    5. } catch (UnknownHostException ex) {

    6. JOptionPane.showMessageDialog(null, ex);

    7. Logger.getLogger(frmIpAddress.class.getName()).log(Level.SEVERE, null, ex);


    8. }


    9. }


  5. Do import library that needful (import java.net.InetAddress; import javax.swing.JOptionPane;)

  6. Following code cannikin type on btnCheckIPAddress, event actionPerformed


    1. private void btnCheckIPAddressActionPerformed(java.awt.event.ActionEvent evt) {


    2. try {

    3. String strIPAddress = InetAddress.getByName(txtHostName.getText()).getHostAddress() ;

    4. JOptionPane.showMessageDialog(null, "Alamat IP dari '"+txtHostName.getText() +"' ="+ strIPAddress);

    5. } catch (UnknownHostException ex) {

    6. JOptionPane.showMessageDialog(null, ex);

    7. Logger.getLogger(frmIpAddress.class.getName()).log(Level.SEVERE, null, ex);


    8. }

    9. }


  7. Following code cannikin type on btnCheckLokal event actionPerformed

    1. private void btnCheckLocalActionPerformed(java.awt.event.ActionEvent evt) {

    2. try {

    3. InetAddress AlamatInternet = InetAddress.getLocalHost();

    4. JOptionPane.showMessageDialog(null, "Host name lokal : " +AlamatInternet.getHostName());

    5. JOptionPane.showMessageDialog(null, "IP Address lokal : " +AlamatInternet.getHostAddress() );

    6. } catch (UnknownHostException ex) {

    7. JOptionPane.showMessageDialog(null, ex);

    8. Logger.getLogger(frmIpAddress.class.getName()).log(Level.SEVERE, null, ex);


    9. }

    10. }


  8. Save and runs application (SHIFT + F6). Insert IP Address and click on “Check Host Name” or insert host name and click “Check IP Address”. Check IP and Host Local don't need entry. Following is umpteen screenshot message one that performs:



    This program not smart's ala detect entry what that internet protocol address or host name. To easy, I utilize 2 numbers TextField for example. On terapan's application, input IP Address or Host Name can thru get variable.

[+/-] Read More...

NetBeans IDE Java Quick Start Tutorial (Hello Word)

by Son Rokhaniawan Perdata, S.T | 11:10 AM in , , | comments (0)

Welcome to NetBeans IDE!

This tutorial provides a very simple and quick introduction to the NetBeans IDE workflow by walking you through the creation of a simple "Hello World" Java console application. Once you are done with this tutorial, you will have a general knowledge of how to create and run applications in the IDE.

This tutorial takes less than 10 minutes to complete.

After you finish this tutorial, you can move on to the learning trails, which are linked from the Documentation, Training & Support page. The learning trails provide comprehensive tutorials that highlight a wider range of IDE features and programming techniques for a variety of application types. If you do not want to do a "Hello World" application, you can skip this tutorial and jump straight to the learning trails.

To complete this tutorial, you need the following software and resources.

Software or Resource Version Required
NetBeans IDE version 6.5
Java Development Kit (JDK) version 6 or
version 5

Setting Up the Project

To create an IDE project:

  1. Start NetBeans IDE.
  2. In the IDE, choose File > New Project (Ctrl-Shift-N), as shown in the figure below.

    New Project menu item selected." class="margin-around" height="85" width="199">

  3. In the New Project wizard, expand the Java category and select Java Application as shown in the figure below. Then click Next. NetBeans IDE, New Project wizard, Choose Project page.
  4. In the Name and Location page of the wizard, do the following (as shown in the figure below):
    • In the Project Name field, type HelloWorldApp.
    • Leave the Use Dedicated Folder for Storing Libraries checkbox unselected.
    • In the Create Main Class field, type helloworldapp.HelloWorldApp.
    • Leave the Set as Main Project checkbox selected.

    NetBeans IDE, New Project wizard, Name and Location page.

  5. Click Finish.

The project is created and opened in the IDE. You should see the following components:

  • The Projects window, which contains a tree view of the components of the project, including source files, libraries that your code depends on, and so on.
  • The Source Editor window with a file called HelloWorldApp open.
  • The Navigator window, which you can use to quickly navigate between elements within the selected class.
  • The Tasks window, which lists compilation errors as well other tasks that are marked with keywords such as XXX and TODO.

NetBeans IDE with the HelloWorldApp project open.

Adding Code to the Generated Source File

Because you have left the Create Main Class checkbox selected in the New Project wizard, the IDE has created a skeleton class for you. You can add the "Hello World!" message to the skeleton code by replacing the line:

            // TODO code application logic here
with the line:
            System.out.println("Hello World!");

Save the change by choosing File > Save.

The file should look something like the following code sample.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package helloworldapp;

/**
*
* @author Patrick Keegan
*/
public class HelloWorldApp {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Hello World!");
}

}

Compiling and Running the Program

Because of the IDE's Compile on Save feature, you do not have to manually compile your project in order to run it in the IDE. When you save a Java source file, the IDE automatically compiles it.

To run the program:

  • Choose Run > Run Main Project (F6).

The next figure shows what you should now see.

The program prints Hello World! to the Output window (along with other output from the build script).

Congratulations! Your program works!

If there are compilation errors, they are marked with red glyphs in the left and right margins of the Source Editor. The glyphs in the left margin indicate errors for the corresponding lines. The glyphs in the right margin show all of the areas of the file that have errors, including errors in lines that are not visible. You can mouse over an error mark to get a description of the error. You can click a glyph in the right margin to jump to the line with the error.

Building and Deploying the Application

Once you have written and test run your application, you can use the Clean and Build command to build your application for deployment. When you use the Clean and Build command, the IDE runs a build script that performs the following tasks:

  • Deletes any previously compiled files and other build outputs.
  • Recompiles the application and builds a JAR file containing the compiled files.

To build your application:

  • Choose Run > Clean and Build Main Project (Shift-F11).

You can view the build outputs by opening the Files window and expanding the HelloWorldApp node. The compiled bytecode file HelloWorldApp.class is within the build/classes/helloworldapp subnode. A deployable JAR file that contains the HelloWorldApp.class is within the dist node.

Image showing the Files window with the nodes for the HelloWorldApp          expanded to show the contents of the build and dist subnodes.

[+/-] Read More...

Categories

Comments

Subcribe

Enter your email address:

About Me

My photo
I am its person simple. Bad blood platitude. Directly az goes to aim target

Shoutbox