< SIO_RCVALL, SIO_RCVALL_MCAST & SIO_RCVALL_IGMPMCAST Examples | Socket Options & IOCTLs Main | Some More SO Program Examples >


 

 

Socket Options and Ioctls 7 part 9

 

 

What do we have in this chapter 7 part 9?

  1. Using the SIO_ROUTING_INTERFACE_QUERY Option Program Example

  2. The SO_ACCEPTCONN Option Program Example

  3. The Broadcast Data and SO_BROADCAST Option Program Example

 

Using the SIO_ROUTING_INTERFACE_QUERY Option Program Example

 

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

 

Winsock 2 socket options and ioctls: Using the SIO_ROUTING_INTERFACE_QUERY option program example

 

Add the following source code.

 

// Description:

//    This sample illustrates how to use the SIO_ROUTING_INTERFACE_QUERY

//    option. This takes a destination IP address and port and calls

//    the interface query to determine which local IP interface should

//    be used to transmit the data on. This is a Windows 2000 specific option.

//

// Command Line Arguments/Parameters

//    Sio_routing_interface_query Remote-IP Remote-Port

//      Remote-IP      Address of remote machine interested in

//      Remote-Port    Port of remote machine interested in

//

// Link to ws2_32.lib

#include <winsock2.h>

#include <ws2tcpip.h>

#include <stdio.h>

 

// Function: main

// Description:

//    Load Winsock and parse the command line. Then create a

//    socket and perform the routing query.  Print out the route that should be taken.

int main(int argc, char **argv)

{

    WSADATA       wsd;

    SOCKET        s;

    SOCKADDR_IN   input, *lpIf=NULL;

    DWORD         dwBytesRet=0, nRet, i;

    char          buf[1024];

    int           ret;

 

    // Parse the command line and load Winsock

    if (argc != 3)

    {

        printf("Usage: %s remote-ip remote-port\n", argv[0]);

        printf("Example: %s 209.131.36.158 80\n", argv[0]);

        return -1;

    }

 

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

    {

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

        return -1;

    }

    else

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

 

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

 

    // Perform the query on the given IP address

    input.sin_family = AF_INET;

    input.sin_addr.s_addr = inet_addr(argv[1]);

    input.sin_port = htons((short)atoi(argv[2]));

 

    ret = WSAIoctl(s, SIO_ROUTING_INTERFACE_QUERY, &input, sizeof(input), buf, 1024, &dwBytesRet, NULL, NULL);

    if (ret == SOCKET_ERROR)

    {

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

        return -1;

    }

    else

        printf("WSAIoctl(SIO_ROUTING_INTERFACE_QUERY) is fine!\n");

 

    printf("bytes returned: %d\n", dwBytesRet);

 

    nRet = dwBytesRet / sizeof(SOCKADDR_IN);

    lpIf = (SOCKADDR_IN *)buf;

 

    // Print out the interface information

    for(i=0; i < nRet ;i++)

    {

        printf("Interface:\n   sin_family = %d\n   sin_addr   = %s\n   sin_port   = %d\n", lpIf[i].sin_family, inet_ntoa(lpIf[i].sin_addr), lpIf[i].sin_port);

    }

 

    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: Using the SIO_ROUTING_INTERFACE_QUERY option program example - sample output without arguments

 

Testing the program with the following argument (IP address):

Sio_routing_interface_query 209.131.36.158 80

Winsock 2 socket options and ioctls: Using the SIO_ROUTING_INTERFACE_QUERY option program example - sample output without arguments - sample output with an IP address as the argument

 

 

The SO_ACCEPTCONN 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_ACCEPTCONN option program example

 

Add the source code.

 

// Description:

//    This module illustrates the SO_ACCEPTCONN option to find

//    out whether a socket is listening for connections.

//

// No command line arguments

//

// Link to ws2_32.lib

#include <winsock2.h>

#include <stdio.h>

 

// Function: main

// Description: Loads Winsock, creates a listening socket and get the state of SO_ACCEPTCONN.

int main(int argc, char **argv)

{

    WSADATA       wsd;

    SOCKET        s;

    SOCKADDR_IN   local;

    int           ret, iSize;

    BOOL          bOpt;

 

    // Load Winsock

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

    {

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

        return -1;

    }

    else

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

 

    // Creat a socket, bind to it, and listen

    s = socket(AF_INET, SOCK_STREAM, 0);

    if (s == INVALID_SOCKET)

    {

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

        return -1;

    }

    else

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

 

    local.sin_family = AF_INET;

    local.sin_addr.s_addr = INADDR_ANY;

    local.sin_port = htons(5150);

 

    iSize = sizeof(bOpt);

    ret = getsockopt(s, SOL_SOCKET, SO_ACCEPTCONN, (char *)&bOpt, &iSize);

    if (ret == SOCKET_ERROR)

    {

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

        return -1;

    }

    else

        printf("getsockopt(SO_ACCEPTCONN) is OK!\n");

    if (bOpt == TRUE)

        printf("getsockopt(SO_ACCEPTCONN) returned TRUE!\n");

    else

        printf("getsockopt(SO_ACCEPTCONN) returned FALSE!\n");

    if (bind(s, (SOCKADDR *)&local, sizeof(local)) == SOCKET_ERROR)

    {

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

        return -1;

    }

    else

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

 

    listen(s, 7);

 

    // Get the option value

    iSize = sizeof(bOpt);

    ret = getsockopt(s, SOL_SOCKET, SO_ACCEPTCONN, (char *)&bOpt, &iSize);

    if (ret == SOCKET_ERROR)

    {

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

        return -1;

    }

 

    if (bOpt == TRUE)

        printf("getsockopt(SO_ACCEPTCONN) returned TRUE!\n");

    else

        printf("getsockopt(SO_ACCEPTCONN) returned FALSE!\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_ACCEPTCONN option program example - a sample output

 

The Broadcast Data and SO_BROADCASt Option Program Example

 

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

 

Winsock 2 socket options and ioctls: The broadcast data and SO_BROADCASt option program example

 

Add the following source code.

 

// Description:

//    This sample illustrates how to send and receive

//    broadcast data. The SO_BROADCASt option only needs

//    to be set on the sending socket; however in reality,

//    the Microsoft provider's don't check to see if the

//    option is set. In other words you could send data

//    to INADDR_BROADCAST without the option set and no error would occur.

//

// Command line arguments

//    SO_BROADCAST s|r

//      s      Send broadcast data

//      r      Receive data

//

// Link to ws2_32.lib

#include <winsock2.h>

#include <ws2tcpip.h>

#include <stdio.h>

 

// Function: main

// Description:

//    The main function does everything: loads the Winsock library,

//    parses the arguments, sets the broadcast option, and either

//    sends or receives data depending on the command line options.

int main(int argc, char **argv)

{

    WSADATA       wsd;

    SOCKET        s;

    SOCKADDR_IN   to;

    int           ret, fromsz;

    BOOL          bOpt,bSender;

    char         *msg="This is a test string from sender", rcvbuf[1024];

 

    // Check for arguments

    if (argc != 2)

    {

        printf("Usage: %s s|r\n", argv[0]);

        printf("    s  =  send\n");

        printf("    r  =  receive\n");

        printf("Example: %s s\n", argv[0]);

        return -1;

    }

 

    if (tolower(argv[1][0]) == 's')

        bSender = TRUE;

    else

        bSender = FALSE;

 

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

    // Set the broadcast option (really only necessary for the sender

    bOpt = TRUE;

    ret = setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char *)&bOpt, sizeof(bOpt));

    if (ret == SOCKET_ERROR)

    {

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

        return -1;

    }

    else

        printf("setsockopt(SO_BROADCAST) should be OK!\n");

 

    if (bSender)

    {

        // Send some data

        to.sin_family = AF_INET;

        to.sin_addr.s_addr = htonl(INADDR_BROADCAST);

        to.sin_port = htons(5150);

 

        ret = sendto(s, msg, strlen(msg), 0, (SOCKADDR *)&to, sizeof(to));

        if (ret == SOCKET_ERROR)

        {

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

            return -1;

        }

        else

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

    }

    else

    {

        // Receive data so we must bind first

        to.sin_family = AF_INET;

        to.sin_addr.s_addr = htonl(INADDR_ANY);

        to.sin_port = htons(5150);

 

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

        if (ret == SOCKET_ERROR)

        {

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

            return -1;

        }

        else

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

        // Now we can read data

        fromsz = sizeof(to);

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

        if (ret == SOCKET_ERROR)

        {

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

            return -1;

        }

        else

            printf("recvfrom() is pretty fine!\n");

        rcvbuf[ret] = 0;

        printf("Read: '%s' from %s\n", rcvbuf, inet_ntoa(to.sin_addr));

    }

 

    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 broadcast data and SO_BROADCASt option program example - sample output without argument

 

Run the program with r option (receiver).

 

Winsock 2 socket options and ioctls: The broadcast data and SO_BROADCASt option program example - sample output with r argument

 

Then, run the program with s option (sender).

 

Winsock 2 socket options and ioctls: The broadcast data and SO_BROADCASt option program example - sample output with s argument

 

The previous receiver program sample output is shown below when communication was completed.

 

Winsock 2 socket options and ioctls: The broadcast data and SO_BROADCASt option program example - sample output with r argument when communication was completed

 

 

 


< SIO_RCVALL, SIO_RCVALL_MCAST & SIO_RCVALL_IGMPMCAST Examples | Socket Options & IOCTLs Main | Some More SO Program Examples >