Maicake
Maicake

Reputation: 1126

What exactly does a udp socket receive?

  #include <sys/socket.h>
  #include <netinet/in.h>
  #include <netinet/udp.h>

  udp_socket = socket(AF_INET, SOCK_DGRAM, 0);

when I use an UDP socket, do I receive just the payload or also the UDP header?

Upvotes: 1

Views: 1370

Answers (1)

ryyker
ryyker

Reputation: 23218

What you can or cannot access depends on how the socket is created.

Here is a comment in the docs relevant to your question:

The recv() function shall receive a message from a connection-mode or connectionless-mode socket. It is normally used with connected sockets because it does not permit the application to retrieve the source address of received data.

(emphasis mine)

read more here...

Edit: If accessing header information is what you want to do, read this and the following link.

...You cannot create a raw socket with IPPROTO_UDP and manipulate the UDP header; likewise with TCP. To manipulate the IP header as well as either the TCP or UDP header (or any other protocol encapsulated in IP), you must use the IP_HDRINCL socket option with a raw socket. ...

Read more on the topic...

Upvotes: 2

Related Questions