sendsendto Function

 < Day Day Up > 



send/sendto Function

The send and sendto functions are used to send data to a given peer socket through a local socket. Like the recv functions, 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 send and sendto functions are:

#include <sys/types.h> #include <sys/socket.h> int send( int sock, const void *buf, int len,               unsigned int flags ); int sendto( int sock, const void *buf, int len,                 unsigned int flags,                struct sockaddr *addr, socklen_t len );

The send 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 sendto function can be used by stream and datagram sockets in most implementations. On implementations that support the sendto function on a stream socket, the addr and len reference parameters are ignored.

Let’s take a more in-depth look at these functions. Both the send and sendto functions take as arguments the socket through which the data is sent (sock), the buffer containing the data to send (buf), the length of the data contained in the buffer (len), and a flags bit field. The sendto function also requires two arguments that define the recipient of the data. The addr argument specifies the socket address (IP address and port within the AF_INET address family) and the len argument passes in the size of the sockaddr_in structure.

The flags field of the send and sendto functions alters the behavior of the calls (see Table 3.4). The MSG_OOB flag defines that the caller is providing out-of-band data within the buffer. As defined in [Stevens98], the caller may provide only one byte of out-of-band data. The receiver can read this out-of-band data using the same flag within the recv/recvfrom function.

Table 3.4: MOST COMMON FLAGS FOR THE send/sendto FUNCTIONS

Flag

Description

MSG_OOB

Requests transmission of out-of-band data through the socket

MSG_DONTWAIT

Forces this call to be nonblocking

MSG_DONTROUTE

Bypasses routing tables

The MSG_DONTWAIT flag makes this call nonblocking. This means that the call doesn’t block if it’s unable to queue all of the user buffer data in the socket send buffer. In this case, the number of bytes consumed in the user buffer is returned. The socket could also be made nonblocking for all calls, which differs depending upon the particular API being used. Unix-like systems utilize the fcntl system call for this purpose, whereas some stacks provide a socket option for this purpose.

Finally, the MSG_DONTROUTE flag disables routing algorithms on the local host for this emission. Typically, the destination address is checked to see if it’s on the subnet, and if not, the routing algorithms identify how to route the packet to the destination (first hop). With this flag set, no routing is performed and the resulting packet is emitted as if the destination was on the local subnet.

The send and sendto functions return either the number of bytes queued for transmission in the socket, or a –1 if a local error was detected. Note the distinction between sent and queued for transmission. Upon return of the send (and sendto) call, the bytes passed down in buf are copied into the local socket send buffer. The return value represents the number of bytes that were placed into the socket buffer. If the socket is nonblocking, this may be less than what was attempted, which means that the caller may need to call the send function again to queue the remaining data for transmission. Within the Transport layer, the send buffer is consumed and sent in a manner consistent with the particular transport protocol. Another item of interest is that local errors are detected, but there is no immediate indication that the peer socket received the data.

Let’s now look at a couple of examples. The first example illustrates the send function in a stream socket setting (Listing 3.8) and the second example demonstrates the sendto function in a datagram Sockets application (Listing 3.9).

Listing 3.8 Example of the send function with a stream socket.

start example
int sock, ret; struct sockaddr_in sa; char buf[MAX_BUFFER+1]; sock = socket( AF_INET, SOCK_STREAM, 0 ); memset( &sa, 0, sizeof(sa) ); sa.sin_family = AF_INET; sa.sin_port = htons( SERVER_PORT ); sa.sin_addr.s_addr = inet_addr( "192.168.1.1" ); connect( sock, (struct sockaddr_in *)&sa, sizeof(sa) ); strcpy( buf, "Hello\n" ); ret = send( sock, buf, strlen(buf), 0 );
end example

The stream socket example (Listing 3.8) demonstrates the typical setup of a stream socket, along with defining a server address (through a sockaddr_in structure) and then connecting to it. Once connected, the send function is utilized to send a simple string message to the peer socket. To send, we specify our socket (sock), the data to send to the peer (buf) along with its length (computed using the strlen function). No flags are specified in this example (fourth argument to send, 0).

Listing 3.9 Example of the sendto function with a datagram socket.

start example
int sock, ret; struct sockaddr_in sa; char buf[MAX_BUFFER+1]; sock = socket( AF_INET, SOCK_DGRAM, 0 ); memset( &sa, 0, sizeof(sa) ); sa.sin_family = AF_INET; sa.sin_port = htons( SERVER_PORT ); sa.sin_addr.s_addr = inet_addr( "192.168.1.1" ); strcpy( buf, "Hello\n" ); ret = sendto( sock, buf, strlen(buf), 0,               (struct sockaddr_in *)&sa,                 sizeof(sa) );
end example

The datagram socket example (Listing 3.9) demonstrates a typical setup of a datagram socket. After the datagram socket is created, we create the address structure (sa), which represents the peer of our socket (the recipient of the datagrams that we’ll generate). We load our message into buf (using the string copy function, strcpy). Using sendto, we specify the same parameters as before for the send function (socket descriptor, buffer, length of buffer, and flags), and then provide the recipient address. This comes in the form of the previously created sockaddr_in structure, along with the size of the structure using the sizeof primitive.



 < 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