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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:42:02+00:00 2026-06-17T18:42:02+00:00

I try to send a structure from a client to a server. I read

  • 0

I try to send a structure from a client to a server. I read in the internet that i have to type cast the structure to a char array, but when i try this the char has not content.

Here is my struct:

struct packet
{
    int pa_ID;
    char message[MESSAGESIZE];
};

And here I try to send the struct:

int sendFile(FILE *file, SOCKET sock)
{
int amountToSend, amountSent;
int i = 0;
char buffer[BUFFERSIZE], serializedPacket[BUFFERSIZE];

while (!feof(file)) {
    struct packet p;
    fgets(p.message, MESSAGESIZE, file);
    p.pa_ID = i;

    if (p.message[strlen(p.message) - 1] == '\n')
        p.message[strlen(p.message) - 1] = '\0';

    amountToSend = sprintf_s(serializedPacket, sizeof(buffer), (char*)&p);
    amountSent = send(sock, serializedPacket, amountToSend, 0);
    if (amountSent == SOCKET_ERROR) {
        fprintf(stderr, "send() failed with error %d\n", WSAGetLastError());
        getchar();
        WSACleanup();
        return -1;
    }
    printf("Send %d bytes (out of %d bytes) of data: [%.*s]\n", amountSent, amountToSend, amountToSend, serializedPacket);
    memset(buffer, 0, sizeof(buffer));
    memset(p.message, 0, sizeof(p.message));
    memset(&p, 0, sizeof(p));
    i++;
}

fclose(file);
return 0;

}

The reading from the file functions without errors, but the serializedPack is just empty and I really dont see why.
Hope someone can help.

  • 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-17T18:42:03+00:00Added an answer on June 17, 2026 at 6:42 pm

    This:

    amountToSend = sprintf_s(serializedPacket, sizeof(buffer), (char*)&p);
    

    doesn’t make any sense at all.

    The third argument to sprintf_s() is a formatting string, but you pass a pointer to a structure, re-cast as a character pointer. This makes absolutely no sense.

    Representing your structure as a textual string (serializing to string) might be a good idea, but then you need a proper formatting string:

    amountToSend = sprintf_s(serializedPacket, sizeof(buffer), "%d %s", p.pa_ID, p.message);
    

    Depending on how you send this, you might also want to include the 0-terminator for the string in the transmitted data.

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

Sidebar

Related Questions

I try to send an email, but I have a problem, however, I found
I try to send a request to my server via GET, but qooxdoo sends
i try to send some data to my php file from ajax , but
I try to send json array to ajax (from PHP to jQuery) in my
I have server which send message into MQ. Message remove from queue when worker
i try to send data to my php scripts but i think i miss
i try to send my datas to php with ajax but there's strange mistake.
I try to send a sms using next code, but recipient gets my message
I try to send a message from to global page to my injected.js on
How do I convert a structure to a byte array in C#? I have

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.