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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:20:07+00:00 2026-06-15T04:20:07+00:00

sorry for my bad English. The Context : I have 6 variables unsigned char

  • 0

sorry for my bad English.

The Context :

I have 6 variables

  1. unsigned char
  2. char
  3. unsigned short
  4. int
  5. int
  6. int

I serialize this data for prepare the send to socket.

The problem, how can i store my serialized data and send it ?

My first solution, use an structure for send my data but this solution requires a cast and the cast action is very slow.

Have you better solution than store my variable in structure ?

#include <sstream>
#include <string.h>

typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;

#define PORT 4241
template<typename T>
inline std::ostream& raw_write(std::ostream &os, const T &t)
{
  return os.write( reinterpret_cast<const char*>(&t), sizeof t);
}

int main()
{
  char protocole;
  char id_module;
  unsigned short id_message;
  int id_client;
  int size_msg;
  unsigned long timestamp;

  SOCKET csock;
  SOCKADDR_IN csin;

  protocole = 1;
  id_module = 2;
  id_message = 256;
  id_client = 8569;
  size_msg = 145;
  timestamp = 1353793384;
  csock = socket(AF_INET, SOCK_STREAM, 0);
  csin.sin_addr.s_addr = inet_addr("127.0.0.1");
  csin.sin_family = AF_INET;
  csin.sin_port = htons(PORT);
  connect(csock, (SOCKADDR*)&csin, sizeof(csin));
  raw_write(ss, protocole);
  raw_write(ss, id_module);
  raw_write(ss, id_message);
  raw_write(ss, id_client);
  send(csock, ss.str().c_str(), strlen(ss.str().c_str()), 0);
  close(csock);

}

Doesn’t work because string contain ‘\0’ and send cut it.

I said, cast is slow on many data send. At the receive i need to cast again for recovery my data.

This action is more slow than just bytes reading.

  • 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-15T04:20:09+00:00Added an answer on June 15, 2026 at 4:20 am

    I f you dont want to do it from scratch, you could use Google Protocol Buffers. Its a strong library letting you define a protocol, which is nothing else but a class, that can (de)serialize itself to(from) a stream, file, buffer or string.

    I use it myself, its nice to serialze it to a string which is easy to send via socket.

    EDIT: Reply to comment:

    Okay then notice that its not send (...) truncating your stream. Its c_str() which ends on a '\0'. Maybe you should try to avoid streams and use a container which offers access to its raw data like std::string or std::vector with const void * data(). You could pass this and size() to send(...).

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

Sidebar

Related Questions

Sorry for my bad English. Just like what I asked. I have this class
Sorry for bad English :( Suppose i can preliminary organize recipes and ingredients data
Sorry for bad english and bad title! I have the table post id title
At first sorry my bad English. I want define routing for this url: For
First, sorry for my bad english, I'm French. Here the problem : I have
Sorry for my bad english i'm working to rtsp communicate program this RTSP server
First of all, sorry for my bad English... I have a problem with one
Hey, sorry for my bad english... Using EF4 code-only, I have created some POCO
At first sorry for bad english, Im deafinitely not specialized into math-english. I have
at first sorry my bad English I want load model in my plugin controller

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.