I tried what you said...
Originally posted by Joe Ess:
Just open the file with a FileInputStream and write it to the socket's InputStream. On the client, do the reverse: socket's OutputStream -> FileInputStream.
But I still cant get it to send. I am getting a read failed message on the server when I try and write the image back to a file. I have a print statement to see what is being sent.
Im sure Im still not doing something right. Any further tips?
Originally posted by Dan Lynch:
Like I said above, you don't write the input stream instance to the output stream. You READ from the input stream and WRITE to the output stream. You did it on the server side correctly (except for using a Writer, as noted below).
I see you are using out.println() to write the data to the socket. Dont. It will introduce new lines into the data, which will cause havoc in a GIF. That means whatever you declared out to be is inappropriate for binary data.
My example above wasn't the best. Readers and Writers are specialized streams for use with character-based data. You want to use Streams, which don't do character conversion on the data. The same example as above, done with Streams, from the Java Tutorial:
you should take a gander at the Java IO Tutorial. Java IO is not easy to grasp at first, but once you do, you can use the same classes to do IO on sockets, files, databases, URL's. Pretty much everything.
Im another step closer...I think. I have it now sending information. The problem is that its mixing up the information, or so it seems. Here are the bytes on both sides.
Client..255 216 255 224 0 16 74 70 73 70 0 1 1 1 0 72 0 72 0 0 255 219 0 67 0 5 3 4 4 4 3 5 4 4 4 5 5 5 6 7 12 8 7 7 7 7 15 11 11
Server.. 255 255 0 74 73 0 1 0 0 0 255 0 0 3 4 3 4 4 5 6 12 7 7 15 11 12 15 18 15 17
The client is where the file comes from. As you can see the sequence isnt followed. Am I still missing some step.
Thanks again. You were right David.
I also noticed before that all the information is not being sent to the server. The server code does not print out STUFF and the number of bytes. Am I sending the information badly by sending every byte on their own. Is there some kind of limit to the amount of informatin being sent. Ill include the client code so you can see whats happening.
[ April 28, 2005: Message edited by: Dan Lynch ]
To send and receive chunks for a big performance gain, wrap the input/output streams with their buffered cousins: BufferedInput/OutputStream. Even if you write out one byte at a time, the buffered stream won't write to the underlying stream until its buffer is full. For example, a buffer size of 2000 bytes would accept 2000 write(byte) calls before actually writing to the network or file, and then it would write the entire buffer at once.*
Similar to sending chunks, use the write(byte) and read(byte) methods to operate on entire arrays of bytes rather than single bytes. Be forewarned, though, that read(byte) returns the number of bytes actually read, meaning that it may not fill the full array. For writing the possibly partial array to the file's output stream, you'll find write(byte, offset, length) helpful; length here would be the value returned from read().
Buffering is pretty much essential for good performance, and using the byte array methods, while slightly more complicated, is standard practice and gives its own performance boost. I highly recommend combining the two.
By the way, Sun has a great I/O tutorial that will cover these topics much better than a single post can. As well, they include excellent sample code that you can adapt to your needs for copying from one stream to another.
Finally, if you are using JDK 1.4 or later and want to learn something really cool, check out New I/O (a.k.a. Merlin). Sun and IBM's DeveloperWorks again have great tutorials.
Let us know if you hit any snags or need further clarification, but I highly recommend the tutorials for quick learning. After going through them you'll be able to get much farther before needing help, and the questions you'll have will be more easily answered on this forum.
No, images are always messed up when sent over networks. Seriously, this should be fixable if you can describe how the image is messed up. Open the file in a text editor that has a hex mode and compare it to the bytes you are receiving on the server side.
Originally posted by Dan Lynch:
The image is messed up though. Is there anything I can do about this?
If there's no difference, post your latest server code, and let's see what we can find.
Note: Can you please fix your previous post that begins "David and Joe" to close the first CODE tag after the "output.close();" line so that the following paragraph wraps correctly. This page is getting a bit difficult to read. Thanks!
Ive got it working now. Sorry it took so long. Now that Im using the proper streams is there any reason why when I send large images 50KB + the socket resets. And Ive sent an image thats 9KB and it has lost over 300 bytes (9039-8704). Is there anything I can do about this?
Disclaimer: The above contains wild speculation based upon the behavior at hand and may not actually represent the inner workings of the JVM.
Taking out all the close statements sends the entire image. So Ill try some guard to stop the program hanging because of the lack of close statements.