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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:25:27+00:00 2026-06-01T04:25:27+00:00

The code below is reading 4 bytes from a socket into an integer. I

  • 0

The code below is reading 4 bytes from a socket into an integer. I can see the bytes are (in decimal) 130 0 0 0. I suspect the code below will return version 130 but I don’t know why. Will this return 130? I attempted to replicate it in Java but I a very large negative number (more to what I would expect). How do I interpret pseudo/C code below?

#include <socket.h>
void readVersion(char *buf, int iCount) {
      recv(hSocket, buf, iCount, MSG_WAITALL);
}
int m_iVersion;
readVersion((char *) &m_iVersion, sizeof (m_iVersion))
count << m_iVersion;
  • 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-01T04:25:28+00:00Added an answer on June 1, 2026 at 4:25 am

    Ignoring the obvious error that you are calling return in a void function…

    The recv function, by definition, returns an integer telling you the number of bytes read (or negative if error). If you want to read the bytes and return them, you want to cout the buffer, not the # of bytes read.

    However, printing an int will print the entire thing.. not byte-by-byte like you’re expecting. What about something like this?

    int nb, i;
    union {
         uint32_t whole;
         char bytes[4];
    } v;
    nb = recv(hSocket, v.bytes, 4, MSG_WAITALL);
    if (nb != 4)
        printf("Error: recv returned: %d\n", nb);
    else
        printf("%d %d %d %d\n", v.bytes[0], v.bytes[1], v.bytes[2], v.bytes[3]);
    

    First, you should make sure you’re seeing “130 0 0 0” or “0 0 0 130” from the above example.

    Now, try adding:

    printf("%u\n", v.whole);
    

    And see if you get 130. If you do, great. If you instead get 2 181 038 080 or -33 554 432it means your bytes are in the wrong order (google ‘endianness’). You can fix this with a byte-reordering command:

    v.whole = ntohl(v.whole);
    printf("%u\n", v.whole);
    

    ntohl re-orders the bytes to make the endianness match your local computer.

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

Sidebar

Related Questions

My code for reading feed from a url is like below, var reader =
I got the code below from a website,and this way of serial port reading
Reading on this oauth tutorial . The code below, shows how this can be
When I was reading from a usocket stream using the code below: (let ((stream
Code below is used to save PostgreSql database backup from browser in Apache Mono
(can skip this part just an explanation of the code below. my problems are
I can see vast difference in performance between below two programs. import java.io.*; import
The code below is what I am using to send and receive information from
From the code below, I though thhat y would be printed out before x,
I have some code from the net reading hyperspectral data (image, so lots of

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.