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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:05:27+00:00 2026-06-07T00:05:27+00:00

I must send a struct between two machines via a udp socket the information

  • 0

I must send a struct between two machines via a udp socket
the information I need is to send a structure as follows:

unsigned long x;

unsigned long y;

char *buf;

I have to send the struct in a single time.

My problem is: How can I handle this structure to, for example, set in one variable that I can send through the socket, especially as the size of the variable buf is not fixed

Thank you for your 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-07T00:05:28+00:00Added an answer on June 7, 2026 at 12:05 am

    You will need to copy everything within your struct sequentially into a separate char buffer and then write that to the socket. Optionially, because your char* buffer inside your struct is not of fixed length it is often a good idea to calculate the size of what you want to send and also write this as an integer at the start of your message so that at the other end the length of the packet that you are sending can be verified by your receiving socket.

    When unpacking the data at the other end, simply start at the beginning of your receive buffer and memcpy data into your values

    char* message; // This is the pointer to the start of your
    // received message buffer

    // TODO: assign message to point at start of your received buffer.
    
    
    unsigned long xx;
    unsigned long yy;
    memcpy(&xx,message,sizeof(unsigned long));  // copy bytes of message to xx
    message += sizeof(unsigned long);           // move pointer to where yy SHOULD BE 
                                                // within your packet    
    memcpy(&yy,nessage,sizeof(unsigned long));  // copy bytes to yy
    message += sizeof(unsigned long);           // message now points to start of 
                                                // the string part of your message
    
    int iStringLength =   //  ?????? You need to calculate length of the string
    char tempBuffer[1000]; // create a temp buffer this is just for illustration
                           // as 1000 may not be large enough - depends on how long
                           // the string is
    memcpy(tempBuffer,message,iStringLength);
    

    Then xx,yy contain your long values and tempBuffer contains the string. If you want the string to persist beyond the current scope then you will need to allocate some memory and copy it there. You can calculate the size of this string from the size of the whole message minus the size of the 2 unsigned long items (or using an extra item sent in your packet IF you did as I suggested above).

    I hope this clarifies what you need to do

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

Sidebar

Related Questions

I have a need for a simple html + javascript page that must send
I want send e-mail with some images in content. I think I must attached
I'm trying to create a complex custom NSControl that must be able to send
This is my first steps with XML and I must send a XML by
After a fork call, i have one father that must send sigusr1 or sigusr2
I am working on a loginMiddleware class for Django. This middleware class must send
I must send copy of Form1 object to my method where I don't want
I trying to make a tell a friend script. The script must send an
I have a .NET app that must send data to a Zabbix server. How
Ok, here is my problem. I need to create a socket program that can

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.