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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:07:09+00:00 2026-05-13T06:07:09+00:00

I am trying to send my struct over a UDP socket. struct Packet {

  • 0

I am trying to send my struct over a UDP socket.

struct Packet {
int seqnum;
char data[BUFFERSIZE];
};

So on the sender I have

bytes = sizeof(packet);
char sending[bytes];
bzero(sending, bytes);
memcpy((void *) sending, (void *) &packet, sizeof(bytes));
bytes = sendto(sockfd, sending, sizeof(sending), 0,
    (struct sockaddr *) &client, clientSize);

So I’m hoping that copies my struct into the Char[].

On the receiver I have

int bytes;
bytes = sizeof(struct Packet);
char recv[bytes];
bytes = recvfrom(sockfd, recv, bytes, 0,
    (struct sockaddr *) &client, &clientSize);
memcpy((void *) currentpkt, (void *) recv, bytes);

However on the receiver with memcpy((void *) currentpkt, (void *) recv, bytes); I get an error:

error: cannot convert to a pointer type

What am I doing wrong? Is there a better way to send my struct over a UDP socket?

***** UPDATE *****

Thanks for the answers everyone. In the end I missed the ‘&’ but my code now looks like this.

Sender:

void udt_send(struct Packet packet) {
    int bytes;
    bytes = sendto(sockfd, (char *) &packet, sizeof(packet), 0,
            (struct sockaddr *) &client, clientSize);
}

Receiver:

bytes = recvfrom(sockfd, (char *) &currentpkt, bytes, 0,
        (struct sockaddr *) &client, &clientSize);

In C its nice that we can just cast it to a char and send the bytes over.

  • 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-13T06:07:09+00:00Added an answer on May 13, 2026 at 6:07 am

    currentpkt is of struct type; you need to get a pointer to the struct to get this to work:

    memcpy(&currentpkt, recv, bytes);
    

    For your second question, you have some other problems. What if you receive more bytes in a packet than sizeof(struct Packet)? As it’s written now, you’ll overrun your struct.

    What if the client and server applications are compiled using different compilers or settings, or on platforms with different endianness? In this case, the struct might be different sizes on the two platforms and may be laid out in memory differently.

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

Sidebar

Related Questions

I'm trying to convert a struct to a char array to send over the
I'm trying to send a single UDP packet with a specific payload and my
ok so I am trying to send a structure like so over MPI struct
I'm trying to send a network packet of data to a hardware device. I
I'm trying to send an array of hexadecimal values through an udp socket but
I'm trying to send and receive using pipes: send.cpp struct { long a; long
I am trying to send an array of this struct to a DBus Server:
Trying to send 3rd party scripts over an SSL or SPDY connection is a
Trying to send a complex type between two systems that have the same code
I`m trying to send simple html page with form data to user with GET,

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.