• Post Reply Bookmark Topic Watch Topic
  • New Topic

corrupt JPG file with sockets....

 
Kevin Schneider
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello
in my quest of learning sockets, i am developing a simple file transfer program. first i tested it locally and i sent and received both a small text file, and a 1mb jpg file. i then tested it over the internet, and i received the text file fine, but the jpg was corrupt. can anyone tell me why this is happening, and the best way to avoid this problem? heres some of my code to give you an idea of what i'm doing...


thanks to all who reply
[ October 22, 2003: Message edited by: Kevin Schneider ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Regardless of the value of bytesRead, which can be equal to buffer.length, or any smaller value, you write buffer.length bytes to the file each time through the loop. This guarantees garbage at the end, and it makes garbage in the middle highly likely, too. Both the read and write loops should do this instead:

[ October 23, 2003: Message edited by: Ernest Friedman-Hill ]
 
Kevin Schneider
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey thats a great idea. thanks for the suggestion. it works perfectly now
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!