< SIO_ROUTING_INTERFACE_QUERY, SO_ACCEPTCONN & SO_BROADCAST Examples | Socket Options & IOCTLs Main | Chap 8: Name Registration & Resolution >


 

 

Socket Options and Ioctls 7 part 10

 

 

What do we have in this chapter 7 part 10?

  1. The SO_MAX_MSG_SIZE Option Program Example

  2. The SO_PROTOCOL_INFO Option Program Example

  3. The SO_RCVTIMEO Option Program Example

  4. The SO_TYPE Option Program Example

 

The SO_MAX_MSG_SIZE Option Program Example

 

Create a new empty Win32 console mode application and add the project/solution name.

 

Winsock 2 socket options and ioctls: The SO_MAX_MSG_SIZE option

 

Add the following source code.

 

// Description:

//    This is a very short sample that simply calls the

//    SO_MAX_MSG_SIZE option to determine the largest datagram size possible.

//

// No command line arguments

//

// Link to ws2_32.lib

#include <winsock2.h>

#include <ws2tcpip.h>

#include <stdio.h>

 

// Function: main

// Description: Load Winsock, creat a UDP socket, and then call the option. Print the results.

int main(int argc, char **argv)

{

    WSADATA       wsd;

    SOCKET        s;

    int           iSize, iVal, ret;

 

    // Load Winsock

    if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)

    {

        printf("WSAStartup() failed with error code %d\n", WSAGetLastError());

        return -1;

    }

    else

        printf("WSAStartup() is fine!\n");

 

    // Create a UDP socket

    s = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, WSA_FLAG_OVERLAPPED);

    if (s == INVALID_SOCKET)

    {

        printf("WSASocket() failed with error code %d\n", WSAGetLastError());

        return -1;

    }

    else

         printf("WSASocket() should be OK!\n");

 

    // Retrieve the option and print the result

    iSize = sizeof(iVal);

    ret = getsockopt(s, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&iVal, &iSize);

    if (ret == SOCKET_ERROR)

    {

        printf("getsockopt(SO_MAX_MSG_SIZE) failed with error code %d\n", WSAGetLastError());

        return -1;

    }

    else

        printf("getsockopt(SO_MAX_MSG_SIZE) is pretty good!\n");

 

    printf("\nThe max message size = %d\n\n", iVal);

 

    if(closesocket(s) == 0)

          printf("closesocket() should be fine!\n");

    else

          printf("closesocket() failed with error code %d\n", WSAGetLastError());

 

     if(WSACleanup() == 0)

          printf("WSACleanup() is OK!\n");

     else

          printf("WSACleanup() failed with error code %d\n", WSAGetLastError());

    return 0;

}

 

Build and run the project.

 

Winsock 2 socket options and ioctls: The SO_MAX_MSG_SIZE option - a sample output

 

The SO_PROTOCOL_INFO Option Program Example

 

Create a new empty Win32 console mode application and add the project/solution name.

 

Winsock 2 socket options and ioctls: The SO_PROTOCOL_INFO option program example

 

Add the following code.

 

// Description:

//    This short sample illustrates how to call the SO_PROTOCOL_INFO

//    option to retrieve the Winsock catalog entry corresponding

//    to the socket. This function only shows how to call the option

//    and does not print out the whole WSAPROTOCOL_INFO structure.

//

// No command line arguments

//

// Link to ws2_32.lib

#include <winsock2.h>

#include <ws2tcpip.h>

#include <stdio.h>

 

// Function: main

// Description:

//    Load Winsock, create a socket, and call SO_PROTOCOL_INFO to

//    retrieve the catalog entry corresponding to our socket.

int main(int argc, char **argv)

{

    WSADATA          wsd;

    SOCKET           s;

    WSAPROTOCOL_INFOA pinf;

    int              iSize,ret;

 

    // Load Winsock

    if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)

    {

        printf("WSAStartup() failed with error code %d\n", WSAGetLastError());

        return -1;

    }

    else

        printf("WSAStartup() should be OK!\n");

 

    // Create our TCP socket

    s = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);

    if (s == INVALID_SOCKET)

    {

        printf("WSASocket() failed with error code %d\n", WSAGetLastError());

        return -1;

    }

    else

        printf("WSASocket() is OK!\n");

 

    // Get the protocol information. You can either use WSA_PROTOCOL_INFO

    // or SO_PROTOCOL_INFOA since we didn't #define UNICODE.

    iSize = sizeof(pinf);

    ZeroMemory(&pinf, iSize);

    ret = getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOA, (char *)&pinf, &iSize);

    if (ret == SOCKET_ERROR)

    {

        printf("getsockopt(SO_PROTOCOL_INFO) failed with error code %d\n", WSAGetLastError());

        return -1;

    }

    else

        printf("getsockopt(SO_PROTOCOL_INFO) should be OK!\n");

 

    // Print a couple fields out

    printf("\nProtocol: [%s]\n", pinf.szProtocol);

    printf("Flags:    0x%x\n\n", pinf.dwServiceFlags1);

 

    if(closesocket(s) == 0)

         printf("closesocket() should be fine!\n");

    else

         printf("closesocket() failed with error code %d\n", WSAGetLastError());

    if(WSACleanup() == 0)

         printf("WSACleanup() is OK!\n");

    else

         printf("WSACleanup() failed with error code %d\n", WSAGetLastError());

    return 0;

}

 

Build and run the project.

 

--------------------------------------------------------------------

Winsock 2 socket options and ioctls: The SO_PROTOCOL_INFO option program example - a sample output

 

The SO_RCVTIMEO Option Program Example

 

Create a new empty Win32 console mode application and add the project/solution name.

 

Winsock 2 socket options and ioctls: The SO_RCVTIMEO option program example

 

Add the following source code.

 

// Description:

//    This is a trivial sample that sets the receive timeout

//    option and then attempts to receive a datagram which will fail with a timeout message.

//

// No command line arguments

//

// Link to ws2_32.lib

#include <windows.h>

#include <winsock.h>

#include <stdio.h>

 

// Function: main

// Description:

//    Load Winsock, create a UDP socket, set the timeout value,

//    and then call recvfrom() will will fail with a timeout since no data is being sent.

int main(int argc, char **argv)

{

    WSADATA       wsd;

    SOCKADDR_IN   from;

    SOCKET        s;

    int           ret, iVal=0, fromsz;

    unsigned int  sz = sizeof(iVal);

    char          rcvbuf[1024];

 

    // Load Winsock

    if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)

    {

        printf("WSAStartup() failed with error code %d\n", WSAGetLastError());

        return -1;

     }

     else

        printf("WSAStartup() should be OK!\n");

 

    // Create a datagram socket

    s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

    if (s == INVALID_SOCKET)

    {

        printf("socket() failed with error code %d\n", WSAGetLastError());

        return -1;

    }

    else

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

 

    // Set the timeout value

    iVal = 1000;

    ret = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *)&iVal, sz);

    if (ret == SOCKET_ERROR)

    {

        printf("setsockopt() failed with error code %d\n", WSAGetLastError());

        return -1;

    }

    else

        printf("setsockopt() is OK!\n");

 

    printf("Timeout was set...\n");

 

    // Retrieve the value just to be sure

    printf("Do a verification...\n");

    ret = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *)&iVal, &sz);

    if (ret == SOCKET_ERROR)

    {

        printf("getsockopt() failed with error code %d\n", WSAGetLastError());

        return -1;

    }

    else

        printf("getsockopt() is fine!\n");

 

    printf("Timeout value == %d\n", iVal);

 

    // To test the receive function we need to bind first

    from.sin_family = AF_INET;

    from.sin_addr.s_addr = htonl(INADDR_ANY);

    from.sin_port = htons(5150);

 

    ret = bind(s, (SOCKADDR *)&from, sizeof(from));

    if (ret == SOCKET_ERROR)

    {

        printf("bind() failed: %d\n", WSAGetLastError());

        return -1;

    }

 

    // Call receive which will fail with WSAETIMEDOUT

    fromsz = sizeof(from);

    ret = recvfrom(s, rcvbuf, 1024, 0, (SOCKADDR *)&from, &fromsz);

    if (ret == SOCKET_ERROR)

    {

        if (WSAGetLastError() == WSAETIMEDOUT)

            printf("recvfrom() failed with error code 10060 (WSAETIMEDOUT)\n");

        else

            printf("recvfrom() failed with error code %d\n", WSAGetLastError());

    }

    else

        printf("recvfrom() looks fine!\n");

    if(closesocket(s) == 0)

        printf("closesocket() should be fine!\n");

    else

        printf("closesocket() failed with error code %d\n", WSAGetLastError());

     if(WSACleanup() == 0)

         printf("WSACleanup() is OK!\n");

     else

         printf("WSACleanup() failed with error code %d\n", WSAGetLastError());

    return 0;

}

 

Build and run the project.

 

Winsock 2 socket options and ioctls: The SO_RCVTIMEO option program example - a sample output

 

The SO_TYPE Option Program Example

 

Create a new empty Win32 console mode application and add the project/solution name.

 

Winsock 2 socket options and ioctls: The SO_TYPE option program example

 

Add the following source code.

 

// Description:

//    This sample creates a socket and then calls the SO_TYPE option

//    to retrieve the socket type (SOCK_STREAM, SOCK_DGRAM, etc) for that socket.

//

// Command line arguments

//

// Link to ws2_32.lib

#include <winsock2.h>

#include <ws2tcpip.h>

#include <stdio.h>

 

// Function: main

// Description: Load Winsock, create a socket, and then call the SO_TYPE option and print the results.

int main(int argc, char **argv)

{

    WSADATA       wsd;

    SOCKET        s;

    int           ret, iVal, iSize;

 

    // Load Winsock

    if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)

    {

        printf("WSAStartup() failed with error code %d\n", WSAGetLastError());

        return -1;

    }

    else

        printf("WSAStartup() should be fine!\n");

 

    // In this case create a UDP SOCK_DGRAM socket

    s = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, WSA_FLAG_OVERLAPPED);

    if (s == INVALID_SOCKET)

    {

        printf("WSASocket() failed with error code %d\n", WSAGetLastError());

        return -1;

    }

    else

        printf("WSASocket() is OK!\n");

 

    // Retrieve the type and print its value

    iSize = sizeof(iVal);

    ret = getsockopt(s, SOL_SOCKET, SO_TYPE, (char *)&iVal, &iSize);

    if (ret == SOCKET_ERROR)

    {

        printf("getsockopt(SO_TYPE) failed with error code %d\n", iVal);

        return -1;

    }

    else

        printf("getsockopt(SO_TYPE) is pretty fine!\n");

 

    printf("Protocol type: ");

 

    if (iVal == SOCK_STREAM)

        printf("SOCK_STREAM\n");

    else if (iVal == SOCK_DGRAM)

        printf("SOCK_DGRAM\n");

    else if (iVal == SOCK_RDM)

        printf("SOCK_RDM\n");

    else if (iVal == SOCK_SEQPACKET)

        printf("SOCK_SEQPKACKET\n");

    else if (iVal == SOCK_RAW)

        printf("SOCK_RAW\n");

    else

        printf("Unknown\n");

 

    // Clean-up

    if(closesocket(s) == 0)

         printf("closesocket() should be fine!\n");

    else

         printf("closesocket() failed with error code %d\n", WSAGetLastError());

    if(WSACleanup() == 0)

         printf("WSACleanup() is OK!\n");

    else

         printf("WSACleanup() failed with error code %d\n", WSAGetLastError());

    return 0;

}

 

Build and run the project.

 

Winsock 2 socket options and ioctls: The SO_TYPE option program example - a sample output

 

 

 


< SIO_ROUTING_INTERFACE_QUERY, SO_ACCEPTCONN & SO_BROADCAST Examples | Socket Options & IOCTLs Main | Chap 8: Name Registration & Resolution >