Open C Sockets: recv, recvfrom, recvmsg methods

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Created: polaka_sr (17 Jun 2008)
Last edited: hamishwillee (27 Jul 2012)

The recv, recvfrom, recvmsg methods helps in receiving a message from a socket.

ssize_t recv (int s, void *buf, size_t len, int flags);
ssize_t recvfrom (int s, void * restrict buf, size_t len, int flags,
struct sockaddr * restrict from, socklen_t * restrict fromlen);
ssize_t recvmsg (int s, struct msghdr *msg, int flags);

The recvfrom and recvmsg system calls are used to receive messages from a socket, and may be used to receive data on a socket whether or not it is connection-oriented. The recv function is normally used only on a connected socket (see connect and is identical to recvfrom with a null pointer passed as its from argument. All three routines return the length of the message on successful completion.

The recvmsg system call uses a msghdr structure to minimize the number of directly supplied arguments. This structure has the following form, as defined in sys/socket.h:

struct msghdr {
caddr_t msg_name; /* optional address */
u_int msg_namelen; /* size of address */
struct iovec *msg_iov; /* scatter/gather array */
u_int msg_iovlen; /* # elements in msg_iov */
caddr_t msg_control; /* ancillary data, see below */
u_int msg_controllen; /* ancillary data buffer len */
int msg_flags; /* flags on received message */

And following code snippets shows the usage of recv, recvfrom, recvmsg functions:

#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
void Recv()
struct sockaddr_in serv_addr;
int sock_fd;
char line[10];
int size = 10;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
serv_addr.sin_port = htons(5000);
connect(sock_fd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
recv(sock_fd, line, size, 0);

#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
void Sendto()
struct sockaddr_in sender_addr;
int sock_fd;
char line[15] = "Hello World!";
unsigned int size = sizeof(sender_addr);
sender_addr.sin_family = AF_INET;
sender_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
sender_addr.sin_port = htons(5000);
recvfrom(sock_fd,line,13,0,(struct sockaddr*)&sender_addr,&size);

#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
void SendMsgRecvMsg()
int sock_fd;
unsigned int sender_len;
struct msghdr msg;
struct iovec iov;
struct sockaddr_in receiver_addr,sender_addr;
char line[10];
receiver_addr.sin_family = AF_INET;
receiver_addr.sin_addr.s_addr = htonl(INADDR_ANY);
receiver_addr.sin_port = htons(5000);
bind(sock_fd,(struct sockaddr*)&receiver_addr,sizeof(receiver_addr));
sender_len = sizeof(sender_addr);
msg.msg_name = &sender_addr;
msg.msg_namelen = sender_len;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_iov->iov_base = line;
msg.msg_iov->iov_len = 10;
msg.msg_control = 0;
msg.msg_controllen = 0;
msg.msg_flags = 0;

These calls return the number of bytes received, or -1 if an error occurred.

Wiki Links

Open C Sockets Overview

This page was last modified on 27 July 2012, at 07:18.
180 page views in the last 30 days.