< SIO_GET_ATM_ADDRESS etc Example | Socket Options & IOCTLs Main | SIO_RCVALL, SIO_RCVALL_MCAST & SIO_RCVALL_IGMPMCAST Examples >


 

 

Socket Options and Ioctls 7 part 7

 

 

What do we have in this chapter 7 part 7?

  1. The SIO_GET_BROADCAST_ADDRESS Program Example

  2. Setting the SO_KEEPALIVE_VALS on a Socket Program Example

 

The SIO_GET_BROADCAST_ADDRESS Program Example

 

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

 

Winsock 2 socket options and ioctls: The SIO_GET_BROADCAST_ADDRESS program example

 

Add the following source code.

 

// Description: This sample shows how to obtain the broadcast address for a UDP socket.

//

// No command line arguments/parameters

//

// Link to ws2_32.lib

#include <winsock2.h>

#include <ws2tcpip.h>

#include <stdio.h>

 

// Function: main

// Description: Load Winsock, create a socket and find the broadcast address

int main(int argc, char **argv)

{

    WSADATA       wsd;

    SOCKET        s;

    SOCKADDR_IN   bcast;

    int           ret;

    DWORD         dwBytesRet;

 

    // 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");

 

    // Create a 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");

 

    // Call the ioctl

    ret = WSAIoctl(s, SIO_GET_BROADCAST_ADDRESS, NULL, 0,&bcast, sizeof(bcast), &dwBytesRet, NULL, NULL);

    if (ret == SOCKET_ERROR)

    {

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

        return -1;

    }

    else

        printf("WSAIoclt(SIO_GET_BROADCAST_ADDRESS) should be fine!\n");

 

    printf("Broadcast address is: '%s'\n", inet_ntoa(bcast.sin_addr));

 

    // Cleanup

    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 SIO_GET_BROADCAST_ADDRESS program example - sample output

 

Setting the SO_KEEPALIVE_VALS  on a Socket Program Example

 

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

 

Winsock 2 socket options and ioctls: Setting the SO_KEEPALIVE_VALS  on a socket program example

 

Add the following source code.

 

// Description:

//    This sample shows you how to set the SO_KEEPALIVE_VALS

//    on a socket. This option allows you to set the keepalive

//    interval on a per socket basis as opposed to the rather

//    useless SO_KEEPALIVE option.

//

// No command line arguments

//

// Link to ws2_32.lib

#include <winsock2.h>

#include <ws2tcpip.h>

#include <Mstcpip.h>

#include <stdio.h>

 

int main(int argc, char **argv)

{

    WSADATA       wsd;

    SOCKET        s;

    DWORD         dwBytesRet=0;

    struct tcp_keepalive   alive;

 

    // Load Winsock lib

    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");

 

    // Create a 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 fine!\n");

 

    // Set the keepalive values

    alive.onoff = TRUE;

    alive.keepalivetime = 7200000;

    alive.keepaliveinterval = 6000;

 

    if (WSAIoctl(s, SIO_KEEPALIVE_VALS, &alive, sizeof(alive), NULL, 0, &dwBytesRet, NULL, NULL) == SOCKET_ERROR)

    {

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

        return -1;

    }

    else

        printf("WSAIotcl(SIO_KEEPALIVE_VALS) is working!\n");

 

    printf("SIO_KEEPALIVE_VALS set:\n");

    printf("   Keepalive Time     = %lu\n", alive.keepalivetime);

    printf("   Keepalive Interval = %lu\n", alive.keepaliveinterval);

 

    // Cleanup

    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: Setting the SO_KEEPALIVE_VALS  on a socket program example - sample output

 

 

 


< SIO_GET_ATM_ADDRESS etc Example | Socket Options & IOCTLs Main | SIO_RCVALL, SIO_RCVALL_MCAST & SIO_RCVALL_IGMPMCAST Examples >