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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:37:31+00:00 2026-05-23T11:37:31+00:00

I wrote a server using c++ and the code looks like this char *sendBuffer

  • 0

I wrote a server using c++ and the code looks like this

char *sendBuffer = new char[4+4+1+8+48];
m_magicNumberBuffer[0] = 0xA9;
m_magicNumberBuffer[1] = 0xA9;
m_magicNumberBuffer[3] = 0xA9;
m_magicNumberBuffer[4] = 0xA9;
int m_dataLength = 4+4+1+8+48;
unsigned char m_type = 1;
long long m_deviceId = 12123122123;
char m_msg = new char[48];
m_msg = "NO ERROR";

/*
 * Magic number;
 */
memcpy(sendBuffer, m_magicNumberBuffer + offset, 4);
offset += 4;

/*
 * Data length
 */
memcpy(sendBuffer, &m_dataLength + offset, 4);
offset += 4;

/*
 * Packet type
 */
memcpy(sendBuffer, &m_type + offset, 1);
offset += 1;

/*
 * Device ID
 */

memcpy(sendBuffer, &m_deviceId + offset, 8);
offset += 8;

memcpy(sendBuffer, m_msg + offset, 48);
offset += 48;

I am writing the sendBuffer

 write(client->getFd(), sendBuffer, MAX_LENGTH);

Client is written in Java

I am doing something like this

        InputStream in = mSocket.getInputStream();
        DataInputStream dis = new DataInputStream(in);

        int magicNumber = dis.readInt();
        int length = dis.readInt();
        byte[] data = new byte[length];
        if(length > 0){
            dis.readFully(data);
        }

And it reads something wrong. I am getting unreasonable number for the magic number and always reads 0 for the data length.

How do I fix this ? Thanks in advance..

  • 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-05-23T11:37:31+00:00Added an answer on May 23, 2026 at 11:37 am

    Pretty simple, really: you want to add your offset to the destination sendBuffer, not your sources. E.g. instead of

    memcpy(sendBuffer, &m_deviceId + offset, 8);
    

    you want

    memcpy(sendBuffer + offset, &m_deviceId, 8);
    

    As it is, you are storing various random data to the first few bytes of sendBuffer, and nothing to the rest of it.

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

Sidebar

Related Questions

I have problem installing simple Windows Service, my code looks like this: using System;
We have some html that looks like this: <form id=MyUserControl runat=server method=post> <script language=javascript
I wrote an application server (using python & twisted) and I want to start
I wrote a server-client rmi application and I'm using MacOsX and Ubuntu laptops to
Im using this code http://platinumdogs.wordpress.com/2008/10/30/net-c-impersonation-with-network-credentials/#comment-770 to connect from a machine in the domain to
I have this function to make a directory on a remote server using ftp,
We are using Emacs on Sun OS 5.9. I wrote the exact same code
I start to write a client - server application using .net (C#) for both
I need to write a web application using SQL Server 2005, asp.net, and ado.net.
I am using the AdoNetAppender to write to a SQL Server 2005 database table.

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.