Program to know the Sockets of a System using Java

Program to know the Sockets of a System using Java

     This is the program to find out the inused ports in the system. The range between 0-1024 ports are reserved for internal system usage. In that some of the ports are unused. Socket object is used to find out the required ports. Socket is created with the combination of IP address and port number.

import java.io.*;
import java.net.*;
class Sock
{
public static void main(String args[])
{
                        for(int port=0;port<1024;port++)

                        {
                                    try
                                    {
                                    Socket s=new Socket("165.165.1.35",port);
                                    System.out.println("The Ports are:"+s);
                                    }
                                    catch(Exception e)
                                    {}
                        }
           
           
}
}

Output

E:\java Sock
The Ports are:Socket[addr=AK1-35.kcetdc/165.165.1.35,port=135,localport=2054]
The Ports are:Socket[addr=AK1-35.kcetdc/165.165.1.35,port=139,localport=2058]
The Ports are:Socket[addr=AK1-35.kcetdc/165.165.1.35,port=445,localport=2366]
The Ports are:Socket[addr=AK1-35.kcetdc/165.165.1.35,port=523,localport=2444]

Program for Client - Server Communication using Java

Program for Client - Server Communication using Java


Client Program

       Socket object must be created to communicate with server. The Socket object is clsct in the following program . 

import java.io.*;
import java.net.*;
import java.util.*;
class Client

{
            public static void main(String args[])
            {
            try
            {
                        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
                        Socket clsct=new Socket("127.0.0.1",139);
                        DataInputStream din=new DataInputStream(clsct.getInputStream());
                        DataOutputStream dout=new DataOutputStream(clsct.getOutputStream());
                        System.out.println("Enter String");
                        String str=in.readLine();
                        dout.writeBytes(str+'\n');
                        String str1=din.readLine();
                        System.out.println("The String is: "+str1);
                        clsct.close();
            }
            catch (Exception e)
            {
            System.out.println(e);
            }
            }
}

Server
     The ServerSocket object is created to create specific port in server side for client which is requesting to access the server. More than one ports are created for more number of clients. Each port have only one client.

     The ServerSocket object is obj in the following program. 

import java.io.*;
import java.net.*;
import java.util.*;
class Server
{
            public static void main(String args[])
            {
            try
            {
                        ServerSocket obj=new ServerSocket(139);
                        while(true)
                        {
                                    Socket obj1=obj.accept();
                                    DataInputStream din=new DataInputStream(obj1.getInputStream());
                                    DataOutputStream dout=new DataOutputStream(obj1.getOutputStream());
                                    String str=din.readLine();
                                    dout.writeBytes(str+'\n');
                                    System.out.println(str);
                        }
            }
            catch(Exception e)
            {
                        System.out.println(e);
            }
            }
}

Output

E:\mayil\networks>java Client
Enter String
mayil
The String is: mayil

E:\mayil\networks>java Server
mayil

Comments in java program

Comments in java program

     Comments are included in a program for better understanding about the program. There are three types of comments in java. They are
               1 .  Single line comment
               2 . Multi line comment
               3 . Documentation comment

Single line comment
  
     Single line comment should end on a single line and should begin with double slash (//).
                          Example:  //Java is flexible

Multi line comments

     If the comment exceeds one line, multiline comment is used. these comments must begin with /* and end with */.
         Example: /*This program is used to calculate
                              the salary of the employees*/

Documentation comments

     This comment is used to generate documentation automatically. these comments must begin with /** and ends with */. Java scans the program finds /** and */ and generate documentation automatically with the help of the documentation utility javadoc .

Program for Client - Server Communication to Display date using Java

Program for Client - Server Communication to Display date using Java


     This is a program for displaying the date from server to client. Client is requesting the server to retrieve the current date.

Client:

import java.io.*;
import java.net.*;
import java.util.*;
class Client
{
            public static void main(String args[])
            {
            try
            {
                        Socket clsct=new Socket("127.0.0.1",139);

                        DataInputStream din=new DataInputStream(clsct.getInputStream());
                        DataOutputStream dout=new DataOutputStream(clsct.getOutputStream());
                        String str=din.readLine();
                        dout.writeBytes(str+'\n');
                        System.out.println(str);                      
                        clsct.close();
            }
            catch (Exception e)
            {
            System.out.println(e);
            }
            }
}

Server:

import java.io.*;
import java.net.*;
import java.util.*;
class Server
{
            public static void main(String args[])
            {
            try
            {
                        ServerSocket obj=new ServerSocket(139);
                       
                        while(true)
                        {
                                    Socket obj1=obj.accept();
                                   
                                    DataOutputStream dout=new DataOutputStream(obj1.getOutputStream());
                                    Date d=new Date();
                                    String S=d.toString();
                                    dout.writeBytes(S+'\n');
                       
                                    obj.close();
                        }
            }
            catch(Exception e)
            {
                        System.out.println(e);
            }
            }
}

Output

E:\networks>javac Server.java

E:\networks>javac Client.java

E:\networks>java Server

E:\networks>java Client
Tue Jul 12 22:25:06 PDT 2011

Program to Chat between Client & Server in Java

Program to Chat between Client & Server in Java

     This program enables us to chat between the client and server. This is a bidirectional data transfer.

Client


import java.io.*;
import java.net.*;
import java.util.*;
class Client
{
            public static void main(String arghs[])
            {
                        try
                        {
                                    Socket clientskt=new Socket("127.0.0.1",1024);
                                    while(true)
                                    {
                                    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
                                   
                                    DataInputStream din=new DataInputStream(clientskt.getInputStream());
                                    DataOutputStream dout=new DataOutputStream(clientskt.getOutputStream());
                                    System.out.print("You have to type:");
                                    String str=in.readLine();
                                    dout.writeBytes(str+"\n");
                                    if(str.equals("bye"))
                                    {
                                                break;
                                    }
                                    String str1=din.readLine();
                                    System.out.println("Server:"+str1);
                                    if(str1.equals("bye"))
                                    {
                                                break;
                                    }
                                    }
                                    clientskt.close();
                                   
                        }
                        catch(Exception e)
                        {
                                    System.out.println(e);
                        }
            }
}

SERVER

import java.io.*;
import java.net.*;
import java.util.*;
class Server
{
            public static void main(String args[])
            {
            try
            {
                        ServerSocket sock=new ServerSocket(1024);
                        Socket clientskt=sock.accept();
                        while(true)
                        {         
                                   
                                    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
                                    DataInputStream din=new DataInputStream(clientskt.getInputStream());
                                    DataOutputStream dout=new DataOutputStream(clientskt.getOutputStream());
                                    String str1=din.readLine();
                                    System.out.println("Client:"+str1);
                                    if(str1.equals("bye"))
                                    {
                                                break;
                                    }
                                    System.out.print("You have to type:");
                                    String str=in.readLine();
                                    dout.writeBytes(str+'\n');
                                    if(str.equals("bye"))
                                    {
                                                break;
                                    }
                                                                       
                        }
                        sock.close();
                       
            }
            catch(Exception e)
            {
                        System.out.println(e);
                        }
            }
}   

Output

E:\java>javac Server.java

E:\java>java Client

E:\java>java Client
You have to type:hai
Server:hello
You have to type:how r u
Server:fine u?
You have to type:fine
Server:r u free?
You have to type:bye

E:\java>java Server
Client:hai
You have to type:hello
Client:how r u
You have to type:fine u?
Client:fine
You have to type:r u free?
Client:bye

Program to demonstrate the Switch case and If statement

Program to demonstrate the Switch case and If statement


This a Program to demonstrate the Switch case and If statement by calculating the discount amount of a cloth products

Program:
 
import java.io.*;
import java.util.*;
class Cloth1
{
void purchase()
                {
                Scanner s=new  Scanner(System.in);
                System.out.println("What do  you want to Purchase? \n 1)Mill Cloth \n 2)Handloom Cloth \n 3)Exit");

                int x=s.nextInt();
                switch(x)
                                {
                                                case 1:
                                                               
                                                                                System.out.println("Enter the amount of mill cloth: ");
                                                                                float d=s.nextFloat();
                                                                                discountmill(d);
                                                                                break;
                                                               
                                                case 2:
                                                               
                                                                                System.out.println("Enter the amount of handloom cloth: ");
                                                                                float d1=s.nextFloat();
                                                                                discounthl(d1);
                                                                                break;
                                                case 3:
                                                                                System.out.println("Thank you!!!");
                                                                                break;
                                    }
                }
                void discountmill(float a)
                {
                                double dis;
                                double res;
                                if(a<=100)
                                {
                                                System.out.println("No discount!!!");
                                }
                                else if(a>100 && a<=200)
                                {
                                                System.out.println("5% Discount for this item:");
                                                dis=0.05*a;
                                                res=a-dis;
                                                System.out.println("Net Amount with Discount: "+res);
                                }
                                else if(a>200 && a<=300)
                                {
                                                System.out.println("7.5% Discount for this item:");
                                                dis=0.075*a;
                                                res=a-dis;
                                                System.out.println("Net Amount with Discount: "+res);
                                }
                                else
                                {
                                                System.out.println("10% Discount for this item:");
                                                dis=0.1*a;
                                                res=a-dis;
                                                System.out.println("Net Amount with Discount: "+res);                               
                                }
                                purchase();
                }
                void discounthl(float b)
                {
                               
                                double dis;
                                double res;
                                if(b<=100)
                                {
                                                System.out.println("5% Discount for this item:");
                                                dis=0.05*b;
                                                res=b-dis;
                                                System.out.println("Net Amount with Discount: "+res);
                                }
                                else if(b>100 && b<=200)
                                {
                                                System.out.println("7.5% Discount for this item:");
                                                dis=0.075*b;
                                                res=b-dis;
                                                System.out.println("Net Amount with Discount: "+res);
                                }
                                else if(b>200 && b<=300)
                                {
                                                System.out.println("10% Discount for this item:");
                                                dis=0.1*b;
                                                res=b-dis;
                                                System.out.println("Net Amount with Discount: "+res);
                                }
                                else
                                {
                                                System.out.println("15% Discount for this item:");
                                                dis=0.15*b;
                                                res=b-dis;
                                                System.out.println("Net Amount with Discount: "+res);                               
                                }
                                purchase();
                }
                               
}
class Cloth
{
                public static void main(String args[])
                {
                                Cloth1 c1=new Cloth1();
                                c1.purchase();
                }
}             

OUTPUT
D:\Java>javac Cloth.java

D:\Java>java Cloth
What do you want to Purchase?
 1)Mill Cloth
 2)Handloom Cloth
 3)Exit
1
Enter the amount of mill cloth:
50
No discount!!!
What do you want to Purchase?
 1)Mill Cloth
 2)Handloom Cloth
 3)Exit
1
Enter the amount of mill cloth:
140
5% Discount for this item:
Net Amount with Discount: 133.0
What do you want to Purchase?
 1)Mill Cloth
 2)Handloom Cloth
 3)Exit
1
Enter the amount of mill cloth:
240
7.5% Discount for this item:
Net Amount with Discount: 222.0
What do you want to Purchase?
 1)Mill Cloth
 2)Handloom Cloth
 3)Exit
1
Enter the amount of mill cloth:
340
10% Discount for this item:
Net Amount with Discount: 306.0
What do you want to Purchase?
 1)Mill Cloth
 2)Handloom Cloth
 3)Exit
2
Enter the amount of handloom cloth:
40
5% Discount for this item:
Net Amount with Discount: 38.0
What do you want to Purchase?
 1)Mill Cloth
 2)Handloom Cloth
 3)Exit
2
Enter the amount of handloom cloth:
140
7.5% Discount for this item:
Net Amount with Discount: 129.5
What do you want to Purchase?
 1)Mill Cloth
 2)Handloom Cloth
 3)Exit
2
Enter the amount of handloom cloth:
240
10% Discount for this item:
Net Amount with Discount: 216.0
What do you want to Purchase?
 1)Mill Cloth
 2)Handloom Cloth
 3)Exit
2
Enter the amount of handloom cloth:
340
15% Discount for this item:
Net Amount with Discount: 289.0
What do you want to Purchase?
 1)Mill Cloth
 2)Handloom Cloth
 3)Exit
3
Thank you!!!

Program to Implement Stack in Java

Program to Implement Stack in Java


    This program is used to demonstrate the Stack operations PUSH & POP with the help of Switch an if statements.

import java.io.*;
import java.util.*;
class Stack
{
    int a[]=new int[10];
    int top=0;
    Stack()


    {
        for(int i=0; i<5 ; i++)
        {
            a[i]=0;
        }
    }
    void push()
    {
        if(top >= 4)
        {
            System.out.println("Stack full");
        }
        else
        {
            Scanner sc1=new Scanner(System.in);
            System.out.println("Enter the item to Push");
            int y=sc1.nextInt();          
            a[top]=y;
            top++;
        }
    }
  
    void pop()
    {
        if(top <= 0)
        {
            System.out.println("Stack empty");
        }
        else
        {    top-- ;
            System.out.println("Popped element is:"+a[top]) ;
          
        }
  }
}
  class Mainstack
{
    public static void main(String args[])
    {
     try
     {
        Stack s= new Stack();
        Scanner sc=new Scanner(System.in);
        int a;
        do
        {
        System.out.println("Choose any one <1,2> to do follwing operation \n 1) PUSH \n 2) POP ");
        int x=sc.nextInt();
        switch(x)
        {
            case 1:
                s.push();
                break;
            case 2:
                s.pop();
                break;
            default :
                System.out.println("Thank You!!!");
                break;
        }
        System.out.print("Do you wish to continue \n Press \n 1 to continue \n 0 to exit :");
        a=sc.nextInt();
        }  
        while(a==1);
     }
     catch(Exception e)
     {
        System.out.println(e);
     }  
    }
}

 

Output:
Choose any one <1,2> to do follwing operation
1) PUSH
2) POP
1
Enter the item to Push
10
Do you wish to continue
Press
1 to continue
0 to exit :1
Choose any one <1,2> to do follwing operation
1) PUSH
2) POP
1
Enter the item to Push
12
Do you wish to continue
Press
1 to continue
0 to exit :1
Choose any one <1,2> to do follwing operation
1) PUSH
2) POP
1
Enter the item to Push
11
Do you wish to continue
Press
1 to continue
0 to exit :1
Choose any one <1,2> to do follwing operation
1) PUSH
2) POP
1
Enter the item to Push
15
Do you wish to continue
Press
1 to continue
0 to exit :1
Choose any one <1,2> to do follwing operation
1) PUSH
2) POP
1
Stack full
Do you wish to continue
Press
1 to continue
0 to exit :1
Choose any one <1,2> to do follwing operation
1) PUSH
2) POP
2
Popped element is:15
Do you wish to continue
Press
1 to continue
0 to exit :1
Choose any one <1,2> to do follwing operation
1) PUSH
2) POP
2
Popped element is:11
Do you wish to continue
Press
1 to continue
0 to exit :1
Choose any one <1,2> to do follwing operation
1) PUSH
2) POP
2
Popped element is:12
Do you wish to continue
Press
1 to continue
0 to exit :1
Choose any one <1,2> to do follwing operation
1) PUSH
2) POP
2
Popped element is:10
Do you wish to continue
Press
1 to continue
0 to exit :1
Choose any one <1,2> to do follwing operation
1) PUSH
2) POP
2
Stack empty
Do you wish to continue
Press
1 to continue
0 to exit :0

Creating Objects

Creating Objects                  Creating objects means to allocate memory space for all the instance variables of the objects. S...