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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:37:40+00:00 2026-05-12T00:37:40+00:00

I have to transfer some values to be used as commands over the network,

  • 0

I have to transfer some values to be used as commands over the network, and I wish to make it as efficient and robust as possible, i.e. I need opinions on what to use for these commands, #defines or enums?

The range of commands shall not be more than 20 commands(lets say 40 with every defined response to the command), so according to most things I’ve heard, it would fit well in the char limit.

So far, I’m assuming that the best approach would be to use an enum.

  • 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-12T00:37:40+00:00Added an answer on May 12, 2026 at 12:37 am

    When transferring numeric values across the internet, you have 2 things to bear in mind.

    The first is the endianess, the order that bytes (or sometimes bits) appear in. Suppose you have the 32-bit value 0xAABBCCDD. Intel CPUs are little-endian machines, which mean those bytes would be stored as { 0xDD, 0xCC, 0xBB, 0xAA }. In other words, the least significant byte is stored at the lowest address. In a big-endian machine, the bytes would be stored as { 0xAA, 0xBB, 0xCC, 0xDD }, with the least significant byte at the highest address.

    When transferring multi-byte integers between 2 machines, you have to ensure that they both interpret each others data correctly, even if they have different byte orderings. Thankfully, there is a standard called Network byte order, which is big-endian, and there are 4 useful functions for converting between host-order and network-order:

    ntohl (Network to Host, long)
    ntohs (Network to host, short)
    htonl (Host to network, long)
    htons (Host to network, short)

    The long versions work with 32-bit integers and the short versions with 16-bit integers. As long as you always call *hton** before transmitting data across the network and call *ntoh** when reading from the network, data will be in the correct byte order.

    Of course, the easiest way to get around this problem, especially since you have only 20 commands, is to just use single bytes, or chars.

    The second problem you have to deal with is encoding. How are signed integers represented? Using a sign-bit? Twos complement? Again, you run in to problems when different platforms on the network use different representations. If you stick to unsigned types, you shouldn’t really have a problem.

    What you use to represent your commands in your program is entirely up to you. Just make sure that you define your protocol well and adhere to that when transmitting and reading data.

    For example:

    enum command_t { one, two, three }
    
    void send_command(command_t c) {
        send((unsigned char)c);
    }
    
    command_t read_command() {
        return (command_t)recv();
    }
    
    void send(unsigned char c) { ... }
    unsigned char recv() { ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some rows in a table and need to transfer them to another
I have several csv files that I need to transfer over to mdb format.
I have a class that uses XStream and is used as a transfer format
I have used JSON::Any in my program to transfer the hash between client and
So I have some neural network simulator code that works correctly on the CPU,
I've some code I use to transfer a table1 values to another table2 ,
I have to transfer objects between activities. Objects are with complex structure. I'm not
I have a file transfer program. The program (Client) does following operations to send
I have this method to transfer files using a FTP Server: private void TransferNeededFiles(IEnumerable<string>
I have implemented a file transfer rate calculator to display kB/sec for an upload

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.