Saturday, January 29, 2011

Tcp Echo (Client - Server)

//Client

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

class client
{
    public static void main(String[] arr) throws Exception
    {
        BufferedReader br=null;
        PrintWriter pw=null;
        Scanner input=null;
        Socket cs;

        cs = new Socket("localhost",1987);
        input = new Scanner(System.in);
        br = new BufferedReader(new InputStreamReader(cs.getInputStream()));
        pw = new PrintWriter(cs.getOutputStream(),true);

        System.out.print("enter data : ");
        String str=input.nextLine();

        pw.println(str);

        System.out.println("Data from Server : " + br.readLine());
    }
}

//Server


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

class myserver implements Runnable
{
    Socket cs;
    Thread t;
    BufferedReader br = null;
    PrintWriter pw = null;

    myserver(Socket cs)
    {
        this.cs=cs;
        t = new Thread(this);
        t.start();
    }
   
    public void run()
    {
        while(true)
        {
            try
            {
                br = new BufferedReader(new InputStreamReader(cs.getInputStream()));
                pw = new PrintWriter(cs.getOutputStream(),true);
                pw.println(br.readLine());
            }
            catch(IOException ie){}
        }
    }

}

class server
{
    public static void main(String[] arr)
    {
        ServerSocket ss;
        try
        {
            ss = new ServerSocket(1987);

            while(true)
            {
                Socket cs=ss.accept();
                new myserver(cs);
            }
        }
        catch(IOException ie){}
    }
}

Image Encryption & Decryption in JAVA

// Client.java

import java.io.*;
import javax.crypto.*;
import java.security.*;
import java.util.*;
import javax.crypto.spec.*;
import java.net.*;

class client
{
    public static void main(String[] arr) throws Exception
    {

        int i;
        Key k;
        Cipher c;
        KeyGenerator kg;
        FileOutputStream fos;
        CipherInputStream cis;

        Scanner input = new Scanner(System.in);

        Socket cs = new Socket("localhost",1987);
   
        ObjectOutputStream oos = new ObjectOutputStream(cs.getOutputStream());

        c = Cipher.getInstance("DES");
        kg = KeyGenerator.getInstance("DES");
        k = kg.generateKey();

        c.init(Cipher.ENCRYPT_MODE,k);
   
        cis = new CipherInputStream(new FileInputStream(new File("my.jpg")),c);

        int j=0;

        oos.writeObject(k);       

        while((i=cis.read())!=-1)
        {
            oos.writeInt(i);
            oos.flush();
        }
        cs.close();
    }
}


// server.java

import java.io.*;
import javax.crypto.*;
import java.security.*;
import java.util.*;
import javax.crypto.spec.*;
import java.net.*;


class myserver implements Runnable
{
    Socket cs;
    Thread t;
    Cipher c;
    KeyGenerator kg;

    myserver(Socket cs)
    {
        this.cs=cs;

        try
        {

        }catch(Exception e)
        {
   
        }

        t = new Thread(this);
        t.start();
    }

    public void run()
    {

        try
        {
            c = Cipher.getInstance("DES");
            ObjectInputStream ois = new ObjectInputStream(cs.getInputStream());
            FileOutputStream fos = new FileOutputStream("myenc.jpg");

            Key k = (Key) ois.readObject();
            c.init(Cipher.DECRYPT_MODE,k);

            int i;
            while((i=ois.readInt())!=-1)
            {
                fos.write(i);
            }
        }
        catch(Exception e)
        {

        }
       
        try
        {
            CipherInputStream cis = new CipherInputStream(new FileInputStream(new File("myenc.jpg")),c);

            FileOutputStream fos1 = new FileOutputStream("again.jpg");

            int j;
            while((j=cis.read())!=-1)
                fos1.write(j);

            fos1.close();
        }
        catch(Exception e)
        {

        }   
    }
}

class server
{
    public static void main(String[] arr) throws Exception
    {
        Socket cs;
        ServerSocket ss = new ServerSocket(1987);

        while(true)
        {
            cs = ss.accept();
            new myserver(cs);
        }
    }
}

Disqus for yogi's talk

comments powered by Disqus