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 8971057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:51:46+00:00 2026-06-15T17:51:46+00:00

In my program, pc sends data to my android phone continuously. Each time pc

  • 0

In my program, pc sends data to my android phone continuously. Each time pc send 62 bytes(24 for head and 38 for content). My program runs well. But sometimes my phone can read 24 bytes for head and only 10 bytes for content.

I receive data via a non-block socketchannel.

private SocketChannel client = null;

    public int read(byte[] data,int offset, int len){
    try {
        ByteBuffer buffer = ByteBuffer.allocate(len);
        int read_len = client.read(buffer);
        if(read_len == 0){
        }else if(read_len == -1){
        }else{
            buffer.flip();
            buffer.get(data, offset, read_len);
        }
        return read_len;

    } catch (IOException e) {
        e.printStackTrace();
    }
    return -2;
}

How can I solve this problem?

  • 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-15T17:51:47+00:00Added an answer on June 15, 2026 at 5:51 pm

    But sometimes my phone can read 24 bytes for head and only 10 bytes for content.

    It’s possible there is a network problem, but I think it’s more likely that since TCP is a stream oriented protocol, which does not preserve message boundaries, you are occasionally getting payloads that do not correspond with the bytes sent by the pc. This is OK and expected. It just means you have to do a bit more work.

    I assume that with your read message you want to keep reading data until you have enough data to fill the supplied byte array. Try something this:

       public int read(byte[] data, int offset, int len)
       {
          try
          {
             ByteBuffer buffer = ByteBuffer.allocate(len - offset);
             while (buffer.hasRemaining())
             {
                int read_len = client.read(buffer);
                if (read_len == -1)
                   return -1;
             }
             buffer.flip();
             int result = buffer.remaining();
             buffer.get(data, offset, buffer.remaining());
             return result;
          }
          catch (IOException e)
          {
             e.printStackTrace();
          }
          return -2;
       }
    

    The nice thing about reading into a byte buffer is that it takes care of concatenating the bytes correctly on multiple reads, so just loop until full.

    BTW – don’t forget to subtract the offset (in case it is non-zero) from the length of your buffer.

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

Sidebar

Related Questions

I'm creating a program on my Android phone to send the output of the
I'm using the program to send data from database to the Excel file .
Is it possible to send data to another C++ program, without being able to
I have a Client program by tcp connection that send data to a server.
I have a Flash program that needs to make url requests to send data
In my program, I stat the files they want and send the data over.
I've got a program that sends text to stdout. But I only want to
I want to be a able to securely send data from my Android App
I am trying to send data from a C#.NET (Windows Application) program to a
I am using URLConnection Object to send data from my android client to server.

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.