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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:18:58+00:00 2026-05-25T20:18:58+00:00

Writing an application which sends 4 ints over socket, trying the following but getting

  • 0

Writing an application which sends 4 ints over socket, trying the following but getting 0s at receiving end…

I assume this is something to do with the way I’m passing them signedness & endianness etc…

int _send(int sock, int c, int x, int y, int w)
{
    int cc, xc, yc, wc;
    char buf[16];
    int offset;
    struct sockaddr_in sap;
    char echoBuffer[1];     /* Buffer for echo string */
    int bytesRcvd, totalBytesRcvd;   /* Bytes read in single recv() and total bytes read */

    offset = 0;

    buf[offset] = htonl(c);
    buf[4] = htonl(x);
    buf[8] = htonl(y);
    buf[12] = htonl(w);

    if (send(sock, buf, 16, 0) != 16)
    {
        printf("send() sent a different number of bytes than expected");
        return(-1);
    }

        //...

}

And this is the code at the receiving end:

while (listen(sock, 2) == 0)
    {
        printf("listened...\r\n");


            int addrlen;
            struct sockaddr_in address;

            addrlen = sizeof(struct sockaddr_in);
            int channel = accept(sock, (struct sockaddr *)&address, &addrlen);

            if (channel<0)
            {
                perror("Accept connection");
                return -1;
            }

        else {
            printf("accepted\r\n");

            while (1)
            {

            int size = 16;
            char buffer[16];

            recv( channel, buffer, size, 0);

            for (int i=0; i<=12; i+=4)
            {
                int c = ntohl(buffer[i]);
                printf("%d\r\n", c);
            }
                 }
  • 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-25T20:18:58+00:00Added an answer on May 25, 2026 at 8:18 pm
    buf[offset] = htonl(c);
    buf[4] = htonl(x);
    buf[8] = htonl(y);
    buf[12] = htonl(w);
    

    this is broken. you should declare array of integers. Reason is that buf[i] = x meants put a byte at i-th cell, and if it doesn’t fit then truncate. That is what is happening.

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

Sidebar

Related Questions

I'm writing an application which is quite graphically heavy and therefore I'm trying to
I am writing an application which tests email addresses, for one part of this
I'm writing an application which sends a lot of emails throughout the app's lifecycle.
I'm writing a client application which runs on a users computer and sends various
I am writing an iOS application which will be sent notifications over APNS from
I'm writing an application which reads large arrays of floats and performs some simple
I am writing an application which blocks on input from two istreams . Reading
I'm writing an application which has to be configurable to connect to Oracle, SQL
I've been writing an application which will need to expand environment strings in a
I am writing an application which is going to allows users to change the

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.