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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:56:51+00:00 2026-05-27T03:56:51+00:00

I have an issue when my client sends struct data to my server. My

  • 0

I have an issue when my client sends struct data to my server.
My client uses Qt tcp and my server uses boost.asio. On my server side, I can receive the buffer data sent by the client, but when I cast the data to my struct data, I get a struct data unreadable.

This is the struct data in question :

struct Protocole
{
  int type;
  char infos[1024];
}

this is the code in my server to read data on the client socket :

    this->_socket.async_read_some(boost::asio::buffer(_buffer), // _buffer is type of char[1024];
    _strand.wrap(boost::bind(&ClientManager::HandleRead, 
    this, 
    boost::asio::placeholders::error, 
    boost::asio::placeholders::bytes_transferred))
    );

in the ClientManager::HandleRead :

ProtocoleCS *_proto; // this is the struct data i have to cast 

_proto = static_cast<ProtocoleCS*>(static_cast<void*>(&_buffer));
// I can read _proto

This is the code in my Client to send the struct data :

void                Network::SendMsgToServer()
{   
    QByteArray      block;
    QDataStream     out(&block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_7);
    Protocole       proto;

    proto.type = 1;

    std::cout << " i am sending a message" << std::endl;

    proto._infos[0] = 'H';
    proto._infos[1] = 'E';
    proto._infos[2] = 'L';
    proto._infos[3] = 'L';
    proto._infos[4] = 'O';
    proto._id[5] = '\0';

    out <<  static_cast<char*>(static_cast<void*>(&proto));
    this->socket->write(block);
}
  • 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-27T03:56:52+00:00Added an answer on May 27, 2026 at 3:56 am

    QDataStream operator << is used for serialization, and not to write raw data as is.

    For example byte sequences are sent with a 32-bits “header” indicating the size of the sequence.

    And because you are casting the whole structure to char*, it interprets it as a string and stops at the first '\0' character which is in the int part of the struct.

    So you should rather write the two members separately and avoid explicit casting:

    // If you want to avoid endianness swapping on boost asio side
    // and if both the server and the client use the same endianness
    out.setByteOrder(QDataStream::ByteOrder(QSysInfo::ByteOrder));
    
    out << proto.type; 
    out.writeRawData(proto.infos, sizeof(proto.infos));   
    

    On boost asio side, since you know the size of the struct, you should use async_read instead of async_read_some because the latter might return before the whole structure has been received.

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

Sidebar

Related Questions

I currently have a client / server setup where the client sends data to
I'm new to Boost. I'm developing a little server. I have an issue when
I have an asp.net MVC server on one side and a silverlight 4 client
I have client/server data passing all working correctly. Text, Images, etc. My users create
I have an XP client that is experiencing an issue. My dev box is
I have a client server application writter in C# .NET using Sockets. I often
I'm using the SocketServer module for a TCP server. I'm experiencing some issue here
I have a form that sends out a notification when a client fills out
I have the following Java socket client app, that sends same string to socket
Hi guys i have an issue currently when using a Socket to send data.

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.