ServerSocket and Socket classes


Java provides us two classes ServerSocket and Socket to create a Client/Server architecture. We use ServerSocket to create a server and the Socket class to create clients. The Socket class sends requests to the ServerSocket class and the ServerSocket responds according to the reqeust being sent by the client. Examples of both ServerSocket and Socket classes are as follows :

// ServerSocket Example


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

public class Server
{
        private ServerSocket server;
        private Socket client;
        private BufferedReader readClient;

        public Server()
        {
                try
                {
                        server = new ServerSocket(9999);
                        System.out.println("Server Started...");

                        client = server.accept();
                        System.out.println("Client Connected..");

                        readClient = new BufferedReader(new InputStreamReader(client.getInputStream()));

                        System.out.println(readClient.readLine());

                        readClient.close();
                        server.close();
                }
                catch(Exception e)
                {
                        System.out.println(e);
                }
        }
        public static void main(String[] args)
        {
                new Server();
        }
}


// Socket Example


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

public class Client
{
        private Socket client;
        private PrintWriter write_To_Server;

        public Client()
        {
                try
                {
                        client = new Socket("localhost", 9999);
                        write_To_Server = new PrintWriter(client.getOutputStream());

                        write_To_Server.println("Programming World");

                        write_To_Server.close();
                        client.close();
                }
                catch(Exception e)
                {
                        System.out.println(e);
                }
        }
        public static void main(String[] args)
        {
                new Client();
        }
}

How to use Do-While loop


Do-While is a special kind of loops. In this loop "While" does not have any body but "Do" does have. This means when the loop is executed the execution first goes to the "Do" block and then it goes to the "While" to check the condition or expression. This makes the Do-While loop run once even if the condition results in false and this makes this loop special.

public class Do_While
{
        public static void main(String[] args)
        {
                int var = 1;
                do
                {
                        System.out.println(var);
                        var++;
                }while(var <= 10);
        }
}

How to use While loop


"While" loop takes only one part as it condition or expression that is Termination.

public class While_Demo
{
        public static void main(String[] args)
        {
                int var = 1;
                while(var <= 10)
                {
                        System.out.println(var);
                        var++;
                }
        }
}

How to use For loop


"For" loop is a very famous loop. As you can see below it takes three parts as its condition or expression. The first part is said "Initialization". The second one is said "Termination". And the third one is said "Iteration". Initialization initializes the variable that the loop operates on. Termination checks if the loop should be executed. And iteration increases or decreases the value of the variable that the loop operates on.

public class For_Demo
{
        public static void main(String[] args)
        {
                for(int var = 1; var <= 10; var++)
                {
                        System.out.println(var);
                }
        }
}

How to use if-else in java


if-else are very important control statement. We use if-else to make our program follow some rules. We define a condition or expression for if-else and if the condition associated with "if" is true then the "if" block is executed and if the condition is false then the execution goes to the "else" block. We can use "if" statement alone with no "else" but we can not use "else" alone. A very simple program is depicted here to show how if-else works.

public class IF_Else_Demo
{
        public static void main(String[] args)
        {
                int var = 10;

                if(var == 10)
                {
                        System.out.println("INSIDE IF BLOCK");
                }
                else
                {
                        System.out.println("INSIDE ELSE BLOCK");
                }
        }
}

        In the above program, we have a variable "var" and we have assigned it a value of ten (10). When the execution goes to the "if" block it checks if the value of "var" is equal to 10, yes it is, it goes inside the "if" block and prints the given texts on the screen.
        If you change the value of "var" other than 10, the "if" block will not be running because the condition of the "if" will be "false" and the "else" part will be running and it will print "INSIDE ELSE BLOCK" on the screen.

How to compile a java program


Use the following series of steps to compile a java source file.


  • Open the command prompt.


  • Go to the directory, where your java source file is stored.


  • Type javac filename.java


  • The above command creates a .class file and your java source file is compiled.


  • To run the compiled java program type java filename without .java extension.

A simple program


public class SimpleProgram
{
        public static void main(String[] args)
        {
                System.out.println("Programming World");
        }
}


        This example shows how to print a string on the screen. This is a very simple program and it uses a method println() to print the texts given inside its braces. This example has only one method main method. This method is the entry point for any program so this means that when we start any program in java it start from the main method. But it must be given importance that the main method is written inside a class named as SimpleProgram because java does not permit to define a method outside a class unlike C++.

What is Java ?


Java is an object-oriented programming language intruduced by Sun Microsystems in 1995. Java is inherits many features of C++. Java solves many problems that were encountered in C++. Java does not support pointers. Java does not support multi-inheritance. And the most important feature of java is that it is a platform-independant programming language. Some of java features are :
  1. Robust.
  2. Object-Oriented.
  3. Platform-Independant.
  4. Secure (Bytecode).
  5. High Performance and
  6. Easy to learn.