6 Replies Latest reply: May 15, 2009 6:01 AM by 807588 RSS

    client-server file transfer application program including encryption

    807588
      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!!!
        • 1. Re: client-server file transfer application program including encryption
          807588
          If you have written code for encrypting and decrypting then you will know that the result of the encryption will be binary data that must be transferred using OutputStream and InputStream and not Writers and Readers.

          Consider using Cipher streams. They make life a lot easier.
          • 2. Re: client-server file transfer application program including encryption
            807588
            ya i have transfer the binary code to a file but now i have to send that encrypted file to the client program which further transfers that file to sever.
            I have posted ma client program above plz guide me how do i do it ...

            Waiting for ur support!!!
            • 3. Re: client-server file transfer application program including encryption
              807588
              tukkish wrote:
              ya i have transfer the binary code to a file but now i have to send that encrypted file to the client program which further transfers that file to sever.
              I have posted ma client program above plz guide me how do i do it ...

              Waiting for ur support!!!
              Sorry but your code is so far from being applicable that you need to start again. In your place I would start with the networking tutorial - http://java.sun.com/docs/books/tutorial/networking/index.html .
              • 4. Re: client-server file transfer application program including encryption
                807588
                ya i tried to transfer the binary code to a file using the following code but its giving error statement like this
                "Exception in thread "main" java.lang.NoSuchMethodError: main"
                import java.io.*;
                
                public class filewriter{
                
                public static void main() throws Exception{
                
                try{
                FileOutputStream fs = new FileOutputStream("out.txt");
                String str = "this is a dummy file";//new String(new char[]{(char)0x4EC1,(char)0x4001});// example of Unicode characters
                byte buff[] = str.getBytes("UTF8");
                fs.write(buff);
                fs.close();
                 
                byte buff1[]=new byte[1024];
                FileInputStream fis = new FileInputStream("out.txt");
                int read = fis.read(buff);
                byte buff2[] = new byte[read];
                for(int i =0; i<read; i++) {
                   if(buff2[i] !=buff1) System.out.println("error");
                }
                fis.close();
                }
                catch( FileNotFoundException e)
                { System.out.println(e);}
                }
                }
                So am trying to transfer the binary code directly to the client considering the binary code as array.
                Plz Help me in this issue.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                • 5. Re: client-server file transfer application program including encryption
                  807588
                  tukkish wrote:
                  ya i tried to transfer the binary code to a file using the following code but its giving error statement like this
                  "Exception in thread "main" java.lang.NoSuchMethodError: main"
                  Sorry but this indication of your lack of basic Java knowledge shows that you are not ready to tackle either IO or networking or encryption. Go back to basics - http://java.sun.com/docs/books/tutorial/ .
                  • 6. Re: client-server file transfer application program including encryption
                    807588
                    Waiting for ur support!!!
                    http://www.catb.org/~esr/faqs/smart-questions.html#writewell

                    How To Ask Questions The Smart Way
                    Eric Steven Raymond
                    Rick Moen

                    Write in clear, grammatical, correctly-spelled language

                    We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding (often enough to bet on, anyway). Answering questions for careless and sloppy thinkers is not rewarding; we'd rather spend our time elsewhere.

                    So expressing your question clearly and well is important. If you can't be bothered to do that, we can't be bothered to pay attention. Spend the extra effort to polish your language. It doesn't have to be stiff or formal — in fact, hacker culture values informal, slangy and humorous language used with precision. But it has to be precise; there has to be some indication that you're thinking and paying attention.

                    Spell, punctuate, and capitalize correctly. Don't confuse "its" with "it's", "loose" with "lose", or "discrete" with "discreet". Don't TYPE IN ALL CAPS; this is read as shouting and considered rude. (All-smalls is only slightly less annoying, as it's difficult to read. Alan Cox can get away with it, but you can't.)

                    More generally, if you write like a semi-literate b o o b you will very likely be ignored. So don't use instant-messaging shortcuts. Spelling "you" as "u" makes you look like a semi-literate b o o b to save two entire keystrokes.