Chapter Contents

Previous

Next
recvfrom

recvfrom



Stores Messages from a Connected or Unconnected Socket Descriptor

Portability: UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
PORTABILITY
EXAMPLE
RELATED FUNCTIONS


SYNOPSIS

#include <sys/types.h>
#include <sys/socket.h>

int recvfrom(int s, void *buf, int len, int flags, void *from,
             int *addrlen);


DESCRIPTION

recvfrom receives data from an unconnected or connected socket descriptor s . If from is nonzero, the source address of the message is stored in the buffer addressed by from . addrlen is the size of the buffer pointed to by from . buf points to the buffer receiving the message. len is the size of the message. recvfrom is particularly useful for unconnected datagram sockets.

If no messages are available, recvfrom blocks the call until a message arrives unless the socket is set in non-blocking mode. flags consists of the following:

MSG_OOB
processes a single byte of out-of-band data if data are present. If out-of-band data have been moved inline with the SO_OOBINLINE socket option, multiple bytes of data can be accessed, and the MSG_OOB option is not necessary.

MSG_PEEK
peeks at the incoming message. The data remain available for the next recvfrom call.


RETURN VALUE

If recvfrom succeeds, it returns the length of the message. If recv returns a 0 , it indicates that the connection is closed. Otherwise, it returns a -1 , and sets errno to indicate the type of error. It is common for recvfrom to return fewer than the total number of bytes in the buffers. This is not an error.


CAUTION

recvfrom discards excess bytes if a datagram is larger than the specified buffer.


PORTABILITY

recvfrom is portable to other environments, including most UNIX systems, that implement BSD sockets.


EXAMPLE

In this example, recvfrom receives a datagram on socket descriptor s and places it in buffer in .

#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>

#define MAX_DGRAM_SIZE 256    /* Chosen by the application. */

f()
{
   int s;
   int b;
   struct sockaddr_in from;            /* Sender's address. */
   int fromlen;              /* Length of sender's address. */
   char in[MAX_DGRAM_SIZE] ;

      /* Open a datagram socket to receive the packet.         */
   s = socket(AF_INET, SOCK_DGRAM, 0);
   .
   .
   .
      /* Receive a datagram into the buffer.                   */
   fromlen = sizeof(from);
   b = (recvfrom(s, in, MAX_DGRAM_SIZE, 0, &from, &fromlen));
   if (b == -1) {
      perror("Recvfrom failed");
      return -1;
   }

   printf("Datagram size: %d.\n", b);
   printf("Datagram's IP address is: %s\n", inet_ntoa(from.sin_addr));
   printf("Datagram's port is: %d\n", (int) ntohs(from.sin_port));
   .
   .
   .
}


RELATED FUNCTIONS

connect , getsockopt , recv , recvmsg , send , sendmsg , sendto , setsockopt


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.