recvrecvfrom Function

 < Day Day Up > 



recv/recvfrom Function

The recv and recvfrom functions are used to retrieve data that has been queued for a given socket. The semantics of the functions are based upon the type of socket being used (stream or datagram), in addition to a set of flags that can be provided to the functions. The prototypes for the recv and recvfrom functions are:

#include <sys/types.h> #include <sys/socket.h> int recv( int sock, char *buf, int len, int flag ); int recvfrom( int sock, const char *buf, int len, int flag,                 struct sockaddr *addr, socklen_t *len );

The recv function is used by sockets in a connected state. This implies sockets of type SOCK_STREAM as well as SOCK_DGRAM sockets that have previously performed a connect function to associate the remote peer to the local socket. The recvfrom function can be used by stream and datagram sockets.

Let’s now take a deeper look at the recv. The recv function returns either the number of bytes received from the socket, or a –1 if an error occurred. The socket from which data is desired is passed as the first argument (sock), followed by a buffer argument called buf into which the recv function will copy the socket data. The amount of data requested (which must be at least the size in bytes of the preallocated buf argument) is provided next as the len argument. Finally, the flag argument is provided, which can be used to alter the behavior of the recv call (see Table 3.3).

Table 3.3: MOST COMMON FLAGS FOR THE recv/recvfrom FUNCTIONS

Flag

Description

MSG_OOB

Requests out-of-band data from the socket

MSG_PEEK

Returns data, but doesn’t consume

MSG_WAITALL

Requests blocking call until all requested data is returned

MSG_OOB refers to the request of out-of-band data through the socket. Out-of-band data, otherwise known as expedited data, permits an application to communicate data with higher priority to the peer. MSG_PEEK allows an application to preview the data that’s available in the socket buffer to read, without actually consuming it at the Sockets layer. This means that an application must at some point read the data without specifying the MSG_PEEK flag to remove it from the Sockets buffer. Finally, the MSG_WAITALL flag tells the Sockets layer to block on the recv and recvfrom call until the specified number of bytes is available to read.

Let’s look at a couple of examples of the recv and recvfrom calls. Either call can be used in a stream or datagram setting, though the recvfrom call is rarely used for stream sockets. Because the recvfrom call provides peer socket information, and stream sockets are tied to a specific peer, the recvfrom call is not useful in this setting. If a stream socket needs the peer information, the getpeername function can be used instead.

The first example illustrates the recv call in a stream setting. We’ll omit some of the intermediate functions to concentrate solely on recv (see Listing 3.6). After the following recv call completes, the ret variable contains the number of bytes returned or an error status (–1). The number of bytes returned is contained in the buffer character array. A maximum of MAX_BUF are returned, though a smaller number of bytes may be returned.

Listing 3.6 Example of the recv function in a stream setting.

start example
int sock, ret; struct sockaddr_in sa; char buffer[MAX_BUF+1]; sock = socket( AF_INET, SOCK_STREAM, 0 ); ... ret = recv( sock, buffer, MAX_BUF, 0 ); printf( "%d bytes returned\n", ret );
end example

Now let’s turn our attention to the recvfrom call (shown in Listing 3.7). Prior to calling recvfrom, we must initialize the palen variable with the size of the address structure. The recvfrom call updates the palen variable internally (note the pass by reference) to identify the size of the resulting pa structure. We’re operating in the AF_INET domain, so this size will not change before or after the call to recvfrom. After recvfrom has completed, the size of the message is returned into the ret variable. Our datagram socket is provided in sock along with the message in the buffer character array (up to a maximum size MAX_BUF). No flags are passed in this example. Finally, the address structure is passed by reference (pa) along with the structure length (palen).

Listing 3.7 Example of the recvfrom function in a datagram setting.

start example
int sock, ret, palen; struct sockaddr_in sa, pa; char buffer[MAX_BUF+1]; sock = socket( AF_INET, SOCK_DGRAM, 0 ); ... palen = sizeof( pa ); ret = recvfrom( sock, buffer, MAX_BUF, 0,                 (struct sockaddr_in *)&pa, &palen ); printf( "Returned %d bytes\n", ret ); printf( "Peer Address %s\n", inet_ntoa( pa.sin_addr ) ); printf( "Peer port %d\n", pa.sin_port );
end example

To demonstrate the returns, the three printf calls at the end of Listing 3.7 illustrate the result of the recvfrom call. The size of the message is emitted first (ret), followed by the IP address of the peer (sin_addr), reconstructed into a string using the inet_ntoa function (to be discussed later in this chapter). Finally, the port number of the peer socket is emitted from the sin_port field of the socket address structure.



 < Day Day Up > 



BSD Sockets Programming from a Multi-Language Perspective
Network Programming for Microsoft Windows , Second Edition (Microsoft Programming Series)
ISBN: 1584502681
EAN: 2147483647
Year: 2003
Pages: 225
Authors: Jim Ohlund

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net