Powered by Blogger.

Sockets or Networking in Java

Socket is the Java programming model to establish the communication link between two processes. A socket can hold input and output stream. A process sends data to another process through the network by writing to the output stream associated with the socket. A process reads data written the another process by reading from the input stream associated with the socket.

Code 5−7: The HostServer.java application

import java.net.*;
import java.io.*;

public class HostServer{
  public static void main (String [ ] args){
    ServerSocket s = null;
    try{
      s = new ServerSocket(5432);
      Socket s1 = s.accept();
      OutputStream s1out = s1.getOutputStream ();
      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter (s1out 12 bw.write (”Hellow Net World”);
      bw.close();
      s1.close();
    }catch (IOException e){
      e.printStackTrac e ();
    }
  }
}

import java.net.*;
import java.io.*;

public class ClientServer{
  public static void main (String [ ] args){
    try{
       Socket s1 = new Socket(”127.0.0.1”, 5432);
       InputStream is = s1.get InputSt ream();
       DataInputStream dis = new DataInputStream(is);
       System.out.println(dis.readUTF());
       dis.close();
       s1.close();
    }catch (ConnectException e1){
       System.out.println(e1);
    }catch(IOException e2){
       e2.printStackTrace();
    }
  }
}

What is Java?

Java is a computer programming language that is used to build softwares. It is also an application, development, and deployment environment. Java is object oriented programming language to help us visualize the program in real-life terms. The syntax of Java is similar to C and C++ based language. Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture.

Brief History of Java

In 1990, Sun Microsystems began a research project named Green to develop the C and C++ based language. James Gosling, a network software designer was assigned to this project. Gosling’s solution to the problems of C++ was a new language called Oak after an oak tree outside his window at Sun. In May 1995, Sun Microsystems formally announced Java (Oak was renamed Java). Finally, in 1996, Java arrived in the market as a programming language.

With the advent of Java 2 (released initially as J2SE 1.2 in December 1998), new versions had multiple configurations built for different types of platforms. For example, J2EE targeted enterprise applications and the greatly stripped-down version J2ME for mobile applications (Mobile Java). J2SE designated the Standard Edition. In 2006, for marketing purposes, Sun renamed new J2 versions as Java EE, Java ME, and Java SE, respectively.

On November 13, 2006, Sun released much of Java as free and open source software, (FOSS), under the terms of the GNU General Public License (GPL). On May 8, 2007, Sun finished the process, making all of Java’s core code available under free software/open-source distribution terms.


Table: Major Release Versions of Java.

Version
Date
JDK 1.0
January 23, 1996
JDK 1.1
February 19, 1997
J2SE 1.2
December 8, 1998
J2SE 1.3
May 8, 2000
J2SE 1.4
February 6, 2002
J2SE 5.0
September 30, 2004
Java SE 6
December 11, 2006
Java SE 7
July 28, 2011

What is Java Applets?

Applets are Java programs that reside on web servers, download by a browser to a client’s computer and run by that browser. Applets are usually small in size to minimize download time and are invoked by a Hypertext Markup Language (HTML) web page.

Java Virtual Machine (JVM)

A Java virtual machine is a software that is capable of executing Java bytecode. Code for the JVM is stored in .class or .jar files, each of which contains code for at most one public class. JVM enables the Java application to be platform independent. JVM is distributed along with a set of standard class libraries that implement the Java application programming interface (API). JVM mainly performs three tasks:

Loads Code The class loader loads all classes needed for the execution of a program.

Verifies Code The byte code verifier tests format of code fragment and checks code fragments for illegal code, which is code that forges pointers, violates access rights on objects, or attempts to change object type.

Execute Code Performed by the runtime interpreter.

The Java Runtime Environment(JRE) is an implementation of the Java Virtual Machine (JVM), which actually executes Java programs. The Java Development Kit (JDK) is the original Java development environment for Java programmers. The JDK consists of a library of standard classes and a collection of utilities for building, testing, and documenting Java programs.

Java Automatic Memory Management | Java Coding

Automatic Memory Management:

Many programming languages like C and C++ permit the memory to be allocated dynamically at runtime. After the allocated memory is no longer required, the program or runtime environment should de-allocated the memory. If the program does not de-allocate the memory that can crash eventually when there is no memory left for the system to allocate. These types of programs are said memory leaks. Java overcome this problem by using garbage collection. It provides a system level thread that tracks each memory allocation. During idle cycles in the JVM, the garbage collection thread checks for and frees any memory the can be freed in the object lifecycle.