Connectionless Sockets
peer to peer
Datagrams
Connectionless communications uses datagrams, which are independent chunks of data containing a destination address.There's no attempt to keep datagrams in order, and no guarantee that they'll even arrive. By contrast, a SOCK_STREAM garantees both ordering and arrival.
sendto -- send message to socket
#include <sys/socket.h>
ssize_t sendto(
int socket_fd,
const void* message,
size_t length,
ing flags,
const struct sockaddr *sa,
socklen_t sa_len
);
recvfrom -- recevie message from socket
ssize_t recvfrom(
int sock_fd,
void* buffer,
size_t length,
int flags,
struct sockadd* sa,
socklen_t* sa_len
);
sendmsg -- send message to socket using msghdr structure
#include <sys/socket.h>
ssize_t sendmsg(
int socket_fd,
const struct msghdr* message,
int flag
);
recvmsg -- receive message from socket using msghdr structure.
ssize_t recvmsg(
int socket_fd,
struct msghdr* message,
int flags
);
struct msghdr {
void *msg_name; /* socket address */
socklen_t msg_namelen; /* size of address */
struct iovec* msg_iov;
int msg_iovlen;
void* msg_control;
socklen_t msg_controllen;
int msg_flags;
}
Out-of-Band Data
To send an urgent message on a socket connection, it's usually sent out-of-band, so that it won't wait for data sent ahead of it to finish.
To send an out-of-band message, set the MSG_OOB flag with sendto, sendmsg or send.