Chapter Contents

Previous

Next
getsockopt

getsockopt



Gets the Value of an Option

Portability: UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
PORTABILITY
EXAMPLE
RELATED FUNCTIONS


SYNOPSIS

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

int getsockopt(int s, int level, int optname, void *optval, int *optlen);


DESCRIPTION

getsockopt returns the value of an option associated with socket s . The level argument is the level of the option. The optname argument is the name of the option. The optval argument is a pointer to the buffer that receives the value of the requested option. As an input parameter, the integer pointed to by optlen should be set to the size of the optval buffer. On output, the integer pointed to by optlen is set to the size of the returned value. For cases in which optval points to an integer, a stored value of 0 indicates that the option is off. A nonzero returned value indicates that the option is on.

Most options are at the socket level. Pass SOL_SOCKET as the level parameter for these options:

SO_ERROR
gets the error status of the socket. The TCP/IP software maintains an errno value (the SO_ERROR field on a UNIX operating system) for each socket. The SO_ERROR option obtains and then clears this field, which is useful when checking for errors that occur between socket calls. In this case, optval should point to an integer.

SO_LINGER
gets the setting of the linger option. The linger option controls the behavior of the call to close when some data have not yet been sent. optval should point to a struct linger . If the value of the l_onoff field is 0 , close returns immediately. In this case, TCP/IP continues to attempt to deliver the data, but the program is not informed if delivery fails. If the value of the l_onoff field is nonzero, the behavior of the close call depends on the value of the l_linger field. If this value is 0 , unsent data are discarded at the time of the close . If the value of l_linger is not 0 , the close call blocks the caller until there is a timeout or until all data are sent. The l_linger field indicates the length of the desired timeout period. Some TCP/IP implementations may ignore the value of the l_linger field. s must be a stream socket.

SO_OOBINLINE
receives out-of-band data inline, that is, without setting the MSG_OOB flag in recv calls. optval should point to an integer. s must be a stream socket. Refer to "Out-of-Band Data" in Chapter 6, "Berkeley Sockets," in UNIX Network Programming for information on out-of-band data.

SO_REUSEADDR
allows local port address to be reused. optval should point to an integer. s must be a stream socket.

SO_TYPE
gets type of socket: SOCKET_STREAM , SOCK_RAW , or SOCK_DGRAM . The optval pointer should point to an integer.

The option level IPPROTO_TCP is available for one option. The TCP_NODELAY option indicates that TCP's normal socket buffering should not be used on the socket. The TCP_NODELAY option is not operative; it is supported for source code compatibility. The <netinet/in.h> and <netinet/tcp.h> headers files are required for this option.


RETURN VALUE

If getsockopt succeeds, it returns a 0 . Otherwise, it returns a -1 and sets errno to indicate the type of error.


PORTABILITY

getsockopt is portable to other environments, including most UNIX systems, that implement BSD sockets. The supported options may vary depending on the system, and the options described previously may be supported differently. Additional options may be supported by a specific TCP/IP software product. Refer to your software documentation for details.


EXAMPLE

In this example getsockopt gets the type of socket.

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

main()
{
   int optlen, gs, socktype, s;

      /* Create a datagram socket. */
   s = socket(AF_INET, SOCK_DGRAM, 0);
   if (s == -1) {
      perror("Socket not created");
      return EXIT_FAILURE;
   }

      /* Ask for the socket type. */
   optlen = sizeof(socktype);
   gs = getsockopt (s, SOL_SOCKET, SO_TYPE, &socktype, &optlen);
   if (gs == -1) {
      perror("getsockopt failed");
      return EXIT_FAILURE;
   }
      /* Print socket type. */
   switch (socktype)
   {
   case SOCK_STREAM:
      puts("Stream socket.\n");
      break;
   case SOCK_DGRAM:
      puts("Datagram socket.\n");
      break;
   case SOCK_RAW:
      puts("Raw socket.\n");
      break;
   default:
      puts("Unknown socket type.\n");
      break;
   }
   return EXIT_SUCCESS;
}


RELATED FUNCTIONS

bind , close , fcntl , ioctl , recv , setsockopt , socket


Chapter Contents

Previous

Next

Top of Page

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