Saturday, 31 January 2015

JAVA program to find out ECHO Server using socket.

//  File name = ConServer.java

import java.net.*;
import java.io.*;
public class ConServer
{
    public static void main(String[] dha)
    {
        ServerSocket s=null;
        try
        {
            s=new ServerSocket(12345);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }       
        while(true)
        {
            try
            { 
          System.out.println("Wllcom To SERVER .......");          
                Socket s1=s.accept();                       
                System.out.println("Enter Your Message For Client ...............");           
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                OutputStream out=s1.getOutputStream();               
                BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(out));           
                String str;           
                System.out.println("Enter exit++ to stop");                       
                str=br.readLine();
                while(str!=null)
                {
                    if(str.equals("exit++"))
                        break;
                    bw.write(str);
                    bw.newLine();
                    System.out.println("Read: "+str);               
                    str=br.readLine();                               
                }                      
                br.close();
                bw.close();
                s1.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }       
        }
    }   
}

***********************************************************************

 // File Name = ConClient .java

import java.io.*;
import java.net.*;   
public class ConClient
{
    public static void main(String[] dha)
    {
        try
        {
            Socket s1=new Socket("127.0.0.1",12345);
            System.out.println("Wllcom To Client .......");
            System.out.println(" Message Is  .......");        
            InputStream is=s1.getInputStream();    
            BufferedReader br=new BufferedReader(new InputStreamReader(is));
            int i;
            while((i=br.read())!=-1)
            {               
                System.out.print((char)i);                                         
            }
                br.close();
                s1.close();        
    }//try over
        catch(IOException e)
        {           e.printStackTrace();
            System.out.println("Conncection Problem");
        }
    }// main over
  
}// class over



*******************************************************************************
******************** OUTPUT  ********************

ConServer.java



ConClient .java



***************************************************************************

How To Run Server-Client Program                       

***************************************************************************

Step 1 > javac ConServer.java
//compile file

Step 2 > start
//open new terminal

Step 3 > java ConServer

Step 4 >ConClient .java
//compile file In new terminal

Step 5 > java ConClient



Monday, 19 January 2015

Write JAVA Program to print string length using extended thread.

File Name = exteded_thread .java


import java.util.Scanner;
class demo1 extends Thread
{
   Thread t;
   String threadName,s;
   demo1(String name)
   {
       Scanner in = new Scanner(System.in);
       threadName = name;
       System.out.println("Creating Thread......");
       try
        {  
            Thread.sleep(1000);
        }
        catch(InterruptedException e)
        {
            System.out.println("Erroe is --> "+e);
        }
   }
   public void run()
   {
      System.out.println("Running Thread......");
      try
      {     Thread.sleep(1000);
            System.out.println("length Of String Is -->"+s.length());
            Thread.sleep(1000);
      }
      catch(InterruptedException e)
        {
            System.out.println("Erroe is --> "+e);
        }
     System.out.println("Thread Is exiting.");
   }
   public void start ()
   {
      if (t == null)
      {
         t = new Thread (this, threadName);
         t.start ();
      }
   }
}
public class exteded_thread
{  
    public static void main(String args[])
     {
      String s="Wellcome";
      demo1 T1 = new demo1(s);
      T1.start();
     }
}

Write JAVA Program to print 1 to 10 using thread and synchronization.

Write JAVA Program to print 1 to 10 using thread without synchronization.


Write JAVA thread Program to find length of String.

File Name=RunnableDemo

import java.util.Scanner;
class RunnableDemo1 implements Runnable
{
   private Thread t;
   private String threadName;
   String s;
   RunnableDemo1(String name)
   {
   Scanner in = new Scanner(System.in);
   threadName = name;
   System.out.println("Enter String ");
   s = in.nextLine();
   }

  public void run()
{
        try {
                Thread.sleep(5000);
                System.out.println("length Of Thread " +  s.length());
            }
            catch (InterruptedException ex)
            {
               System.out.println("Exception "+ex');
            }
   }

   public void start ()
   {
         t = new Thread (this, threadName);
         t.start ();
   }
}

public class RunnableDemo
{
   public static void main(String args[])
  {
      RunnableDemo1 R1 = new RunnableDemo1("Thread-1");
      R1.start();
   }
}

    

JAVA Program to implement Simple Thread program

File name = RunnableDemo.java 


class RunnableDemo1 implements Runnable
{
   private Thread t;
   private String threadName;
   RunnableDemo1(String name)
   {
   threadName = name;
   System.out.println("Thread Is Start"+  threadName );
   }
  public void run()
  {
        try {
                Thread.sleep(5000);
                System.out.println("Thread Is Running " +  threadName );
                Thread.sleep(5000);
                System.out.println("length Of Thread " +  threadName.length());
            }
            catch (InterruptedException ex)
            {
                System.out.println("Exception is  "+ex);
            }
        System.out.println("Thread Is Exit " +  threadName );
   }
   public void start ()
   {
         t = new Thread (this, threadName);
         t.start ();
System.out.println("Thread Is Create "+  threadName );
   }
}

public class RunnableDemo
{
   public static void main(String args[])
 {
      RunnableDemo1 R1 = new RunnableDemo1("Thread-1");
      R1.start();
   }  
}