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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:32:43+00:00 2026-06-04T21:32:43+00:00

I have been looking for a way to have an integer sent over a

  • 0

I have been looking for a way to have an integer sent over a Windows Socket using Winsock2. I have looked at most, if not all, of the questions people have asked on stackoverflow already.

This is what I have for the client, that sends the integer:

struct struct_var
{
    int Packet;
    int Number;
};

struct_var *arraystruct;

arraystruct = (struct_var *) malloc(sizeof(struct_var));
(*arraystruct).Packet = 100;
(*arraystruct).Number = 150;
int bytes = send(client,(char*)arraystruct,sizeof(*arraystruct),0);`

I have also tried to send using:

int int_data = 4;
int bytes = send(server, (char*) &int_data, sizeof(int), 0);`

This was recommended on another stackoverflow question
This is the receiving side, which was also recommended:

int int_data;
int bytes = recv(server, (char*) &int_data, sizeof(int), 0);
cout << int_data;`

When I run these the output I get from command line is: -858993460

Does anyone know why this is happening?

I would also like it to have the correct byte order as this will be sent over multiple kinds of computers.
Thanks in advance who can help me out

Full Server Code:

int main() {
WSADATA wsaData;
WORD version;
int error;

version = MAKEWORD(2, 0);

error = WSAStartup(version, &wsaData);

if ( error != 0 )
{
    return FALSE;
}

if ( LOBYTE( wsaData.wVersion ) != 2 ||
     HIBYTE( wsaData.wVersion ) != 0 )
{
    WSACleanup();
    return FALSE;
}


SOCKET server;

server = socket(AF_INET, SOCK_STREAM, 0);

sockaddr_in sin;

sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(5555);

if (bind( server, (SOCKADDR*)&sin, sizeof(sin) ) == SOCKET_ERROR ){
    DWORD ec=WSAGetLastError();
    cerr << hex << "bind ERROR" << ec << endl;
    return FALSE;
}

if ( listen( server, SOMAXCONN ) == SOCKET_ERROR ) {
    DWORD ec=WSAGetLastError();
    cerr << hex << "listen ERROR" << ec << endl;
    return FALSE;
}

SOCKET client;
int length;

    while(1) {

    if ( listen( server, SOMAXCONN ) == SOCKET_ERROR ) {
    DWORD ec=WSAGetLastError();
    cerr << hex << "listen ERROR" << ec << endl;
    return FALSE;
    }

    length = sizeof sin;
    client = accept( server, (SOCKADDR*)&sin, &length );
    cout << "Client connected" << endl;
    cout << "Sending Instructions..." << endl;

    int int_data;
    int bytes;

    bytes = recv(client, (char*) &int_data, sizeof(int), 0);

    cout << int_data << endl;
}

}

}

Full Client Code:

int main() {

WSADATA wsaData;
WORD version;
int error;

version = MAKEWORD(2, 0);

error = WSAStartup(version, &wsaData);

if ( error != 0 )
{
    return FALSE;
}

if ( LOBYTE( wsaData.wVersion ) != 2 ||
     HIBYTE( wsaData.wVersion ) != 0 )
{
    WSACleanup();
    return FALSE;
}

SOCKET client;

client = socket( AF_INET, SOCK_STREAM, 0 );

sockaddr_in sin;

memset( &sin, 0, sizeof sin );

sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr("127.0.0.1");
sin.sin_port = htons(5555);

if ( connect(client, (SOCKADDR*)(&sin), sizeof sin ) == SOCKET_ERROR ){
    return FALSE;
}

int int_data = 4;
    int bytes = send(client, (char*) &int_data, sizeof(int), 0);

}

  • 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-04T21:32:45+00:00Added an answer on June 4, 2026 at 9:32 pm

    Here’s the problem:

    int bytes = recv(server, (char*) &int_data, sizeof(int), 0);
    

    Should be:

    int bytes = recv(client, (char*) &int_data, sizeof(int), MSG_WAITALL);
    

    A few other nitpicky things, that aren’t causing your problem, but you should know:

    1. Don’t bind your server socket to 10.0.0.5. Bind to 0 (INADDR_ANY). That way, your code still works when you run it on machine who’s IP addrss is 10.0.0.6.

    2. Use htonl(), htons(), ntohs(), and ntohl() for interoperating with big-endian boxes.

    3. Always check the return values from socket calls. If recv returns 0 or -1, it means the remote side has disconnected. (0 technically means the client has stopped sending, but unless you are doing shutdown half-close stuff, it means he disconnected).

    4. TCP sockets are not message oriented, they are stream oriented. That is, just because you tell recv() to get N bytes, doesn’t mean it will return N bytes. IP fragmentation, TCP segmentations, and other factors can make recv() return partial data. Use the MSG_WAITALL flag as appropriate.

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

Sidebar

Related Questions

I have been looking for a way to remove an attachment from Jira using
I have been looking for a way to show the iPhone screen (not the
I have been looking for a way using the MPMediaItem's or the MPMediaLibrary to
I have been looking all over the web for the simplest solution for this,
I have been looking for a way to dynamically load functions into c++ for
I have been looking for a way to make form validation as easy and
I have been looking for a way to create an animated arrow which points
I have been looking for a way to get rid of the nasty black
I have been looking for a way to allow users to manually override geolocation
I have been looking around for a way to launch the VPN settings activity

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.