getsockopt setsockopt Functions

 < Day Day Up > 



getsockopt/ setsockopt Functions

Unix systems provide a number of ways to change the behavior of a socket. These include not only setsockopt but also other Unix-specific functions such as ioctl. We focus here on the socket option functions and how they’re used. In the next chapter (Chapter 5, Socket Options), we look at the specific socket options in more detail.

The prototypes for the getsockopt and setsockopt functions are defined as:

#includes <sys/types.h> #include <sys/socket.h> int getsockopt( int sock, int level, int optname,                  void *optval, socklen_t *optlen ); int setsockopt( int sock, int level, int optname,                  void *optval, socklen_t optlen );

Each function accepts an existing socket as the first argument. It’s important to note that some options must be set prior to connection establishment, whereas others can be performed at any time. We investigate this further in Chapter 5, Socket Options. The second parameter level defines the level of the socket option. The level refers to the layer of the networking stack for which this socket option will take effect. The available levels are shown in Table 4.3.

Table 4.3: STANDARD SOCKET OPTION LEVELS

Level

Description

Option Prefix

SOL_SOCKET

Sockets layer

SO_

IPPROTO_TCP

TCP Transport layer

TCP_

IPPROTO_IP

IP Network layer

IP_

The next argument, optname, defines the particular option that we desire to read or write. A variety of socket option names exist split among the various levels defined in Table 4.3 (more on these in Chapter 5, Socket Options). The optval is a pointer to a variable that defines a value that we want to write (or location for the option value read from the socket). For both read and write, this will be a void pointer because there are a number of different types that can be specified. Finally, optlen defines the length of the variable passed in optval. Because this can be a number of different types of scalars or structures, the optlen parameter provides the size of optval, and in the case of getsockopt, returns the resulting size of optval (as a value-result parameter).

Let’s look at a quick example of both getsockopt and setsockopt. In this example, we’re going to read the size of the send socket buffer (socket option SO_SNDBUF), and then set it to twice that value (see Listing 4.5). The SO_SNDBUF is a Sockets layer option within level SOL_SOCKET.

Listing 4.5 Getting and then setting the send buffer size for a socket.

start example
int sock, ret, size, len; sock = socket( AF_INET, SOCK_STREAM, 0 ); len = sizeof( size ); ret = getsockopt( sock, SOL_SOCKET, SO_SNDBUF,                    (void *)&size, (socklen_t *)&len ); size = size * 2; ret = setsockopt( sock, SOL_SOCKET, SO_SNDBUF,                    (void *)&size, sizeof( size ) ); ...
end example

Both getsockopt and setsockopt return 0 on success and –1 on error. Socket options are an important element of Sockets programming. They can help to improve performance, increase reliability, or simply change the standard behavior of a socket. Chapter 5, Socket Options, discusses a large number of available socket options and their use in Sockets API applications.



 < 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