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();
   }  
}


Tuesday, 14 October 2014

C Program to crate lexical analyzer which will do space removal.

#include
#include
#include
#include

int key = 0;
char expr[100];
char cont[][20]={"CONTROLS","for","do","while","NULL",};
char cond[][20]={"CONDITION","if","then","NULL"};
char oprt[][20]={"OPERATOR","+","-","*","/","%","<","<=",">",">=","=","(",")","NULL"};
char branch[][20]={"BRANCHING","goto","jump" ,"NULL"};

void checking(char[],char[][20]);

void main()
{
int i,j,l,k,m,n;
char sbexpr[50],txt[3];
clrscr();
cout<<"Enter the expression:";
gets(expr);
for(i=0;expr[i]!=NULL;i++)
{
key=0;
for(j=i,k=0;expr[j]!=32 && expr[j]!=NULL;i++,j++,k++)
sbexpr[k]=expr[j];
sbexpr[k]=NULL;

if(key==0) checking(sbexpr,cond);

if(key==0) checking(sbexpr,cont);

if(key==0) checking(sbexpr,branch);

if(key==0)
{
for(m=0;sbexpr[m]!=NULL;m++)
{
key=0;
txt[0]= sbexpr[m];
txt[1] = NULL;

if(key==0) checking(txt,oprt);
if((key==0) || ((sbexpr[m]>=97 && sbexpr[m]<=122) || (sbexpr[m]>=65 && sbexpr[m]<=90)))
{
cout<<"\n"<"<<"Identifier\n";
key = 1;
}
}
}
if(key == 0)
{
cout<<"\n"<"<<"Address\n";
key = 1;
}
}

getch();
}


void checking (char expr[],char check[][20])
{
for(int i=1;strcmp(check[i],"NULL")!=0;i++)
{
if(strcmp(expr,check[i])==0)
{
cout<"<key = 1;
}
}
}