Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

client-server file transfer application program including encryption

807588May 15 2009 — edited May 15 2009
I have these two programs which are in java, which implement my client to server file transfer.
I want a java program, which will:



1. call a function that takes my text (as a file or from html front-end) and performs ENCRYPTION (using DES or AES or any other strong encryption method) and then put the encrypted data in a file (example: file1.enc).

2. next call my sender (Client) process and pass this file1.enc as argument to it. my sender function should be slightly modified to accept this encrypted file as input parameter.

3.my sender process will send it to the receiver(server) and the server will create a new file (example: file2.enc) in its end. and copy all contents of tranferred encrypted file in it.

4.then, the receiver should be able to call a DECRYPTION function, which will put the decrypted data in the destination file.

I have written a code for encryption and decryption already.

Am attaching ma client and server program here.
 
//server program
import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;


public class ServerRun {
    public ServerRun()
    {
        try {
            ServerSocket serverSocket = null;
            boolean listening = true;

            try {
                serverSocket = new ServerSocket(4444);
            } catch (IOException e) {
                System.err.println("Could not listen on port: 4444.");
                System.exit(-1);
            }

            while (listening) {
                new Server(serverSocket.accept()).start();
            }
            serverSocket.close();
        } catch (IOException ex) {
            Logger.getLogger(ServerRun.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public static void main(String[] args) throws IOException {
        ServerRun s=new ServerRun();
    }
     class Server extends Thread {
    private Socket socket = null;
String servermsg[]={"u can i send","i am reciving","receiving is done","bye"};
String clientmsg[]={"can i send","i am sending","sending is done","bye"};

    public Server(Socket socket) {
	
	this.socket = socket;
    }

    public void run() {
try {
	    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
	    BufferedReader in = new BufferedReader(
				    new InputStreamReader(
				    socket.getInputStream()));

	    String inputLine, outputLine;
	   
	    out.println("u can talk");

	    while ((inputLine = in.readLine()) != null) {
                  System.out.println("Client: " + inputLine);
                if(inputLine.equals(clientmsg[0]))
                {
		
		 out.println(servermsg[0]);
                }
                if(inputLine.equals(clientmsg[1]))
                {
		
		 out.println(servermsg[1]);
                }
                if(inputLine.equals(clientmsg[2]))
                {
		
		 out.println(servermsg[2]);
                }
                
                
                
                
		 if(inputLine.equals(clientmsg[3]))
                {
		  out.println("i am closing ");
                break;
		
                }
                
		   
	    }
            
	    out.close();
	    in.close();
	    socket.close();

	} catch (IOException e) {
	    e.printStackTrace();
	}
    }
}

}
//client program

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

public class ClientRun {
    public static void main(String[] args) throws IOException {

        Socket kkSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;
String s[]={"can i send","i am sending","sending is done","bye"};


        try {
            kkSocket = new Socket("localhost", 4444);
            out = new PrintWriter(kkSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(kkSocket.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 to:.");
            System.exit(1);
        }

       
        String fromServer;
        String signal;

        while ((fromServer = in.readLine()) != null) {
                     
                out.println(s[0]);

		while((signal=in.readLine())!=null) { System.out.println("Server response: " + signal); break; }

		out.println(s[1]);

		out.println(args[0]);

		FileInputStream fin= new FileInputStream (args[0]);
		int bytesRead=0;
		byte[] nextBytes = new byte[1024];

				kkSocket.getOutputStream().flush();
				OutputStream output = kkSocket.getOutputStream();
				int tx = 0;
				while((bytesRead = fin.read(nextBytes)) >0) {
					output.write(nextBytes, 0, bytesRead);
					tx += bytesRead;
				}
				
				System.out.println("Bytes read: " + tx);

				//output.flush();System.out.println("output flushed");
				//output.close();System.out.println("output closed");
				//fin.close();System.out.println("fin closed");

		//while((signal=in.readLine()) != null) { System.out.println("Server response: " + signal); break; }
		out.println(s[3]);
        }

        out.close();
        in.close();
        //stdIn.close();
        kkSocket.close();
    }
}
Plz Help me to solve this problem am not able to solve this struggling from many.

eagerly waiting for you support!!!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 12 2009
Added on May 15 2009
6 comments
1,442 views