Results 1 to 2 of 2

Thread: client/server file send problem

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    client/server file send problem

    (These are just the relevant snippets from my program for my question. out and in are the socket streams, im sure you can see what the rest is)
    This snippet here is for the client to send the file to the server, encrypted..

    Code:
    			Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    			cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    
    			// Send file
    			FileInputStream fin = new FileInputStream(file);
    			byte[] buffer = new byte[8192];
    			byte[] bout = null;
    			int length;
    
    			while (true) {
    			    length = fin.read(buffer);
    			    out.writeInt(length);
    			    if (length == -1) break;
    
    			    bout = cipher.update(buffer, 0, length);
    			    out.write(bout);
    			}
    and the server decrypting the file and putting it together on the server..

    Code:
    		    Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    		    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    
    		    // receive file
    		    FileOutputStream fout = new FileOutputStream(file);
    		    
    		    byte[] buffer = new byte[8192];
    		    byte[] bout = null;
    		    int length;
    		    
    		    while (true) {
    			length = in.readInt();
    			if (length <= 0) break;
    			in.read(buffer, 0, length);
    		
    			bout = cipher.update(buffer, 0, length);
    			fout.write(bout);
    		    }
    Everything works fine, except 1 problem. On the client side, say i want to send test.txt and test.txt contains this:

    Code:
    Hello there 
    
    whats up
     
    Multi line test stuff!!
    on the server side, the file ends up like this
    Code:
    Hello there 
    
    whats up
     
    Multi line
    on the last line, it should say: Multi line test stuff!!
    anyone see what im doing wrong?
    Last edited by Pouncer; Feb 14th, 2007 at 11:16 AM.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: client/server file send problem

    read() and write() with sockets are not guaranteed to return as much data as requested. Perhaps that's the problem?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width