Results 1 to 11 of 11
  1. #1
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    7

    Default Sending Image through sockets

    I am trying to send an image through socket to my server, i have two methods to read and write files

    Read :

    Java Code:
    public byte[] readFile(Socket socket) {
    
    		try {
    			DataInputStream input = new DataInputStream(socket.getInputStream());
    
    			int numberOfBytes = readInt(socket);
    
    			if (numberOfBytes == 0)
    				return null;
    
    			ByteArrayOutputStream output = new ByteArrayOutputStream(numberOfBytes);
    
    			int BUFFER_SIZE = 8192;
    
    			byte[] buffer = null;
    
    			if (numberOfBytes > BUFFER_SIZE) {
    				buffer = new byte[BUFFER_SIZE];
    			} else {
    				buffer = new byte[numberOfBytes];
    			}
    
    			int bytesRead = 0;
    			int sum = 0;
    
    			// While there is something to read
    			while ((bytesRead = input.read(buffer)) > 1) {
    
    				// count the total read bytes
    				sum += bytesRead;
    				output.write(buffer, 0, bytesRead);
    
    				int bytesLeft = numberOfBytes - sum;
    
    				if (bytesLeft > BUFFER_SIZE) {
    					buffer = new byte[BUFFER_SIZE];
    				} else {
    					buffer = new byte[bytesLeft];
    				}
    
    				if (sum == numberOfBytes)
    					break;
    			}
    
    			// in case ByteArrayOutputStream decide to allocate more space
    			byte[] outputArray = output.toByteArray();
    			// make a new array and copy only the actual file bytes
    			byte[] result = new byte[numberOfBytes];
    			System.arraycopy(outputArray, 0, result, 0, numberOfBytes);
    
    			System.out.println("Output lenght : " + result.length);
    			return result;
    		} catch (Exception e) {
    			terminateConnection(e);
    		}
    		return null;
    	}
    Write :

    Java Code:
    public void sendFile(Socket socket, File file) {
    
    		try {
    
    			byte[] fileBytes = Files.readAllBytes(file.toPath());
    			System.out.println("file size : " + fileBytes.length);
    
    			InputStream input = new FileInputStream(file);
    			DataOutputStream output = new DataOutputStream(socket.getOutputStream());
    
    			
    			int BUFFER_SIZE = 8192;
    			int numberOfBytes = fileBytes.length;
    
    			send(socket, numberOfBytes);
    
    			byte[] buffer = null;
    
    			if (numberOfBytes > BUFFER_SIZE) {
    				buffer = new byte[BUFFER_SIZE];
    			} else {
    				buffer = new byte[numberOfBytes];
    			}
    
    			int bytesRead = 0;
    			int sum = 0;
    
    			// While there is something to read
    			while ((bytesRead = input.read(buffer)) > 1) {
    
    				// count the total read bytes
    				sum += bytesRead;
    				output.write(buffer, 0, bytesRead);
    
    				int bytesLeft = numberOfBytes - sum;
    
    				if (bytesLeft > BUFFER_SIZE) {
    					buffer = new byte[BUFFER_SIZE];
    				} else {
    					buffer = new byte[bytesLeft];
    				}
    
    				if (sum == numberOfBytes)
    					break;
    			}
    			input.close();
    		} catch (Exception e) {
    			terminateConnection(e);
    		}
    	}
    Those two methods work very well for sending plain text files. Now if I use them to send an image to a local server ( client = windows, server = windows) then everything is working as expected, the problem appears when I use my server which uses Linux. Every time I try to send an image and get it back, it has distortions and the colors are way off.. so I tried to debug the problem

    I check that the bytes on the local file and on the server are exactly the same so there isn't anything fishy there, after I try to take the file from the server (SFTP) and check the content of the file( with a txt editor like notepad++ ) i saw that there are changes inside.

    I could make some guesses.. I know there are differences on files ( encoding etc ) between Linux and Windows so it might have to do with that. I was thinking that there is something to do with the above code, but working on the Windows and not on Linux makes it difficult..

    Does anyone have any idea of what is going on?

    PS. the above code is not the most efficient way i guess but it is just for experimentation
    Last edited by ShadowWalker; 07-23-2017 at 05:29 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Sending Image through sockets

    An idea for testing to see if the data is being changed by the code.
    Create a byte array with consecutive values: 0,1,2,3,4... 255, 0,1,...
    Send it and read it and check its contents to see if and where there are any changes made.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    7

    Default Re: Sending Image through sockets

    I see... that's why you told me to send consecutive values...and the '255' was not irrelevant. As I found out the Java doesn't have unsigned bytes; all bytes are treated as signed (range -128 to 127). So if I understand correctly the bytes of file in the system is unsigned (0 to 255) but Java treat them as signed? If so, I still don't understand why there is the difference between Windows and Linux, I should have the same error on both OS. I need to investigate it more.. Thank for your time i will post back when I found out what is going on ( I hope ) :P

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Sending Image through sockets

    Why are you doing this

    while ((bytesRead = input.read(buffer)) > 1) {

    and not this?

    while ((bytesRead = input.read(buffer)) != -1) {

    Since most of these types of reads return a -1 on end of input stream or file, it seems only natural
    to use that. If you read 0 bytes, then no harm done and no offsets are adjusted.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    7

    Default Re: Sending Image through sockets

    Hmm indeed, I actually when i wrote the code i didn't focus much on the while statement cause I thought it doesn't really matter because the while will stop eventually only on the if statement on the end.. Thank for pointing that out

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Sending Image through sockets

    Unsigned only refers to whether the sign bit is used in comparisons of numbers and on print out (unsigned gives you another power of 2 display magnitude). I do not believe it should affect data.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Sending Image through sockets

    The while will stop if it reads 1 or 0 bytes. Which means in certain cases you could get an incomplete read of the input stream. And I believe this might be more likely when doing network transfers rather than simple file reads within a system.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    7

    Default Re: Sending Image through sockets

    Well i believe that too, but i can't really imagine what else it the problem here.. First i thought I was sending more or less bytes that i should, but that not really the case.. it just i have wrong chunks for bytes into the file.. i am going to check what is been send ( in int format ) from the client and what is received from the server ( in int format as well ) and check if both are similar, which i believe are not.. but it's a start i guess

  9. #9
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    7

    Default Re: Sending Image through sockets

    Quote Originally Posted by jim829 View Post
    The while will stop if it reads 1 or 0 bytes. Which means in certain cases you could get an incomplete read of the input stream. And I believe this might be more likely when doing network transfers rather than simple file reads within a system.

    Regards,
    Jim
    Wow... I didn't thought of that... but even though.. i still believe that's not the case cause i have the correct byte size..oh wait.. do I? I allocate on a local variable the size I need so if you are right ( of course you are ) then i should contain garbage inside my variable... I am going to check that right now!! Thanks for the help
    Last edited by ShadowWalker; 07-23-2017 at 05:33 PM.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Sending Image through sockets

    Most image files are relatively large. When you did your text data test, was the file size greater than 8192? Otherwise, your routines would not be exercised to their fullest when sending text.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    7

    Default Re: Sending Image through sockets

    Well I fixed it with your advice about the condition in while loop + i had a different BUFFER_SIZE on the server side i guess that was kind of strange too :P , now about my text files their size was relative small, i guess i was lucky i didn't had any problem with them too, but if their size was less than my BUFFER_SIZE i was allocating less space , equal with their size on both sides ( server, client ) i believe that ok.

    Again thanks both of you for your effort.

Similar Threads

  1. Server Sockets sending images from URL
    By devett in forum Networking
    Replies: 1
    Last Post: 06-07-2011, 07:25 AM
  2. Sending linked lists via sockets
    By deepthought015 in forum Networking
    Replies: 1
    Last Post: 04-26-2009, 02:33 AM
  3. Sending files over sockets!
    By rameshraj in forum Networking
    Replies: 2
    Last Post: 05-30-2008, 10:18 PM
  4. Sending Mail Using Sockets
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:05 PM
  5. Problems sending file throught TCP sockets
    By Nite in forum Advanced Java
    Replies: 2
    Last Post: 08-04-2007, 09:01 PM

Posting Permissions

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