Java socket multiple file transfer over single stream -


I have written a client-server file transfer program. I am trying to achieve the following work flow:


Connect to server -> open stream -> authenticate -> message ('send file') -> message ([file name]) -> message ([file size]) -> Send a message -> Message ('Send File') ... Message ('Disconnect')


The goal is to connect and authenticate only once the same data stream Send multiple files.

I have revised the method of copying a stream to ensure that the copy does not copy too much data from incoming and outgoing streams.

For example, send a file to a client server:

Server: copy (dataInputStream, fileOutPutStream, length)

For example, this copy method server And used to send and receive clients.

  Fixed zero copy (InputStream, I answer the question: output stream out, long length) throws IOException {byte [] buf; If (length & lt; 8192) {buf = new byte [(int) length]; } Buf = new byte [8192]; Int len ​​= 0; Long reading = 0; While (length> read & amp; amp; (len = in.read (buf))> 1) {read + = len; Out.written (buff, 0, lane); If (read length & lt; 8192) {buf = new byte [(long-read)]; }}}  

  while (length> read & amp; ( Len = in.read (buf)) gt; -1) {read + = len; Out.written (buff, 0, lane); If (read length & lt; 8192) {buf = new byte [(long-read)]; }}  

The easiest way to do this is as follows:

  while (length> read & amp; (len = in.read (buff, 0, Math.Man (buff.length, length-read)))> {read + = len; Out.written (buff, 0, lane); }}  

E & amp; Oi

In this way, the buffer can be of any size which you may prefer more than zero, its size at many points without entering the code.


Comments