Tenouk C & C++ |< Winsock Prog. Example 5 | Main | Winsock Prog. Example 7 >| Site Index | Download | Linux Socket | Winsock in .NET


 

 

 

 

 

 

 

WINSOCK 2

WINDOWS SOCKET: PROGRAM EXAMPLES PART 6

 

 

 

 

 

 

 

 

What do we have in this chapter?

  1. setsockopt()

  2. Return Values

  3. Remarks

  4. setsockopt() Program Example

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Winsock Tutorial

 

Winsock: Story 1

Winsock: Story 2

Winsock: Story 3

Winsock: Example 1

Winsock: Example 2

Winsock: Example 3

Winsock: Example 4

Winsock: Example 5

Winsock: Example 6

Winsock: Example 7

Winsock: Example 8

Winsock: Example 9

Winsock: Example 10

Winsock: Example 11

Winsock: Example 12

Winsock: Reference 1

Winsock: Reference 2

Winsock: Reference 3

Winsock: Reference 4

Another Complete and Advanced C & Winsock2 Programming Tutorial

My Training Period:          hours

 

 

Machine‚Äôs OS is standalone Windows Xp Pro with SP2 except whenever mentioned.  Compiler used was Visual C++ 2003 .Net 1.1. Beware the codes that span more than one line.  Program examples have been tested for Non Destructive Test.  All information compiled for Windows 2000 (NT5.0) above and...

 

  1. The story was discussed at Winsock introduction story.

  2. Related functions, structures and macros used in the program examples have been dumped at Winsock structure & function 1 and Winsock structure & function 2.

  3. Other related and required information (if any) not available in no. 2 can be found at MSDN & Visual C++ online reference.

 

skills to be acquired:

 

  • Able to understand Winsock implementation environment and operations through the APIs and program examples.

  • Able to gather, understand and use the Winsock functions, structures and macros in your programs.

  • Able to build programs that use Microsoft C/Standard C programming language and Winsock APIs.

 

Socket Options

 

You can use the getsockopt() and setsockopt() functions for more getting and setting socket options. To enumerate protocols and discover supported properties for each installed protocol, use the WSAEnumProtocols() function. The following socket option levels are available:

  1. IPPROTO_IP

  2. IPPROTO_IPV6

  3. IPPROTO_RM

  4. IPPROTO_TCP

  5. IPPROTO_UDP

  6. NSPROTO_IPX

  7. SOL_APPLETALK

  8. SOL_IRLMP

  9. SOL_SOCKET

Please refer to MSDN documentation for the details. All SO_* socket options apply equally to IPv4 and IPv6 (except SO_BROADCAST, since broadcast is not implemented in IPv6).

 

setsockopt()

 

Item

Description

Function

setsockopt().

Use

Sets a socket option.

Prototype

int setsockopt( SOCKET s, int level, int optname, const char* optval, int optlen);

Parameters

s - [in] Descriptor identifying a socket.

level - [in] Level at which the option is defined. Example: SOL_SOCKET.

optname - [in] Socket option for which the value is to be set. Example: SO_BROADCAST. The optname value must be a socket option defined within the specified level, or behavior is undefined.

optval - [in] Pointer to the buffer in which the value for the requested option is specified.

optlen - [in] Size of the optval buffer, in bytes.

Return value

See below.

Include file

<winsock2.h>

Library

ws2_32.lib.

Remark

See below.

 

Table 1

 

Return Values

 

If no error occurs, setsockopt() returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError().

 

 

 

 

Error code

Meaning

WSANOTINITIALISED

A successful WSAStartup() call must occur before using this function.

WSAENETDOWN

The network subsystem has failed.

WSAEFAULT

optval is not in a valid part of the process address space or optlen parameter is too small.

WSAEINPROGRESS

A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.

WSAEINVAL

level is not valid, or the information in optval is not valid.

WSAENETRESET

Connection has timed out when SO_KEEPALIVE is set.

WSAENOPROTOOPT

The option is unknown or unsupported for the specified provider or socket.

WSAENOTCONN

Connection has been reset when SO_KEEPALIVE is set.

WSAENOTSOCK

The descriptor is not a socket.

 

Table 2

 

Remarks

 

The setsockopt() function sets the current value for a socket option associated with a socket of any type, in any state. Although options can exist at multiple protocol levels, they are always present at the uppermost socket level. Options affect socket operations, such as whether expedited data (Out-of-Band (OOB) data for example) is received in the normal data stream, and whether broadcast messages can be sent on the socket. If the setsockopt() function is called before the bind() function, TCP/IP options will not be checked with TCP/IP until the bind() occurs. In this case, the setsockopt() function call will always succeed, but the bind() function call may fail because of an early setsockopt() failing. If a socket is opened, a setsockopt() call is made, and then a sendto() call is made, Windows Sockets performs an implicit bind() function call.

There are two types of socket options: Boolean options that enable or disable a feature or behavior, and options that require an integer value or structure. To enable a Boolean option, optval points to a nonzero integer. To disable the option optval points to an integer equal to zero. The optlen parameter should be equal to sizeof(int) for Boolean options. For other options, optval points to an integer or structure that contains the desired value for the option, and optlen is the length of the integer or structure. The following options are supported for setsockopt(). For default values of these options, see the description. The Type identifies the type of data addressed by optval.

 

For level = SOL_SOCKET

 

Value

Type

Meaning

SO_BROADCAST

BOOL

Enables transmission and receipt of broadcast messages on the socket.

SO_CONDITIONAL_ACCEPT

BOOL

Enables sockets to delay the acknowledgment of a connection until after the WSAAccept() condition function is called.

SO_DEBUG

BOOL

Records debugging information.

SO_DONTLINGER

BOOL

Does not block close waiting for unsent data to be sent. Setting this option is equivalent to setting SO_LINGER with l_onoff set to zero.

SO_DONTROUTE

BOOL

Does not route: sends directly to interface. Succeeds but is ignored on AF_INET sockets; fails on AF_INET6 sockets with WSAENOPROTOOPT. Not supported on ATM sockets (results in an error).

SO_GROUP_PRIORITY

int

Reserved.

SO_KEEPALIVE

BOOL

Sends keep-alives. Not supported on ATM sockets (results in an error).

SO_LINGER

LINGER

Lingers on close if unsent data is present.

SO_OOBINLINE

BOOL

Receives OOB data in the normal data stream.

SO_RCVBUF

int

Specifies the total per-socket buffer space reserved for receives. This is unrelated to SO_MAX_MSG_SIZE or the size of a TCP window.

SO_REUSEADDR

BOOL

Allows the socket to be bound to an address that is already in use. (See bind().) Not applicable on ATM sockets.

SO_EXCLUSIVEADDRUSE

BOOL

Enables a socket to be bound for exclusive access. Does not require administrative privilege.

SO_SNDBUF

int

Specifies the total per-socket buffer space reserved for sends. This is unrelated to SO_MAX_MSG_SIZE or the size of a TCP window.

PVD_CONFIG

Service Provider Dependent

This object stores the configuration information for the service provider associated with socket s. The exact format of this data structure is service provider specific.

 

Table 3

 

For level = IPPROTO_TCP1

 

Value

Type

Meaning

TCP_NODELAY

BOOL

Disables the Nagle algorithm for send coalescing.

1     included for backward compatibility with Windows Sockets 1.1

 

Table 4

 

For level = NSPROTO_IPX

 

Windows NT supports all IPX options (Netware-obsolete?). Windows Me/98/95 supports only the following options:

 

  1. IPX_PTYPE

  2. IPX_FILTERPTYPE

  3. IPX_DSTYPE

  4. IPX_RECVHDR

  5. IPX_MAXSIZE (used with the getsockopt() function)

  6. IPX_ADDRESS (used with the getsockopt() function)

 

 

Value

Type

Meaning

IPX_PTYPE

int

Sets the IPX packet type.

IPX_FILTERPTYPE

int

Sets the receive filter packet type

IPX_STOPFILTERPTYPE

int

Stops filtering the filter type set with IPX_FILTERTYPE

IPX_DSTYPE

int

Sets the value of the data stream field in the SPX header on every packet sent.

IPX_EXTENDED_ADDRESS

BOOL

Sets whether extended addressing is enabled.

IPX_RECVHDR

BOOL

Sets whether the protocol header is sent up on all receive headers.

IPX_RECEIVE_BROADCAST

BOOL

Indicates broadcast packets are likely on the socket. Set to TRUE by default. Applications that do not use broadcasts should set this to FALSE for better system performance.

IPX_IMMEDIATESPXACK

BOOL

Directs SPX connections not to delay before sending an ACK. Applications without back-and-forth traffic should set this to TRUE to increase performance.

 

Table 5

 

BSD options not supported for setsockopt are shown in the following table.

 

Value

Type

Meaning

SO_ACCEPTCONN

BOOL

Socket is listening.

SO_RCVLOWAT

int

Receives low watermark.

SO_RCVTIMEO

int

Receives time-out in milliseconds (available in the Microsoft implementation of Windows Sockets 2).

SO_SNDLOWAT

int

Sends low watermark.

SO_SNDTIMEO

int

Sends time-out in milliseconds (available in the Microsoft implementation of Windows Sockets 2).

SO_TYPE

int

Type of the socket.

 

Table 6

 

  1. SO_CONDITIONAL_ACCEPT - Setting this socket option to TRUE delays the acknowledgment of a connection until after the WSAAccept() condition function is called. If FALSE, the connection may be accepted before the condition function is called, but the connection will be disconnected if the condition function rejects the call. This option must be set before calling the listen() function, otherwise WSAEINVAL is returned. SO_CONDITIONAL_ACCEPT is only supported for TCP and ATM. TCP sets SO_CONDITIONAL_ACCEPT to FALSE by default, and therefore by default the connection will be accepted before the WSAAccept() condition function is called. When set to TRUE, the conditional decision must be made within the TCP connection time-out. CF_DEFER connections are still subject to the time-out. ATM sets SO_CONDITIONAL_ACCEPT to TRUE by default.

  2. SO_DEBUG - Windows Sockets service providers are encouraged (but not required) to supply output debug information if the SO_DEBUG option is set by an application. The mechanism for generating the debug information and the form it takes are beyond the scope of this document.

  3. SO_GROUP_PRIORITY - Reserved for future use with socket groups. Group priority indicates the relative priority of the specified socket relative to other sockets within the socket group. Values are nonnegative integers, with zero corresponding to the highest priority. Priority values represent a hint to the underlying service provider about how potentially scarce resources should be allocated. For example, whenever two or more sockets are both ready to transmit data, the highest priority socket (lowest value for SO_GROUP_PRIORITY) should be serviced first with the remainder serviced in turn according to their relative priorities.

  4. SO_KEEPALIVE - An application can request that a TCP/IP provider enable the use of keep-alive packets on TCP connections by turning on the SO_KEEPALIVE socket option. A Windows Sockets provider need not support the use of keep-alives. If it does, the precise semantics are implementation-specific but should conform to section 4.2.3.6 of RFC 1122: Requirements for Internet Hosts - Communication Layers. If a connection is dropped as the result of keep-alives the error code WSAENETRESET is returned to any calls in progress on the socket, and any subsequent calls will fail with WSAENOTCONN.SO_LINGER - The SO_LINGER option controls the action taken when unsent data is queued on a socket and a closesocket() is performed. See closesocket for a description of the way in which the SO_LINGER settings affect the semantics of closesocket. The application sets the desired behavior by creating a LINGER structure (pointed to by the optval parameter) with these members l_onoff and l_linger set appropriately.

  5. SO_REUSEADDR - By default, a socket cannot be bound (see bind()) to a local address that is already in use. On occasion, however, it can be necessary to reuse an address in this way. Since every connection is uniquely identified by the combination of local and remote addresses, there is no problem with having two sockets bound to the same local address as long as the remote addresses are different. To inform the Windows Sockets provider that a bind on a socket should not be disallowed because the desired address is already in use by another socket, the application should set the SO_REUSEADDR socket option for the socket before issuing the bind(). The option is interpreted only at the time of the bind(). It is therefore unnecessary and harmless to set the option on a socket that is not to be bound to an existing address. Setting or resetting the option after the bind has no effect on this or any other socket.

  6. SO_RCVBUF and SO_SNDBUF - When a Windows Sockets implementation supports the SO_RCVBUF and SO_SNDBUF options, an application can request different buffer sizes (larger or smaller). The call to setsockopt() can succeed even when the implementation did not provide the whole amount requested. An application must call getsockopt with the same option to check the buffer size actually provided.

 
  1. SO_RCVTIMEO and SO_SNDTIMEO - When using the recv() function, if no data arrives during the period specified in SO_RCVTIMEO, the recv() function completes. In Windows versions prior to Windows 2000, any data received subsequently fails with WSAETIMEDOUT. In Windows 2000 and later, if no data arrives within the period specified in SO_RCVTIMEO the recv function returns WSAETIMEDOUT, and if data is received, recv returns SUCCESS. If a send or receive operation times out on a socket, the socket state is indeterminate, and should not be used; TCP sockets in this state have a potential for data loss, since the operation could be canceled at the same moment the operation was to be completed.

  2. PVD_CONFIG - This object stores the configuration information for the service provider associated with the socket specified in the s parameter. The exact format of this data structure is specific to each service provider.

  3. TCP_NODELAY - The TCP_NODELAY option is specific to TCP/IP service providers. The Nagle algorithm is disabled if the TCP_NODELAY option is enabled (and vice versa). The process involves buffering send data when there is unacknowledged data already in flight or buffering send data until a full-size packet can be sent. It is highly recommended that TCP/IP service providers enable the Nagle Algorithm by default, and for the vast majority of application protocols the Nagle Algorithm can deliver significant performance enhancements. However, for some applications this algorithm can impede performance, and TCP_NODELAY can be used to turn it off. These are applications where many small messages are sent, and the time delays between the messages are maintained. Application writers should not set TCP_NODELAY unless the impact of doing so is well-understood and desired because setting TCP_NODELAY can have a significant negative impact on network and application performance.

 

setsockopt() Program Example

 

The following example demonstrates the setsockopt() function.

#include <stdio.h>

#include <winsock2.h>

 

void main()

{

  // Declare variables

  WSADATA wsaData;

  SOCKET ListenSocket;

  sockaddr_in service;

 

  // Initialize Winsock, request the Winsock 2.2

  int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);

  if(iResult != NO_ERROR)

    printf("Error at WSAStartup().\n");

  else

    printf("WSAStartup() is OK.\n");

 

  // Create a listening socket

  ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

  if (ListenSocket == INVALID_SOCKET)

  {

    printf("Error at socket().\n");

    WSACleanup();

    return;

  }

  else

         printf("socket() is OK.\n");

 

  // Bind the socket to the local IP address and port 55555

  hostent* thisHost;

  char* ip;

  u_short port;

  port = 55555;

  thisHost = gethostbyname("");

  ip = inet_ntoa(*(struct in_addr *)*thisHost->h_addr_list);

 

  // Try running this program on machine using live IP...

  printf("\nIP address is: %s.\n", ip);

  printf("Hostname is: %s.\n", thisHost->h_name);

  printf("Address type: %i.\n\n", thisHost->h_addrtype);

 

  service.sin_family = AF_INET;

  service.sin_addr.s_addr = inet_addr(ip);

  service.sin_port = htons(port);

 

  if (bind(ListenSocket,(SOCKADDR*) &service, sizeof(service)) == SOCKET_ERROR)

  {

    printf("bind() failed lol!\n");

    closesocket(ListenSocket);

    return;

  }

else

         printf("bind() is OK.\n");

 

  // Initialize variables and call setsockopt().

  // The SO_KEEPALIVE parameter is a socket option

  // that makes the socket send keepalive messages

  // on the session. The SO_KEEPALIVE socket option

  // requires a boolean value to be passed to the

  // setsockopt function. If TRUE, the socket is

  // configured to send keepalive messages, if FALSE

  // the socket configured to NOT send keepalive messages.

  // This section of code tests the setsockopt() function

  // by checking the status of SO_KEEPALIVE on the socket

  // using the getsockopt() function.

  BOOL bOptVal = TRUE;

  int bOptLen = sizeof(BOOL);

  int iOptVal;

  int iOptLen = sizeof(int);

 

  if (getsockopt(ListenSocket, SOL_SOCKET, SO_KEEPALIVE, (char*)&iOptVal, &iOptLen) != SOCKET_ERROR)

  {

    printf("SO_KEEPALIVE value: %ld.\n", iOptVal);

  }

 

  if (setsockopt(ListenSocket, SOL_SOCKET, SO_KEEPALIVE, (char*)&bOptVal, bOptLen) != SOCKET_ERROR)

  {

    printf("Sends keep-alives. Setting the SO_KEEPALIVE.\n");

  }

 

  if (getsockopt(ListenSocket, SOL_SOCKET, SO_KEEPALIVE, (char*)&iOptVal, &iOptLen) != SOCKET_ERROR)

  {

    printf("SO_KEEPALIVE value: %ld.\n", iOptVal);

  }

  // Binding to the already used socket

  BOOL bOptVal1 = TRUE;

  int bOptLen1 = sizeof(BOOL);

  int iOptVal1;

  int iOptLen1 = sizeof(int);

 

  if (setsockopt(ListenSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&bOptVal1, bOptLen1) != SOCKET_ERROR)

  {

    printf("Socket can be bound to an address that is already in use.\n");

  }

 

  if (getsockopt(ListenSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&iOptVal1, &iOptLen1) != SOCKET_ERROR)

  {

    printf("SO_REUSEADDR value: %ld.\n", iOptVal1);

  }

 

  WSACleanup();

  return;

}

 

Sample output:

Windows socket program example output screen: using setsockopt()

 

Figure 1

 

For IrDA Sockets keep in mind the following:

  1. The af_irda.h header file must be explicitly included.

  2. IrDA provides the following settable socket option:

 

Value

Type

Meaning

IRLMP_IAS_SET

*IAS_SET

Sets IAS attributes

 

Table 7

 

The IRLMP_IAS_SET socket option enables the application to set a single attribute of a single class in the local Internet Authentication Service (IAS). The application specifies the class to set, the attribute, and attribute type. The application is expected to allocate a buffer of the necessary size for the passed parameters. IrDA provides an IAS database that stores IrDA-based information. Limited access to the IAS database is available through the Windows Sockets 2 interface, but such access is not normally used by applications, and exists primarily to support connections to non-Windows devices that are not compliant with the Windows Sockets 2 IrDA conventions. The following structure, IAS_SET, is used with the IRLMP_IAS_SET setsockopt() option to manage the local IAS database:

typedef struct _IAS_SET

{

    char      irdaClassName[IAS_MAX_CLASSNAME];

    char      irdaAttribName[IAS_MAX_ATTRIBNAME];

    u_long    irdaAttribType;

    union

    {

              LONG irdaAttribInt;

              struct

              {

                   u_short   Len;

                   u_char    OctetSeq[IAS_MAX_OCTET_STRING];

              } irdaAttribOctetSeq;

              struct

              {

                   u_char    Len;

                   u_char    CharSet;

                   u_char    UsrStr[IAS_MAX_USER_STRING];

              } irdaAttribUsrStr;

    } irdaAttribute;

} IAS_SET, *PIAS_SET, FAR *LPIAS_SET;

The following structure, IAS_QUERY, is used with the IRLMP_IAS_QUERY setsockopt() option to query a peer's IAS database:

typedef struct _WINDOWS_IAS_QUERY

{

        u_char   irdaDeviceID[4];

        char     irdaClassName[IAS_MAX_CLASSNAME];

        char     irdaAttribName[IAS_MAX_ATTRIBNAME];

        u_long   irdaAttribType;

        union

        {

                  LONG    irdaAttribInt;

                  struct

                  {

                          u_long  Len;

                          u_char  OctetSeq[IAS_MAX_OCTET_STRING];

                  } irdaAttribOctetSeq;

                  struct

                  {

                          u_long  Len;

                          u_long  CharSet;

                          u_char  UsrStr[IAS_MAX_USER_STRING];

                  } irdaAttribUsrStr;

        } irdaAttribute;

} IAS_QUERY, *PIAS_QUERY, FAR *LPIAS_QUERY;

Many SO_ level socket options are not meaningful to IrDA. Only SO_LINGER is specifically supported.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Further reading and digging:

 

  1. Linux Sockets: Story and program examples.

  2. Linux and TCP/IP.

  3. A complete info on Windows socket reference from MSDN which include managed and unmanaged API doc.

  4. Structure, enum, union and typedef story can be found struct, enum, union & typedef tutorial.

  5. For Multibytes, Unicode characters and Localization please refer to Multibyte, Unicode and wide characters (Story) and Win32 Windows & Users tutorial (Implementation).

  6. Windows data type information is Win32 - Windows data types.

  7. Check the best selling C / C++ and Windows books at Amazon.com.

 

 

 

 


 

Tenouk C & C++ |< Winsock Prog. Example 5 | Main | Winsock Prog. Example 7 >| Site Index | Download | Linux Socket | Winsock in .NET