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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:04:57+00:00 2026-05-26T14:04:57+00:00

I have a small client server application in which i wish to send an

  • 0

I have a small client server application in which i wish to send an entire structure over a TCP socket in C not C++. Assume the struct to be the following:

    struct something{
int a;
char b[64];
float c;
}

I have found many posts saying that i need to use pragma pack or to serialize the data before sending and recieveing.

My question is, is it enough to use JUST pragma pack or just serialzation ? Or do i need to use both?

Also since serialzation is processor intensive process this makes your performance fall drastically, so what is the best way to serialize a struct WITHOUT using an external library(i would love a sample code/algo)?

  • 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-26T14:04:58+00:00Added an answer on May 26, 2026 at 2:04 pm

    You need the following to portably send struct’s over the network:

    • Pack the structure. For gcc and compatible compilers, do this with __attribute__((packed)).

    • Do not use any members other than unsigned integers of fixed size, other packed structures satisfying these requirements, or arrays of any of the former. Signed integers are OK too, unless your machine doesn’t use a two’s complement representation.

    • Decide whether your protocol will use little- or big-endian encoding of integers. Make conversions when reading and writing those integers.

    • Also, do not take pointers of members of a packed structure, except to those with size 1 or other nested packed structures. See this answer.

    A simple example of encoding and decoding follows. It assumes that the byte order conversion functions hton8(), ntoh8(), hton32(), and ntoh32() are available (the former two are a no-op, but there for consistency).

    #include <stdint.h>
    #include <inttypes.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    // get byte order conversion functions
    #include "byteorder.h"
    
    struct packet {
        uint8_t x;
        uint32_t y;
    } __attribute__((packed));
    
    static void decode_packet (uint8_t *recv_data, size_t recv_len)
    {
        // check size
        if (recv_len < sizeof(struct packet)) {
            fprintf(stderr, "received too little!");
            return;
        }
    
        // make pointer
        struct packet *recv_packet = (struct packet *)recv_data;
    
        // fix byte order
        uint8_t x = ntoh8(recv_packet->x);
        uint32_t y = ntoh32(recv_packet->y);
    
        printf("Decoded: x=%"PRIu8" y=%"PRIu32"\n", x, y);
    }
    
    int main (int argc, char *argv[])
    {
        // build packet
        struct packet p;
        p.x = hton8(17);
        p.y = hton32(2924);
    
        // send packet over link....
        // on the other end, get some data (recv_data, recv_len) to decode:
        uint8_t *recv_data = (uint8_t *)&p;
        size_t recv_len = sizeof(p);
    
        // now decode
        decode_packet(recv_data, recv_len);
    
        return 0;
    }
    

    As far as byte order conversion functions are concerned, your system’s htons()/ntohs() and htonl()/ntohl() can be used, for 16- and 32-bit integers, respectively, to convert to/from big-endian. However, I’m not aware of any standard function for 64-bit integers, or to convert to/from little endian. You can use my byte order conversion functions; if you do so, you have to tell it your machine’s byte order by defining BADVPN_LITTLE_ENDIAN or BADVPN_BIG_ENDIAN.

    As far as signed integers are concerned, the conversion functions can be implemented safely in the same way as the ones I wrote and linked (swapping bytes directly); just change unsigned to signed.

    UPDATE: if you want an efficient binary protocol, but don’t like fiddling with the bytes, you can try something like Protocol Buffers (C implementation). This allows you to describe the format of your messages in separate files, and generates source code that you use to encode and decode messages of the format you specify. I also implemented something similar myself, but greatly simplified; see my BProto generator and some examples (look in .bproto files, and addr.h for usage example).

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

Sidebar

Related Questions

I have written a small client server socket application. It is a proof of
I have a client and a server application which communicate over .NET 2.0 Remoting
I've got a small client/server test application where I have a Flex app that
I have a small linux vps. I have written a Java client application, which
We have developed a small web application for a client. We decided on the
I wish to develop a client-server application in .NET that functions as follows: Clients
I've written a small web application which is basically a JQuery powered chat client
I need to have as part of a desktop application a file server which
I have a small web application developed using Icefaces 1.8.2 and jsf 1.1 which
I have to create a small .NET client application that will be using Exchange

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.