Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7973757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:11:47+00:00 2026-06-04T08:11:47+00:00

I have to transfer ~100MB of data over ServerSocket using NIO, but I can’t

  • 0

I have to transfer ~100MB of data over ServerSocket using NIO, but I can’t figure out how to do this without transfer breaking at any place / keeping the state of transfer.

My first idea was to send the size of file, apparently I can’t send size of that big files because it wont even fit on RAM at once. Then I thought, why not just transfer til nothing is received, but thats when problem comes in.

Even if I am writing server-sided data all the time

        FileChannel fc = new FileInputStream(f).getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        while(fc.read(buffer) > 0) {
            buffer.flip();
            while(channel.write(buffer) > 0);
            buffer.clear();
        }

but because there have to be breaks in file transfer some time reading the data constantly and breaking when nothing is available was bad idea.

I can’t figure out how could I possibly tell the client if theres still data available without having to send each slice of data as new packet with opcode etc., or is it even possible?

I am also wondering if theres better way to send whole buffer than below

while(channel.write(buffer) > 0);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:11:48+00:00Added an answer on June 4, 2026 at 8:11 am

    The correct way to copy channels via buffers is as follows:

    while (in.read(buffer) >= 0 || buffer.position() > 0)
    {
      buffer.flip();
      out.write(buffer);
      buffer.compact();
    }
    

    This takes care of all the corner cases including read length != write length and having data left over at the end of the input.

    NB that is for blocking mode. If you are in non-blocking mode you should return to the select() loop if read() or write() returns zero.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this method to transfer files using a FTP Server: private void TransferNeededFiles(IEnumerable<string>
I have to transfer some values to be used as commands over the network,
i have a task to transfer text files from one pc to another using
I have to transfer large files between computers on via unreliable connections using WCF.
some how i manage to have jquery transfer effect working with jquery dialog but
Given this scenario where you have transfer objects (POJO's with just getters/setters) which are
I have 2 data transfer sql statements being run and sent to the same
I have a simple file transfer socket program where one socket sends file data
I have to transfer mysql data from my old server to new server. The
I am leaning Java. I have to transfer a Hashmap to Server using rpc.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.