URDINESH: CODE
Showing posts with label CODE. Show all posts
Showing posts with label CODE. Show all posts

Tuesday, May 20, 2014

JAVA CODE FOR STREAM CUSTOMIZATION-COUNT READER



PROGRAM:

import java.io.*;
public class Streamcustomization extends Reader
{
            Reader in=null;
int  read=0,charCount=0,wordCount=0,lineCount=0;
            boolean whiteSpace=true;
            public void CountReader(Reader r)
            {
                        in=r;
            }
            public int read(char[]array,int off,int len)throws IOException
            {
                        if(array==null)
                        throw new IOException("null array");
                        read=in.read(array,off,len);
                        charCount+=read;
                        char c;
                        for(int i=0;i<read;i++)
                        {
                                    c=array[i];
                                    if(c=='\n')
                                    lineCount++;
                                    if(Character.isWhitespace(c))
                                                whiteSpace=true;
                                    else if(Character.isLetterOrDigit(c) && whiteSpace)
                                    {
                        wordCount++;
                        whiteSpace=false;
                                    }
                        }
                        return read;
            }
            public void close() throws IOException
            {
                        in.close();
            }
            public int getCharCount()
            {
                        return charCount;
            }
           

public int getWordCount()
            {
return wordCount;
            }
            public int getLineCount()
            {
                        return lineCount;
            }
            public static void main(String a[]) throws Exception
            {
                        CountReader cr=new CountReader(new FileReader("lab.java"));
                        char c[]=new char[4096];
                        int read=0;
                        while((read=cr.read(c,0,c.length))!=-1)
                        System.out.println(new String(c,0,read));
                        cr.close();
System.out.println("\n\n Read chars:"+cr.getCharCount()+"\n words:"+cr.getWordCount()+"\n lines:"+cr.getLineCount());
            }
 }



OUTPUT:

class lab
{
public static void main(String args[])
{
System.out.println("welcome to MCA lab");
}
}


 Read chars:18
 words:4

 lines:1

JAVA CODE FOR SYNCHRONIZATION-BANK



PROGRAM:

class Account
{
int acno;
float amt;
Account(int a,float a1)
{
acno=a;
amt=a1;
System.out.println("\nNow,the Balance is:Rs."+amt);
}
synchronized void putamt(float a)
{
System.out.println("\nProcess::Depositing Amount");
System.out.println("Depositing in Account is :Rs."+a);
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println("Exception is:"+e.toString());
}
amt+=a;
System.out.println("After Deposit,the Amount is:Rs."+amt);
}
synchronized float getamt(float a)
{
System.out.println("\nProcess::Withdrawing Amount");
System.out.println("Amount to be Withdraw is:Rs."+a);
if(a>amt)
{
System.out.println("\nDear Customer,Not Enough Money!!,Please Check your Balance.");
return 0;
}
else
{
amt-=a;
System.out.println("After Withdrawal,the Amount is:Rs."+amt);
return(a);
}

}
}
class deposit extends Thread
{
Account o;
deposit(Account t)
{
o=t;
}
public void run()
{
o.putamt(8000.00f);
}
}
class  withdraw extends Thread
{
Account b;
withdraw(Account t)
{
b=t;
}
public void run()
{
float r=b.getamt(500f);
            }
}
class Bank
{
public static void main(String arg[])
{
Account a=new Account(100,500.00f);
deposit d=new deposit(a);
withdraw w=new withdraw(a);
d.start();
w.start();
}
 }








OUTPUT:


Now,the Balance is:Rs.1500.0

Process::Depositing Amount
Depositing in Account is :Rs.10000.0
After Deposit,the Amount is:Rs.11500.0

Process::Withdrawing Amount
Amount to be Withdraw is:Rs.1500.0
After Withdrawal,the Amount is:Rs.10000.0

JAVA CODE FOR TCP MESSAGE TRANSFER



PROGRAM: (SERVER)

import java.net.*;
import java.io.*;
public class TcpServer
{
public static void main(String args[]) throws IOException
{
ServerSocket serverSocket=null;
try
{
serverSocket=new ServerSocket(1234);
}
catch(IOException e)
{
System.err.println("Could not Listen on Port:1234.");
System.exit(1);
}
Socket clientSocket=null;
try
{
clientSocket=serverSocket.accept();
}
catch(IOException e)
{
System.err.println("Accept Failed.");
System.exit(1);
}
PrintWriter out=new PrintWriter(clientSocket.getOutputStream(),
true);
BufferedReader in=new  BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String int1,int2;
int num1=0,num2=0;
int1=in.readLine();
System.out.println("Received Number1:: "+int1);
int2=in.readLine();
System.out.println("Received Number2::  "+int2);
System.out.println(int1+"*"+int2);
try
{
 num1=Integer.parseInt(int1);
 num2=Integer.parseInt(int2);
}
catch(NumberFormatException nfe)
{
System.out.println("Numbers not Intergers");
out.println("Numbers not intergers");
}
System.out.print("="+num1*num2);
out.println(String.valueOf(num1*num2));
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}

PROGRAM: (CLIENT)

import java.io.*;
import java.net.*;
public class TcpClient
{
public static void main(String[] args) throws IOException
{
Socket socket=null;
PrintWriter out=null;
BufferedReader in=null;
try
{
socket=new Socket("127.0.0.1",1234);
out=new PrintWriter(socket.getOutputStream(),true);
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch(UnknownHostException e)
{
System.err.println("Don't know about Host!!");
System.exit(1);
}
catch(IOException e)
{
System.err.println("Couldn't get I/o for the connection!!");
System.exit(1);           
}
BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
String num1,num2;
System.out.println("Enter Number1:: ");
num1=read.readLine();
out.println(num1);
System.out.println("Enter Number2:: ");
num2=read.readLine();
out.println(num2);
System.out.println("Product of two given Numbers are:: ");
System.out.println(in.readLine());
out.close();
in.close();
read.close();
socket.close();
}
}



OUTPUT:(SERVER)


Received Number1::5
Received Number2::5
5*5
=25

OUTPUT:(CLIENT)


Enter Number1::
5
Enter Number2::
5
Product of two given Numbers are::

25

JAVA CODE FOR TCP CHAT APPLICATION


PROGRAM: (SERVER)
import java.io.*;
import java.net.*;
class ChatS
{
public static void main(String args[])throws IOException
{
ServerSocket s=new ServerSocket(3300);
Socket s1=s.accept();
for(;;)
{
DataInputStream d1=new DataInputStream(s1.getInputStream());
String s2=d1.readUTF();
System.out.println("client:"+s2);
DataInputStream b=new DataInputStream(System.in);
String s3=b.readLine();
DataOutputStream d3=new DataOutputStream(s1.getOutputStream());
System.out.print(“server: “);
d3.writeUTF(s3);
}  
}  
}
PROGRAM: (CLIENT)
import java.io.*;
import java.net.*;
class ChatC
{
public static void main(String args[])throws IOException
{
Socket s1=new Socket("127.0.0.1",3300);
for(;;)
{
DataInputStream dis=new DataInputStream(System.in);
String str=dis.readLine();
DataOutputStream ds=new DataOutputStream(s1.getOutputStream());
System.out.print(“client: “);
ds.writeUTF(str);
DataInputStream d1=new DataInputStream(s1.getInputStream());
String Str1=d1.readLine();
System.out.println("server:"+Str1);    
}   
}  
}






OUTPUT: (CLIENT)

client: Hai
From server:How are you
client: fine


OUTPUT: (SERVER)


From client:Hai
server: How are you

From client: fine

JAVA CODE FOR TCP FILE TRANSFER



PROGRAM: (SERVER)

import java.net.*;
import java.io.*;
import java.util.*;
class FileTcpServer
{
public static void main(String a[])throws IOException
{
BufferedReader din=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Source Filename::  ");
String sf=din.readLine();
String str=" ";
File srcf=new File(sf);
if(!srcf.exists() )
{
System.out.println(sf + "File not Found!!");
return;
}
FileInputStream fin=new FileInputStream(srcf);
int len=fin.available();
byte ch[]=new byte[len];
try
{
ServerSocket ss=new ServerSocket(3000);
while(fin.available() >0 )
{
Socket sock=ss.accept();
PrintWriter bw=new PrintWriter(sock.getOutputStream(),true);
fin.read(ch,0,len);
str=new String(ch,0,len);
System.out.println(str);
bw.println(str);
System.out.println(len);
sock.close();
}
}
catch(Exception e)
{         
}
fin.close();
}
}




PROGRAM: (CLIENT)

import java.net.*;
import java.io.*;
import java.util.*;
class FileTcpClient
{
public static void main(String a[])throws IOException
{
String str=" ";
BufferedReader din=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Destination Filename::  ");
String df=din.readLine();
File dstf=new File(df);
if(!dstf.exists() )
{
System.out.println(df + "File already Exists!!");
return;
}
FileOutputStream fout=new FileOutputStream(dstf);
try
{
Socket sock=new Socket(InetAddress.getByName("  "),3000);
DataInputStream br=new DataInputStream(sock.getInputStream());
int len=2341;
while(true )
{
byte buff[]=new byte[len];
br.read(buff,0,len);
str=new String(buff,0,len);
fout.write(buff);
System.out.println(str);
sock.close();
}                     
}
catch(Exception e)
{
}
fout.close();
}
}








OUTPUT: (SERVER)


Enter Source Filename::
Test1.java

OUTPUT: (CLIENT)

Enter Destination Filename::

Test2.java

JAVA CODE FOR SESSION TRACKING USING COOKIES


COOKIE1.JAVA

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class Cookie1 extends HttpServlet
{
   public void service(HttpServletRequest req,HttpServletResponse res)
   throws ServletException,IOException
          {
                        res.setContentType("text/html");
                        PrintWriter pw=res.getWriter();
                        String color=req.getParameter("s1");
                        pw.println("<html><body bgcolor="+color+">");
                        pw.println("<h1> <center> Session");
pw.println("<form method=get action=\"http://localhost:8080/servlet/Cookie2\">");
                        pw.println("Name : <input name=name type=text> <br>");
                        pw.println("<input name=bcolor type=hidden value="+color+"><br>");
                        pw.println("<input name=Submit type=submit value=submit>");
                        pw.println("<input name=Reset type=reset value=reset>");
                        Cookie c1= new Cookie("cd",color);
                         Cookie  c2 = new Cookie("id","second page");
                          res.addCookie(c1);
                        res.addCookie(c2);
                        pw.println("</body></html>");
                        pw.close();
          }  
}
           

COOKIE2.JAVA

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class Cookie2 extends HttpServlet
{
   public void doGet(HttpServletRequest req,HttpServletResponse res)
   throws ServletException,IOException
          {
                        res.setContentType("text/html");
                        PrintWriter pw=res.getWriter();
                        String color=req.getParameter("bcolor");
                        pw.println("<html><body bgcolor="+color+">");
                        Cookie c[]=req.getCookies();
                        String s1[]= new String[c.length];
                        String v1[]= new String[c.length];
                        for(int i=0;i<c.length;i++)
                        {
                                    s1[i]=c[i].getName();
                                    v1[i]=c[i].getValue();
                                    pw.println(s1[i]+"</br>");  
                                    pw.println(v1[i]+"</br>");
                        }   
                        String name=req.getParameter("name");
                        pw.println("<h1> Welcome "+name+"! </h1>");
                        pw.println("</body></html>");
                        pw.close();
          } 
}

            

Monday, May 19, 2014

JAVA CODE FOR UDP DATAGRAM-MESSAGE TRANSFER




PROGRAM: (UDP SERVER)

import java.io.*;
import java.net.*;
class UDPserver
{
   public static void main(String args[]) throws Exception
    {
       DatagramSocket serverSocket=new DatagramSocket(9876);
       byte[] receiveData=new byte[1024];
       byte[] sendData=new byte[1024];
     while(true)
     {
         DatagramPacket receivePacket=new                                  

         DatagramPacket(receiveData,receiveData.length);
         serverSocket.receive(receivePacket);
         String sentence=new String(receivePacket.getData());
         System.out.println("RECEIVED:"+sentence);
         InetAddress IPAddress=receivePacket.getAddress();
         int port=receivePacket.getPort();
         String capitalizedSentence=sentence.toUpperCase();
         sendData=capitalizedSentence.getBytes();
        DatagramPacket sendPacket=new     
        DatagramPacket(sendData,sendData.length,IPAddress,port);
        serverSocket.send(sendPacket);
  }
    }
       }

PROGRAM: (CLIENT)

import java.io.*;
import java.net.*;
class  UDPclient
{
     public static void main(String arg[]) throws Exception
      {
          BufferedReader inFromUser=new  BufferedReader(new    
          InputStreamReader(System.in));
          DatagramSocket clientSocket=new  DatagramSocket();
          InetAddress  IPAddress=InetAddress.getByName("localhost");
          byte[] sendData=new byte[1024];
byte[] receiveData=new byte[1024];
String sentence=inFromUser.readLine();
sendData=sentence.getBytes();
DatagramPacket sendPacket=new  DatagramPacket(sendData,sendData.length,IPAddress,9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket=new DatagramPacket(receiveData,receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence=new String(receivePacket.getData());
System.out.println("FROM SERVER:"+ modifiedSentence);
clientSocket.close();
}
  }
  
OUTPUT: (SERVER)


RECEIVED:Hai

OUTPUT: (CLIENT)

Hai

FROM SERVER: hai

JAVA CODE FOR RMI FILE TRANSFER



PROGRAM: (SERVER)

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.sql.*;
import java.io.*;

class buff implements Serializable
     public byte b[];
   }
interface trans extends Remote
 {
    public buff get(String s) throws RemoteException;
}
public class fileTrans extends UnicastRemoteObject implements  trans
{
    fileTrans()throws RemoteException
{    }
  public buff get(String s) throws RemoteException
 {
    buff o=new buff();
  try
    {
      System.out.println(s);
      FileInputStream o1=new FileInputStream(s);
     o.b=new byte[o1.available()];
     o1.read(o.b);
     System.out.write(o.b);
     o1.close();
}
   catch(Exception e)
  {
   System.out.println(e);
}
return o;
  }
public static void main(String[] x)throws Exception
{
  fileTrans tt=new fileTrans();
  Naming.rebind("file",tt);
}
  }







PROGRAM: (CLIENT)

import java.rmi.*;
import java.rmi.registry.*;
import  java.io.*;
interface trans extends Remote
{
   public buff get(String s) throws RemoteException;
}

class getfile
{
   getfile()
  {

   try
   {
     trans m1=(trans)Naming.lookup("rmi://Localhost/file");
     String s="chord.wav";
     buff o=m1.get(s);
     FileOutputStream fo=new FileOutputStream("ch.wav");
     fo.write(o.b);
  }
  catch(Exception e)
{
   System.out.println(e);
}
  }
public static void main(String[] a)
{
  getfile o=new getfile();
}

  }

Followers