0

I need some suggestion on how to push an end of stream character without having to have sc.shutdownOutput() call being made on the server side after finish sending a file over the socket channel.

Here is the bit from server side

        long curnset = 0l;
        long temp = 0l;
        int maxCount = 8192 * 1024;
        logger.debug(AUDIT, "begin sending {}", filenameSize);
        while (curnset < fsize)
        {
            temp = inChannel.transferTo(curnset, maxCount, sc);
            curnset += temp;
            logger.debug(AUDIT,"c {} | t {} | max {} | size {}",curnset,temp,maxCount,fsize);
        }
        logger.debug(AUDIT, "end sending {}", filenameSize);

Here is the client side:

        logger.debug(AUDIT, "begin receiving {}", filenameSize);
        while (curnset < this.fileSize)
        {
            logger.debug(AUDIT,"inside of while loop");
            temp = outChannel.transferFrom(sc, curnset, maxCount);
            curnset += temp;
            logger.debug(AUDIT, "c {} | t {} | max {} | size {}", curnset, temp,
                    maxCount, this.fileSize);
        }
        logger.debug(AUDIT, "end receiving {}", filenameSize);

Here is how the program behaves: Server would complete its portion of code meanwhile the client would hang right at logger.debug(AUDIT,"inside of while loop"); Only when I ctrl-c the server side, the client would eventually complete that portion of code - but with missing bytes - around few thousand bytes shy of fileSize.

I have establish the fileSize for client and server. The same portion of code I use for sending a single file from server to client but with the difference in sc.shutdownOutput() -- if i call that, I can't resend another file through the same socket channel. I would like not to shutdown the output of socketchannel.

Any other way out of this?

Thanks in advance.

EdisonCh
  • 13
  • 1
  • 7

1 Answers1

0

You're doing the right thing by calling transferTo() in a loop, but you need to decrement maxCount by the transfer count each time transferTo() returns.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • decrement maxCount ? such as maxCount -= temp ? Not quite sure if that would help since maxCount in this case is not changing in value = 8192 * 1024. transferTo and transferFrom would transfer from the position of curnset up to maxCount byte. If I keep decrement maxCount -= temp in loop, I would have no value in maxCount in hurry. maxCount < filesize. Do I understand your statement corrently? – EdisonCh May 28 '14 at 08:10