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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:09:42+00:00 2026-06-14T22:09:42+00:00

I have a java program that will send(over TCP) packets to a c++ program.

  • 0

I have a java program that will send(over TCP) packets to a c++ program. Only two types of objects will be sent (struct A or struct B). I need to serialize the two objects and send them to the c++ program, such that when de-serialized inside the c++ program they will be of the type struct A or struct B, depending on what has been sent. How can I achieve this without any external java packages?

            struct A
            {
                unsigned int field1; 
                unsigned int field2; 
            }

            struct B
            {
                unsigned int length;
                struct A list[GLOBALLENGTH];
            }

GLOBALLENGTH is a static and globel int.

Please note that I dont need a general solution, as I will strictly be dealing with the mentioned types above. Also, I dont have access to the c++ program(it is a blackbox), both operate in little indian.

  • 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-14T22:09:45+00:00Added an answer on June 14, 2026 at 10:09 pm

    It sounds like the c++ program expects a raw dump of the data in each structure, with no padding, in little-endian order, with the fields in the declared order, using 32-bit integers. If all that’s right, here’s one way to write bytes for consumption by the c++ program:

    private static final int A_SIZE = 8;
    private static final int B_SIZE = 4 + GLOBALLENGTH * 8;
    
    public byte[] writeA(A a) {
        byte[] bytes = new byte[A_SIZE];
        writeA(a, bytes, 0);
        return bytes;
    }
    
    private void writeA(A a, byte[] bytes, int offset) {
        ByteBuffer buff = ByteBuffer.wrap(bytes, offset, A_SIZE);
        buff.order(ByteOrder.LITTLE_ENDIAN);
        buff.putInt(a.field1).putInt(a.field2);
    }
    
    public byte[] writeB(B b) {
        byte[] bytes = new byte[B_SIZE];
        ByteBuffer buff = ByteBuffer.wrap(bytes, 0, 4);
        buff.order(ByteOrder.LITTLE_ENDIAN);
        buff.putInt(b.length);
        int offset = 4;
        for (A a : b.list) {
            writeA(a, bytes, offset);
            offset += A_SIZE;
        }
        return bytes;
    }
    

    You can then call the appropriate public method (writeA(A) or writeB(B)) and send the bytes to the server.

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

Sidebar

Related Questions

I have a java program that will work with a variety of Java Beans.
I have developed a Java program that will count the number of files in
I am trying to write a java program that will allow me to send
I have a constantly-running Java program that needs to send an email whenever it
I have to code a Java program that will receive messages from network and
I am making a program that will have the ability to run the java
I have a Java Program that will import data from a DB2 table to
I have a java program that performs 5 different tasks. When I run the
I have a java program that is running in the background(Windows). I would like
I have a Java program that runs many small simulations. It runs a genetic

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.