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

  • SEARCH
  • Home
  • 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 8484395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:22:10+00:00 2026-06-10T20:22:10+00:00

I am trying to read some data from a network socket using the following

  • 0

I am trying to read some data from a network socket using the following code –

Socket s = new Socket(address, 502);
response = new byte[1024];
InputStream is = s.getInputStream();
int count = is.read(response, 0, 100);

The amount of data isn’t large. It is 16 bytes in total. However the read() statement does not read all the data in one go. It reads only 8 bytes of data into my buffer.

I have to make multiple calls to read() like this in order to read the data –

Socket s = new Socket(address, 502);
response = new byte[1024];
InputStream is = s.getInputStream();
int count = is.read(response, 0, 100);
count += is.read(response, count, 100-count);

Why is this happening? Why does read() not read the entire stream in one go?

Please note that the data is not arriving gradually. If I wait for 2 seconds before reading the data by making a call to Thread.sleep(2000) the behavior remains the same.

  • 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-10T20:22:11+00:00Added an answer on June 10, 2026 at 8:22 pm

    I understand that it blocks until data arrives. “That in turn is because the data doesn’t necessarily arrive all in one go.” Why not is my question.

    The data doesn’t necessarily all arrive in one go because the network typically breaks it up into packets. IP is a packet switching protocol.

    Does TCP transmit it blocks of 8 bytes?

    Possibly, but probably not. The packet size depends on the network / networks that the data has traversed, but a typical internet packet size is around 1500 bytes.

    If you are getting 8 bytes at a time, either your data is either coming through a network with an unusually small packet size, or (more likely) the sender is sending the data 8 bytes at a time. The second explanation more or less jives with what your other comments say.

    And since I explicitly specify 100, a number much larger than the data in buffer shouldn’t it attempt to read up till atleast 100 bytes?

    Well no. It is not specified to work that way, and it doesn’t work that way. You need to write your code according to what the spec says.


    It is possible that this has something to do with the way the device is being “polled”. But without looking at the specs for the device (or even knowing what it is exactly) this is only a guess.

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

Sidebar

Related Questions

I'm trying to store some binary data obtained from read() in my buffer using
I'm trying to read some JSON data from the Tumblr API. I'm using the
I am trying to read some data from a python module from a web.
I'm trying to read in some sample data from an XML file in a
I'm trying to create a plugin which needs to read some data from a
I am trying to read some XML code from a website, and am having
I am trying to read some settings from php.ini using zend. The API that
I am trying to read some info from a text file by using windows
I am using NSFileHandle to read data from a socket. This is how I
I'm new to Python and I'm trying to read some values from an USB

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.