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

The Archive Base Latest Questions

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

I send a struct with one field (unsigned int) to a network emulator and

  • 0

I send a struct with one field (unsigned int) to a network emulator and it outputs the unsigned int but it is incorrect.

If I do NOT re-run the network emulator, if I resend the struct with the same number, it will be the same random integer. If I send that number +1, I get the random integer -1.

If i do re-run the network emulator and resend the struct with the same number, it shows a different random integer.

This is the stuct:

struct pkt_INIT {
  unsigned int router_id;
};

This is the code to send that struct:

  int sock;
  struct sockaddr_in server_addr;
  struct hostent *host;
  char send_data[1024];

  host= (struct hostent *) gethostbyname(emulator_host);

  if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  {
    perror("socket");
    exit(1);
  }

  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(emulator_port);
  server_addr.sin_addr = *((struct in_addr *)host->h_addr);
  bzero(&(server_addr.sin_zero),8);

  struct pkt_INIT init;
  init.router_id = router_id;
  strcpy(send_data,(char *)&init);

  sendto(sock, send_data, strlen(send_data), 0,
         (struct sockaddr *)&server_addr, sizeof(struct sockaddr));

I don’t have any of the code for the network emulator.

  • 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:38:49+00:00Added an answer on June 1, 2026 at 4:38 am

    Don’t use strcpy() to put a binary struct into a binary array. Likewise, do not use strlen() to get the length of a struct. Both functions are designed for strings, and will copy/read bytes until they encounter a null byte. Use memcpy() and sizeof() instead:

    memcpy(send_data, &init, sizeof(init)); 
    sendto(sock, send_data, sizeof(init), 0, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)); 
    

    In which case, just get rid of the array and pass the struct directly to sendto() instead:

    sendto(sock, (char*)&init, sizeof(init), 0, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)); 
    

    Now, with that said, there is an issue with the router_id. Most network protocols require network byte ordering, so you may or may not need to use htonl(), depending on what the emulator is actually expecting:

    init.router_id = htonl(router_id); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to send a struct from my client program to a server
I'm trying to send and receive using pipes: send.cpp struct { long a; long
I send binary data over websocket from python server: data = struct.pack('!BI', 2, 1)
I am trying to send an array of this struct to a DBus Server:
I have defined a custom struct which I need to send over to another
I am trying to send audio file from one computer to other using socket
I want to send a message with socket->ops->sendmsg() in kernel module. One of the
I need to send a C struct over the wire (using UDP sockets, and
I'm trying to get a simple send and receive UDP program working, but I'm
Does a set of functions doing exactly one word but differing in the name

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.