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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:21:27+00:00 2026-06-15T01:21:27+00:00

I am trying to build a packet that will be sent via UDP. However

  • 0

I am trying to build a packet that will be sent via UDP. However I am not getting the correct data on the receiving side.

In the packet I want to include an IP Header, UDP Header, and the data that needs to be sent. In this case I just want to send the word “Hello” along with random header information.

char *data = "Hello";
char *packet = (char *)malloc(sizeof(struct iphdr) + sizeof(struct udphdr) + strlen(data));
struct iphdr *ip = (struct iphdr*) packet;
struct udphdr *udp = (struct udphdr*) (packet + sizeof(struct iphdr));
char *send_buff = (char *) (packet + sizeof(struct iphdr) + sizeof(struct udphdr));
ip->saddr = inet_addr("1.2.3.4");
ip->daddr = inet_addr("5.6.7.8");
ip->ttl = 5;
udp->source = 5950;
udp->dest = 5950;
udp->len = sizeof(struct udphdr);
udp->check = 0;
strcpy(send_buff, data);

sendto(sock, packet, (sizeof(struct iphdr) + sizeof(struct udphdr) + strlen(data)), ROUTER_IP);

The problem I’m having is that the receiving end just gets random data so I’m assuming the number of bytes is incorrect somewhere.

On the receiving side I have it print out one of the fields of the IP header as a test, but it’s not correct.

char recv_buff[1000];
int recv_bytes = recvfrom(sock, recv_buff, sizeof(recv_buff));
struct iphdr *ip = (struct iphdr*) recv_buff;
cout << static_cast<int16_t>(ip->ttl) << endl;

Am I putting the packet together wrong or is there a problem on the receiving end?
I used this example http://www.winlab.rutgers.edu/~zhibinwu/html/c_prog.htm as a reference for putting together the packet.

  • 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-15T01:21:29+00:00Added an answer on June 15, 2026 at 1:21 am

    You are creating the socket as socket(AF_INET, SOCK_DGRAM, 0); meaning that it’s a datagram (=UDP, typically) socket, so the network stack will automatically include IP header & UDP headers, etc.

    But since you are trying to create your own IP and UDP headers you must create a raw socket, then send the packet (and also calculate the checksum as your reference code is doing).

    To create a raw socket, use socket(AF_INET, SOCK_RAW, 0).

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

Sidebar

Related Questions

I'm trying build a regex that will replace any characters not of the format:
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
I'd like to build a turn-based, multiplayer iPhone game that will require a game
I am trying to build an application that converts my old custom Ethernet logs
I'm trying to build a simple echo server/client that works on ethernet level(using raw
all I am trying to establish peer to peer (UDP) communication via firefox extension.
I'm trying to build a download manager class that packets all the async download
I'm trying to build a UDP client on an android tablet. The application can
I am constructing a data packet to be sent over NSStream to a server.
I'm trying to build an RTP packet metric analyzer in C, but i ended

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.