Results 1 to 3 of 3
  1. #1
    impacted is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default Network File Transfer Issue - Data is garbled on recieving end (serverside)

    I don't often stop to ask questions with the problems I have, 99% of the time I can solve any issues on my own through determination and reading and experimentation, but this one has me stuck. I have done a LOT of troubleshooting trying to narrow the source of the problem, which it would seem I have done so, but I'm still unsure of the cause. So, here's the scenario:
    I'm writing client-server software which allows the user to upload any file of any type (for the purposes of this inquiry, I have used a .PDF file) as an "attachment". The problem is that the file that is reconstructed on the receiving end (in this case the server) is garbled. I made my program to read the file from disk then write it right back to a temp file before sending it over the network, to ensure that the data being sent over the network was not garbled, and then compared the temp file to the resulting file on the receiving end. Here are the results:

    .PDF file header and footer BEFORE transmission:
    FILE HEADER:

    %PDF-1.3
    %туЯг
    1 0 obj
    <</Metadata 40 0 R/Pages 2 0 R/Type/Catalog>>
    endobj
    2 0 obj
    <</MediaBox[0.0 0.0 604.56 796.56]/Count 11/Type/Pages/Kids[39 0 R 38 0 R]>>
    endobj
    4 0 obj
    <</Parent 39 0 R/Contents 6 0 R/PieceInfo<</PSL<</Private<</V(3.1.815)>>/LastModified(D:20120314211220-00'00')>>>>/MediaBox[0.0 0.0 604.56 796.56]/Resources<</XObject<</Im0 5 0 R>>/ProcSet[/PDF/ImageB]>>/Type/Page>>
    endobj
    5 0 obj
    <</Subtype/Image/Length 70742/Filter/CCITTFaxDecode/Name/X/BitsPerComponent 1/ColorSpace/DeviceGray/Width 5038/DecodeParms<</K -1/Columns 5038/Rows 6638>>/Height 6638/Type/XObject>>stream


    FILE FOOTER:
    endstream
    endobj
    4 0 obj<</Annots[43 0 R]/PieceInfo<</PSL<</LastModified(D:20120314211220-00'00')/Private<</V(3.1.815)>>>>>>/MediaBox[0.0 0.0 604.56 796.56]/Resources<</ProcSet[/PDF/ImageB]/XObject<</Im0 5 0 R>>>>/Type/Page/Contents 6 0 R/Parent 39 0 R>>
    endobj
    1 0 obj<</Pages 2 0 R/Metadata 46 0 R/AcroForm<</SigFlags 3/DR<</Font<</ZaDb 45 0 R/Helv 44 0 R>>/Encoding/WinAnsiEncoding>>/DA(/Helv 0 Tf 0 g )/Fields[43 0 R]>>/Type/Catalog/Perms<</DocMDP 41 0 R>>>>
    endobj
    7 0 obj<</CreationDate(D:20120314151220-06'00')/ModDate(D:20120314151220-06'00')/Creator(PFU ScanSnap Manager 5.0.12)/Producer(Adobe PDF Scan Library 3.1)>>
    endobj
    xref
    0 2
    0000000000 65535 f
    0000671285 00000 n
    4 1
    0000671040 00000 n
    7 1
    0000671493 00000 n
    41 6
    0000666240 00000 n
    0000669497 00000 n
    0000666113 00000 n
    0000669323 00000 n
    0000669421 00000 n
    0000669702 00000 n
    trailer
    <</Info 7 0 R/Size 47/ID [<57c320c3806136acc6190e73e516bb3e><be1d8f591cca27d cde2af31435fefd47>]/Prev 665136/Root 1 0 R>>
    startxref
    671657
    %%EOF


    .PDF file header and footer AFTER transmission, on receiving end (serverside):
    FILE HEADER:

    ‹П}'ыН„šєяiАƒ§ў:ОїHBїљк}ѕ3ћыTћЗёОCHYVОїАЗ‡џЛi Џoїwџџ§їПЊоЉьчяј†yW§ˆo§яiюЎЊwџЏwxWгЎAИфэј!ЖЕџњы §?wКЄОООяТZгьˆ0]ЊЎ@‹
    ЛЕТ 'О§№в§[*zэ/mv‘7ћнф2Е&№.ьшЛXh"l+˜l6RЦэ#œRzЗm Є§pТ}їуЫs#Нїt[*_фьу|З) е§Ёк ЇјЛhЗаTоЏ„4ыі№ŠъM;џН
    КЛВз.PЃЛѕ{R .œжE|~š šфФ;ь5*Zю“Џ{ER іеЗЄC=.eC>ЖC]Ј
    ъ‚'dZаІšzВXк*СAНa‚jлАЈD&ЄБ…ъEr‘†СGЛ!Ђэv˜ С5ј@п*т"d"
    


    FILE FOOTER:
    endstream
    endobj
    4 0 obj<</Annots[43 0 R]/PieceInfo<</PSL<</LastModified(D:20120314211220-00'00')/Private<</V(3.1.815)>>>>>>/MediaBox[0.0 0.0 604.56 796.56]/Resources<</ProcSet[/PDF/ImageB]/XObject<</Im0 5 0 R>>>>/Type/Page/Contents 6 0 R/Parent 39 0 R>>
    endobj
    1 0 obj<</Pages 2 0 R/Metadata 46 0 R/AcroForm<</SigFlags 3/DR<</Font<</ZaDb 45 0 R/Helv 44 0 R>>/Encoding/WinAnsiEncoding>>/DA(/Helv 0 Tf 0 g )/Fields[43 0 R]>>/Type/Catalog/Perms<</DocMDP 41 0 R>>>>
    endobj
    7 0 obj<</CreationDate(D:20120314151220-06'00')/ModDate(D:20120314151220-06'00')/Creator(PFU ScanSnap Manager 5.0.12)/Producer(Adobe PDF Scan Library 3.1)>>
    endobj
    xref
    0 2
    0000000000 65535 f
    0000671285 00000 n
    4 1
    0000671040 00000 n
    7 1
    0000671493 00000 n
    41 6
    0000666240 00000 n
    0000669497 00000 n
    0000666113 00000 n
    0000669323 00000 n
    0000669421 00000 n
    0000669702 00000 n
    trailer
    <</Info 7 0 R/Size 47/ID [<57c320c3806136acc6190e73e516bb3e><be1d8f591cca27d cde2af31435fefd47>]/Prev 665136/Root 1 0 R>>
    startxref
    671657
    %%EOF
    џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџ џџ


    Obviously there is some serious garbling occurring here. I have also checked that the filesizes match on both ends, and they do. The resulting garbled file is exactly the same size as the original, but obviously can no longer be viewed using a PDF reader, as I get the message the file is corrupt.

    Now, to throw a couple curve balls into this scenario, I must mention that, first, strangely, my file transfer code works perfectly as long as I am connecting my sockets to 127.0.0.1. The moment I use my router's public IP it begins to garble like this. I am on a LAN behind a router/cable modem using NAT. I have gone into my router and set up port forwarding, turned off the firewall, set the server machine on the DMZ in order to confirm that it is not my router that is causing problems, and is not as far as I can tell. I have tried initiating the transfer by running the server and client programs on the same computer, I have run the client on a different computer on the internal LAN, and I have run the client from a computer outside the LAN over the internet, and the results are exactly the same. Secondly, the way my client works is that the user may upload an attachment to the server and then subsequently view it via downloading it from server and loading it in viewing software on the local machine. The viewing code works perfectly. If I connect my client to 127.0.0.1 and upload a file to my server, I can then view that attachment from any computer whether it is on the LAN or otherwise on the internet somewhere. Essentially this is the same code, why the upload is garbling data but the download is working perfectly is also beyond me.

    So here is the actual code that I am working with:
    CLIENT SIDE CODE (UPLOAD):
    Java Code:
    // UPLOAD ATTACHMENT
    	public void UploadAttachmentTask() {
    		// CYCLE THROUGH EACH SELECTED FILE
    		for(int i = 0; i < attach_file.length; i++) {
    			// GET TOTAL COMBINED LENGTH OF ALL FILES
    			totalBytes += attach_file[i].length();
    		}
    		for(int i = 0; i < attach_file.length; i++) {
    			// READ THE FILE FROM DISK AND UPLOAD TO SERVER
    			try {
    				// INITIALIZE
    				fIn = 
    new BufferedInputStream(new FileInputStream(attach_file[i].getAbsolutePath()));
    				byte_buf  = new byte [(int)attach_file[i].length()];
    				server = new Socket(GUI.config.ServerIP, GUI.config.ServerPort);
    				w = new PrintWriter(server.getOutputStream());
    
    				// SEND UPLOAD COMMAND TO SERVER
    				w.println(efid_gui_handler.UPLOAD_FILE + " " + filename
     + ":" + attach_file[i].getName());
    				w.flush();
    
    				// READ FILE
    				fIn.read(byte_buf, 0, byte_buf.length);
    				fIn.close();
    				
    				// DEBUG - this is where I am writing the "temp" file to make sure data is good
    // before being transmitted
    				fOut = 
    new BufferedOutputStream(new FileOutputStream(efid_gui_handler.workingDir + "\\temp.txt", false));
    				fOut.write(byte_buf, 0, byte_buf.length);
    				fOut.flush();
    				fOut.close();
    				
    				// PROVIDE SERVER THE FILESIZE
    				w.println(Integer.toString(byte_buf.length));
    				w.flush();
    
    				// SET UP STREAM AND WRITE DATA
    				data_stream = new BufferedOutputStream(new DataOutputStream(server.getOutputStream()));
    //				for(currentByte = 0; currentByte < byte_buf.length; currentByte++) {
    //					data_stream.write(byte_buf[currentByte]);
    					//DEBUG
    //					System.out.print(byte_buf[currentByte]);
    //					setProgress((int)(((totalBytesRead + currentByte) / totalBytes) * 100));
    //				}
    				//DEBUG
    				data_stream.write(byte_buf, 0, byte_buf.length);
    				data_stream.flush();
    				totalBytesRead += byte_buf.length;
    
    				// SHUTDOWN NETWORK CONNECTIONS
    				data_stream.close();
    				w.close();
    				server.close();
    			} catch (UnknownHostException e) {
    				error = true;
    				JOptionPane.showMessageDialog(null, "Could not find server: " + '\n' + e.toString().substring(e.toString().indexOf(":") + 1, e.toString().indexOf(":", e.toString().indexOf(":") + 1)), "Network Error!", JOptionPane.ERROR_MESSAGE);
    				return;
    			} catch (FileNotFoundException e) {
    				error = true;
    				JOptionPane.showMessageDialog(null, "Could not find file \'" + attach_file[i] + "\': " + '\n' + e.toString(), "File Error!", JOptionPane.ERROR_MESSAGE);
    				return;
    			} catch (IOException e) {
    				error = true;
    				JOptionPane.showMessageDialog(null, "No connection to server: " + '\n' + e.toString(), "Error!", JOptionPane.ERROR_MESSAGE);
    				return;
    			}
    		}
    		totalBytes = totalBytesRead;
    		
    		return;
    	}

    Note that I have commented out a "for" block. I have tried transmitting the data both one byte at a time (using the code that is currently commented out), and en mass, as it is now. Same result.


    SERVER SIDE (DOWNLOAD):
    Java Code:
    // RECIEVE ATTACHMENT FROM CLIENT
    	//TODO
    	public void recieveAttachment(BufferedReader client_in, Socket client, 
    String claimant, String filename) {
    		String fullpath = workingDir + ATTACHMENTS_DIR + claimant + "\\";
    		BufferedOutputStream fOut;
    		BufferedInputStream binary_stream;
    		byte[] byte_buf;
    		int filesize, bytesRead = 0, currentByte = 0;
    		
    		try {
    			// RECIEVE ATTACHMENT FILE SIZE FROM SERVER
    			filesize = Integer.decode(client_in.readLine());
    			byte_buf = new byte [filesize];
    			
    			// INITIALIZE - MAKE SURE DIRECTORY EXISTS IF NOT CREATE IT
    			if(! (new File(fullpath)).exists())
    				if(! (new File(fullpath)).mkdir()) {
    					JOptionPane.showMessageDialog(this, "Could not create directory \'"
     + fullpath + "\':" + '\n' + "Check that the disk is not write protected.", "File Error!",
     JOptionPane.ERROR_MESSAGE);
    					return;
    				}
    			binary_stream = new BufferedInputStream(new DataInputStream(client.getInputStream()));
    			
    			// DOWNLOAD FILE DATA
    			for(currentByte = 0; currentByte < filesize; currentByte++) {
    				byte_buf[currentByte] = (byte) binary_stream.read();
                                    //DEBUG
    //				System.out.print((char) byte_buf[currentByte]);
    			}
    			bytesRead = currentByte;
    			
    			// WRITE DATA TO DISK
    			fOut = new BufferedOutputStream(new FileOutputStream(fullpath + filename, false));
    			fOut.write(byte_buf, 0, byte_buf.length);
    			fOut.flush();
    			
    			// SHUT DOWN STREAMS
    			fOut.close();
    			binary_stream.close();
    		} catch (UnknownHostException e) {
    			console.display("Could not recieve attachment from client:" + '\n' + e.toString() + '\n');
    			return;
    		} catch (FileNotFoundException e) {
    			console.display("Could not recieve attachment from client:" + '\n' + e.toString() + '\n');
    			return;
    		} catch (IOException e) {
    			console.display("Could not recieve attachment from client:" + '\n' + e.toString() + '\n');
    			return;
    		}
    		
    		return;
    	}
    I do apologize for the sheer length of the post, but I am the type of person who will do everything I can possibly think of before coming to a forum to ask for help, and I thought it wise to post the results of the troubleshooting steps I have already covered.
    One more curious thing is that I have written auto client update code into this client-server application, and it works beautifully where it requests the current client version from server, if its out of date, client downloads copy of latest client (in the form of a .JAR file) and then executes it. This all works perfectly from any computer whether on LAN or over the internet. Why is my file transfer code causing this garbling of data when trying to upload attachments, but not when I'm downloading them to view them from the client, and not when I'm downloading an entire executable .JAR file? I'm at a loss on this one, and I have been working on this for a week and a half with no progress. It's really starting to give me a headache, and whats more is that I really need this software to work (and this is the ONLY part of it that isn't working). I would be eternally grateful if someone were able to provide some insight or suggestions on this matter. I'm grateful to anyone who actually took the time to read this large post. Thank you.

  2. #2
    impacted is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default Re: Network File Transfer Issue - Data is garbled on recieving end (serverside)

    And I see that, even though half of this forum page on my browser is empty space, my code is being truncated instead of wrapping or something sensible like that, so that it's not readable. Sigh.
    Last edited by impacted; 03-23-2012 at 06:51 AM.

  3. #3
    impacted is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default Re: Network File Transfer Issue - Data is garbled on recieving end (serverside)

    FIXED MY OWN PROBLEM for anyone who may just run across this issue. After more than a week and a half of headaches and trying everything under the sun, finally, a solution to this data corruption. All I did in order to fix this issue was to, after sending the upload file command and attachment file size to the server, disconnect the socket and open a new one on config.serverPort + 1. This gave me a new, clean stream to read/write binary data, and indeed, it fixed the problem. Works perfectly now. Hope this helps someone who may run into this problem. Good luck.

Similar Threads

  1. File transfer in a network
    By shubhcool1 in forum Advanced Java
    Replies: 4
    Last Post: 08-29-2011, 06:19 PM
  2. pls..transfer data in socket
    By langkhach_codon in forum Networking
    Replies: 7
    Last Post: 03-28-2010, 05:21 AM
  3. data loss in the secure file transfer using java
    By antony75 in forum Networking
    Replies: 4
    Last Post: 02-09-2009, 06:56 AM
  4. Fast Data Transfer 0.6.4
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-20-2007, 08:55 AM

Posting Permissions

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