windows qos api使用示例

news2024/11/17 10:52:35

简介

本文给出C++版以及Rust版调用windows API实现QOS的示例,并提出一些注意事项。QOS可以用来区分socket 优先级、实现带宽控制等诸多功能。

UDP版本

该示例的重要部分是客户端应用程序。客户端代码的工作方式如下:

1. 通过套接字连接到目标 IP 地址。
2. 通过调用 QOSCreateHandle 初始化 QOS 子系统。
3. 通过调用 QOSAddSocketToFlow 为套接字创建一个流。
4. 通过调用 QOSQueryFlow 查询源主机和目标主机之间路径的特性。使用的两个估计值是:

- 瓶颈带宽:最大理论吞吐量;
- 可用带宽:新流量的当前可用吞吐量。

根据网络配置的不同:

A. 可能进行带宽估算。例如,目标可能在另一个子网上。如果是这样,客户端将仅进行流量整形并根据指定的流量类型标记优先级,但永远无法接收可用带宽的通知或估计,而出站流量将根据 DLNA 规范进行标记。
B. QOS 子系统可能没有足够的时间来估计到目的地主机的条件。如果发生这种情况,客户端将在重试之前休眠 1 秒。在生产应用程序中,您可以通过在 A/V 流量开始之前调用 QOSStartTrackingClient API 来最小化发生这种情况的可能性。建议在目标与客户端位于同一子网上时始终使用此 API。

5. 如果可用带宽的估计值小于请求的带宽,则客户端将修订请求的带宽。
6. 客户端为我们的流设置了一个固定速率,该速率将限制出站流量到请求的带宽。为此,它考虑了地址族和使用的协议(UDP)的线路开销。
7. 客户端注册了拥塞通知。
8. 使用形状机制作为控制循环以期望速率发送流量。应用程序使用 TransmitPackets API 调用发送数据包。客户端利用形状器仅在所有数据包离开主机时才完成此 Winsock 传输操作的事实,以确定一次发送多少个数据包。
9. 当外部条件将网络推入拥塞状态时,将接收到拥塞通知。作为反应,客户端将将目标比特率降低到初始值的十分之一。此外,客户端将在可用带宽足以返回到先前的比特率时请求通知。

代码如下:

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
   ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
   PARTICULAR PURPOSE.

   Copyright (c) Microsoft Corporation. All rights reserved.

Module Name:

    qossample.c

Abstract:

    This program implements a simple traffic generator which uses the QOS2
    api to not overload the network path between the source and its destination.

    Although the application can be run as a receiver to process the traffic
    generated, the client side of the application is the important part of this
    sample. This code is in the Client function.

    The code for the client works as follows:

    1) A socket is connected to the destination IP address
    2) The QOS subsystem is initialized by a call to QOSCreateHandle
    3) A flow is created for the socket by a call to QOSAddSocketToFlow
    4) The characteristics of the path between the source host and destination
       host are queried through QOSQueryFlow. The two estimates used are:

        - the bottleneck bandwidth: the maximum theoretical throughput;
        - and the available bandwidth: the currently available throughput for
          new traffic

       Depending on your network configuration:

       A) It may be impossible to run network experiments to the destination.
          For example, the destination might be on another subnet. If so, the
          client will only shape traffic and mark priority with DSCP based on
          the specified traffic type, but will never be able to receive
          notifications or estimates of available bandwidth and outgoing traffic
          would be marked per the DLNA specification.
       B) The QOS subsystem may not have had enough time to estimate conditions
          to the destination host. If this happens, the client will sleep for 1s
          before retrying. In a production application you could minize the
          likelihood of this happening by calling, before the start of your
          A/V traffic, the QOSStartTrackingClient API. It is suggested that this
          API always be used when the destination is on the same subnet as the
          client.

    5) If the estimate of available bandwidth is less than the requested
       bandwidth, the client revises the requested bandwidth.
    6) The client sets a shaping rate for our flow that will limit the outgoing
       traffic to the requested bandwidth. To do so, it accounts for the wire
       overhead given both the address family as well as the protocol used, UDP.
    7) The client registers for notifications of congestion.
    8) Traffic is sent at the desired rate using the shaping mechanism as the
       control loop. The application uses the API call TransmitPackets to
       send the packet bunches. The client uses the fact that the shaper will
       only complete this Winsock transmit operation when all packets have left
       the host to determine how many packets to send at a time.
    9) A congestion notification will be received whenever external conditions
       push the network into a congested state. The client, as a reaction, will
       lower the targeted bit rate to one-tenth of the initial value. Moreover,
       the client will request notification when the bandwidth available is
       enough to return to the previous bit rate.

Environment:

    User-Mode only

Revision History:

------------------------------------------------------------------------------*/

#include <winsock2.h>
#include <mswsock.h>
#include <stdlib.h>
#include <stdio.h>
#include <qos2.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "QWave.lib")
#pragma warning(disable:4127)   // condition expression is constant

//******************************************************************************
// Forward declarations
//******************************************************************************
VOID
client(
    __in                int     argc,
    __in_ecount(argc)   char* argv[]
);

VOID
server();

VOID
help();

//******************************************************************************
// Global defines
//******************************************************************************

// Size of the data part of each datagram that is exchanged between the
// client and the server. This does not count the IP and UDP header.
#define DATAGRAM_SIZE       1400

// Number of bursts we are aiming for per second
#define BURSTS_PER_SECOND   40

// Port to be used for communication, 
// 40007 in hex in network byte order 
#define PORT                0x479c

//******************************************************************************
// Global variables
//******************************************************************************

// Array used by both the client routine and the server routine to
// send and receive data
CHAR                dataBuffer[DATAGRAM_SIZE];

// Version of the QOS subsystem we wish to use
QOS_VERSION QosVersion = { 1 , 0 };

//******************************************************************************
// Routine: 
//      main
//
// Description:
//      Entry point. Verifies that the number of parameters is enough to 
//      ascertain whether this instance is to be a client or server.
// 
//******************************************************************************
int
_cdecl
main(
    __in                int argc,
    __in_ecount(argc)   char* argv[]
)
{
    // Verify the number of command line arguments
    if (argc < 2) {
        help();
        exit(0);
    }

    if (strcmp(argv[1], "-s") == 0) {
        // If the call was "qossample -s" run as a server
        server();
    }
    else if (strcmp(argv[1], "-c") == 0) {
        // If the call was "qossample -c ..." run as a client
        client(argc, argv);
    }

    // Else, run the help routine
    help();
    return 1;
}

//******************************************************************************
// Routine: 
//      socketCreate
//
// Description:
//      This routine prepares the socket for the client instance. To do so,
//      it converts the address parameter from string to IP address. Then
//      it creates a UDP socket which it connects to this destination.
//
//      Since we will use TransmitPackets for our send operations, the function
//      pointer is obtained from Winsock.
// 
//******************************************************************************
VOID
socketCreate(
    __in    PCHAR                       destination,
    __out   SOCKET* socket,
    __out   ADDRESS_FAMILY* addressFamily,
    __out   LPFN_TRANSMITPACKETS* transmitPackets
) {
    // Return value of WSAStartup
    WSADATA             wsaData;

    // Temporarily used to represent the destination IP address
    SOCKADDR_STORAGE    destAddr;

    // Result of the various Winsock API calls
    INT                 returnValue;

    // Used by WSAStringToAddressA
    INT                 sockaddrLen;

    // Used by WSAIoctl
    DWORD               bytesReturned;

    // GUID of the TransmitPacket Winsock2 function which we will
    // use to send the traffic at the client side.
    GUID TransmitPacketsGuid = WSAID_TRANSMITPACKETS;

    // Start Winsock
    returnValue = WSAStartup(MAKEWORD(2, 2), &wsaData);

    if (returnValue != 0) {
        printf("%s:%d - WSAStartup failed (%d)\n",
            __FILE__, __LINE__, returnValue);
        exit(1);
    }

    // First attempt to convert the string to an IPv4 address
    sockaddrLen = sizeof(destAddr);
    destAddr.ss_family = AF_INET;
    returnValue = WSAStringToAddressA(destination,
        AF_INET,
        NULL,
        (LPSOCKADDR)&destAddr,
        &sockaddrLen);

    if (returnValue != ERROR_SUCCESS) {
        // If this fails,
        // Attempt to convert the string to an IPv6 address
        sockaddrLen = sizeof(destAddr);
        destAddr.ss_family = AF_INET6;
        returnValue = WSAStringToAddressA(destination,
            AF_INET6,
            NULL,
            (LPSOCKADDR)&destAddr,
            &sockaddrLen);

        if (returnValue != ERROR_SUCCESS) {
            // If this again fails, exit the application
            // But print out the help information first.
            printf("%s:%d - WSAStringToAddressA failed (%d)\n",
                __FILE__, __LINE__, WSAGetLastError());
            help();
            exit(1);
        }
    }

    // Set the destination port.
    SS_PORT((PSOCKADDR)&destAddr) = PORT;

    // Copy the address family back to caller
    *addressFamily = destAddr.ss_family;

    // Create a UDP socket
    *socket = WSASocket(destAddr.ss_family,
        SOCK_DGRAM,
        0,
        NULL,
        0,
        WSA_FLAG_OVERLAPPED);

    if (*socket == INVALID_SOCKET) {
        printf("%s:%d - WSASocket failed (%d)\n",
            __FILE__, __LINE__, WSAGetLastError());
        exit(1);
    }

    // Connect the new socket to the destination
    returnValue = WSAConnect(*socket,
        (PSOCKADDR)&destAddr,
        sizeof(destAddr),
        NULL,
        NULL,
        NULL,
        NULL);

    if (returnValue != NO_ERROR) {
        printf("%s:%d - WSAConnect failed (%d)\n",
            __FILE__, __LINE__, WSAGetLastError());
        exit(1);
    }

    // Query the function pointer for the TransmitPacket function
    returnValue = WSAIoctl(*socket,
        SIO_GET_EXTENSION_FUNCTION_POINTER,
        &TransmitPacketsGuid,
        sizeof(GUID),
        transmitPackets,
        sizeof(PVOID),
        &bytesReturned,
        NULL,
        NULL);

    if (returnValue == SOCKET_ERROR) {
        printf("%s:%d - WSAIoctl failed (%d)\n",
            __FILE__, __LINE__, WSAGetLastError());
        exit(1);
    }
}

//******************************************************************************
// Routine: 
//      client
//
// Description:
//      This routine creates a datagram socket which it connects to the remote
//      IP address. This socket is then added to a QOS flow. The application
//      uses the flow to rate control its outgoing traffic. Packets on this flow
//      will be prioritized if the network path between the client and receiver
//      support prioritization. Specifically, if the receiver is:
//
//      A) On-link (same subnet), both 802.1Q and DSCP are applied subject to
//         available bandwidth and network support.
//      B) Off-link (different subnet), only DSCP is applied regardless of
//         available bandwidth.
//
//      Moreover, the application queries the characteristics of the network 
//      path for the socket. If estimates are available, the application:
//
//      1) may readjust its throughput so as to not cause congestion on the 
//         network.
//      2) will be notified when the network enters congestion. As a result,
//         it will lower it's throughput to 1/10 the targeted rate.
//      3) will be notified when the network is no longer congested and enough
//         bandwidth is available for the application to return to its targeted
//         rate.
//
//******************************************************************************
VOID
client(
    __in                int     argc,
    __in_ecount(argc)   char* argv[]
) {
    // Address family of the datagram socket
    ADDRESS_FAMILY              addressFamily;

    // Socket for our traffic experiment
    SOCKET                      socket;

    // Function pointer to the TransmitPacket Winsock2 function
    LPFN_TRANSMITPACKETS        transmitPacketsFn;
    // Array of transmit elements
    LPTRANSMIT_PACKETS_ELEMENT  transmitEl;

    // Target packet rate and bit rate this application will aim to send
    // If there is no congestion
    ULONG                       targetPacketRate, targetBitRate;
    // Current rate at which the application is sending. If the network is
    // congested, this will be less than the target rate
    ULONG                       currentPacketRate, currentBitRate;
    // Counters for the achieved packet rate and achieved bit rate
    ULONG                       achievedPacketRate, achievedBitRate;

    // Timer; used to periodically output to the screen our counters
    HANDLE                      timerEvent;
    LARGE_INTEGER               timerAwakenTime;

    // Handle to the QOS subsystem
    // Returned by QOSCreateHandle
    HANDLE                      qosHandle;
    // ID of the QOS flow we will create for the socket. 
    // Returned by QOSAddSocketToFlow
    QOS_FLOWID                  flowID;
    // Result of QOSQueryFlow
    QOS_FLOW_FUNDAMENTALS       flowFund;
    // Parameter to QOSSetFlow
    QOS_FLOWRATE_OUTGOING       flowRate;
    // If TRUE, the QOS subsystem is running network experiments on the
    // network path to the destination. If false, estimates are not available.
    // The flow is still marked and shaped.
    BOOL                        estimatesAvailable;
    // Result of the QOS API calls.
    BOOL                        result;

    // Overlapped operation used for TransmitPackets
    WSAOVERLAPPED               sendOverlapped;
    // Overlapped operation used for QOSNotifyFlow to be notified
    // of network congestions.
    OVERLAPPED                  congestionOverlapped;
    // Overlapped operation used for QOSNotifyFlow to be notified when, 
    // after congestion, there is enough bandwidth for the target rate
    OVERLAPPED                  availableOverlapped;
    // TRUE if the network is currently congested
    BOOL                        congestion;

    ULONG                       temp;

    // Verify the number of command line arguments
    if (argc != 4) {
        help();
        exit(1);
    }

    // Identify what destination IP address we're trying to talk to and
    // connect a UDP socket to it
    socketCreate(argv[2],
        &socket,
        &addressFamily,
        &transmitPacketsFn);

    printf("socket resut %d\n", socket);

    if (FALSE == QOSCreateHandle(&QosVersion, &qosHandle)) {
        printf("%s:%d - QOSCreateHandle failed (%d)\n",
            __FILE__, __LINE__, GetLastError());
        exit(1);
    }

    // The Flow ID parameter MUST be set to 0 as input to QOSAddSocketToFlow
    flowID = 0;

    // Create a flow for our socket
    result = QOSAddSocketToFlow(qosHandle,
        socket,
        NULL,
        QOSTrafficTypeVoice,
        QOS_NON_ADAPTIVE_FLOW,
        &flowID);

    if (result == FALSE) {
        printf("%s:%d - QOSAddSocketToFlow failed (%d)\n",
            __FILE__, __LINE__, GetLastError());
        exit(1);
    }

    // Read the data rate in bits/s passed on the command line
    targetBitRate = atol(argv[3]);

    if (targetBitRate == 0) {
        help();
        exit(1);
    }

    // Convert from bits to bytes
    targetBitRate /= 8;

    // Calculate how many packets we need; round up.
    targetPacketRate = targetBitRate / ARRAYSIZE(dataBuffer);
    if (targetBitRate % ARRAYSIZE(dataBuffer) != 0)
        targetPacketRate++;

    // Calculate the number of packets per bursts; round up
    if (targetPacketRate % BURSTS_PER_SECOND != 0) {
        targetPacketRate /= BURSTS_PER_SECOND;
        targetPacketRate++;
    }
    else {
        targetPacketRate /= BURSTS_PER_SECOND;
    }

    // Calculate the final bit rate, targetBitRate, in bps that the application
    // will send.
    targetBitRate = BURSTS_PER_SECOND
        * targetPacketRate
        * ARRAYSIZE(dataBuffer)
        * 8;

    //
    // Allocate an array of transmit elements big enough to send 
    // targetPacketRate packets every burst.
    transmitEl = static_cast<LPTRANSMIT_PACKETS_ELEMENT>( HeapAlloc(GetProcessHeap(),
        HEAP_ZERO_MEMORY,
        sizeof(TRANSMIT_PACKETS_ELEMENT)
        * targetPacketRate));

    if (transmitEl == NULL) {
        printf("%s:%d - HeapAlloc failed (%d)\n",
            __FILE__, __LINE__, GetLastError());
        exit(1);
    }


    // For the purpose of this application, we only use one data buffer for all 
    // of our packets.
    ZeroMemory(&dataBuffer, sizeof(dataBuffer));

    //
    // Initialize each of these transmit element to point to the same zeroed out
    // data buffer
    for (temp = 0; temp < targetPacketRate; temp++) {
        transmitEl[temp].dwElFlags = TP_ELEMENT_MEMORY | TP_ELEMENT_EOP;
        transmitEl[temp].pBuffer = dataBuffer;
        transmitEl[temp].cLength = sizeof(dataBuffer);
    }

    // Print out what we'll be doing
    printf("----------------------------------"
        "----------------------------------\n"
        "This instance of the QOS sample program is aiming to:\n"
        "\t - Send %d bits of UDP traffic per second\n"
        "\t - In packets of %d bytes\n"
        "\t - In bursts of %d packets every %d milliseconds\n\n"
        "----------------------------------"
        "----------------------------------\n",
        targetBitRate,
        ARRAYSIZE(dataBuffer),
        targetPacketRate,
        1000 / BURSTS_PER_SECOND);

    // Assume, by default, that estimates are not available
    estimatesAvailable = FALSE;

    printf("Querying fundamentals about the network path: ");
    do {
        temp = sizeof(flowFund);

        // Query the flow fundamentals for the path to the destination.
        // This will return estimates of the bottleneck bandwidth, available
        // bandwidth and average RTT.
        result = QOSQueryFlow(qosHandle,
            flowID,
            QOSQueryFlowFundamentals,
            &temp,
            &flowFund,
            0,
            NULL);

        if (result == FALSE) {
            DWORD lastError;

            lastError = GetLastError();

            if (lastError == ERROR_NO_DATA) {
                // If the call failed, this could be because the QOS subsystem 
                // has not yet gathered enough data for estimates. If so, the
                // QOS2 api returns ERROR_NO_DATA.
                printf(".");
                Sleep(1000);
            }
            else if (lastError == ERROR_NOT_SUPPORTED)
            {
                // If the call failed, this could be because the QOS subsystem 
                // cannot run network experiments on the network path to the
                // destination. If so, the API returns ERROR_NOT_SUPPORTED.
                printf("NOT SUPPORTED\n"
                    "\t   Network conditions to this destination could not\n"
                    "\t   be detected as your network configuration is not\n"
                    "\t   supported for network experiments\n");
                break;
            }
            else {
                printf("%s:%d - QOSQueryFlow failed (%d)\n",
                    __FILE__, __LINE__, lastError);
                exit(1);
            }
        }
        else if ((flowFund.BottleneckBandwidthSet == FALSE)
            || (flowFund.AvailableBandwidthSet == FALSE)) {
            // If the call succeeded but bottleneck bandwidth or 
            // available bandwidth are not known then estimates are still
            // processing; query again in 1 second.
            printf(".");
            Sleep(1000);
        }
        else {
            // Estimate where available.
            double bottleneck;
            double available;

            estimatesAvailable = TRUE;

            // Convert the bottleneck bandwidth from bps to mbps
            bottleneck = (double)flowFund.BottleneckBandwidth;
            bottleneck /= 1000000.0;

            // Convert available bandwidth from bps to mbps
            available = (double)flowFund.AvailableBandwidth;
            available /= 1000000.0;

            printf("DONE\n"
                "\t - Bottleneck bandwidth is approximately %4.2f Mbps\n"
                "\t - Available bandwidth is approximately  %4.2f Mbps\n",
                bottleneck,
                available);

            break;
        }
    } while (TRUE);

    if (estimatesAvailable == TRUE) {
        UINT64 targetBitRateWithHeaders;

        printf("\nNOTE: the accuracy of the QOS estimates can be impacted by\n"
            "any of the following,\n\n"
            "\t - Both the network interface at the client\n"
            "\t   and at the server must be using NDIS 6 drivers.\n"
            "\t - If the server is not a Windows Vista host, verify that \n"
            "\t   it implements the LLD2 networking protocol. You can\n"
            "\t   find more information at http://www.microsoft.com.\n"
            "\t - IPSec, VPNs and enterprise class networking equipment\n"
            "\t   may interfere with network experiments.\n\n");

        // Calculate what our target bit rate, with protocol headers for 
        // IP(v4/v6) and UDP will be.
        targetBitRateWithHeaders = QOS_ADD_OVERHEAD(addressFamily,
            IPPROTO_UDP,
            ARRAYSIZE(dataBuffer),
            targetBitRate);

        if (flowFund.AvailableBandwidth < targetBitRateWithHeaders) {
            // If the estimate of available bandwidth is not sufficient for the
            // target bit rate (with headers), drop to a lesser throughput
            UINT64 availableBandwidth;

            // The estimate returned does not account for headers
            // Remove the estimated overhead for our address family and UDP.
            availableBandwidth = QOS_SUBTRACT_OVERHEAD(
                addressFamily,
                IPPROTO_UDP,
                ARRAYSIZE(dataBuffer),
                flowFund.AvailableBandwidth);

            // Calculate the rate of packets we can realistically send
            targetPacketRate = (LONG)availableBandwidth / 8;
            targetPacketRate /= ARRAYSIZE(dataBuffer);
            targetPacketRate /= BURSTS_PER_SECOND;

            // Calculate the real bit rate we'll be using
            targetBitRate = BURSTS_PER_SECOND
                * targetPacketRate
                * ARRAYSIZE(dataBuffer)
                * 8;

            printf("Not enough available bandwidth for the requested bitrate.\n"
                "Downgrading to lesser bitrate - %d.\n", targetBitRate);
        }
    }

    // Our starting rate is this target bit rate
    currentBitRate = targetBitRate;
    currentPacketRate = targetPacketRate;

    // Ask the QOS subsystem to shape our traffic to this bit rate. Note that
    // the application needs to account for the size of the IP(v4/v6) 
    // and UDP headers.

    // Calculate the real bandwidth we will need to be shaped to.
    flowRate.Bandwidth = QOS_ADD_OVERHEAD(addressFamily,
        IPPROTO_UDP,
        ARRAYSIZE(dataBuffer),
        currentBitRate);

    // Set shaping characteristics on our QOS flow to smooth out our bursty
    // traffic.
    flowRate.ShapingBehavior = QOSShapeAndMark;

    // The reason field is not applicable for the initial call.
    flowRate.Reason = QOSFlowRateNotApplicable;
    result = QOSSetFlow(qosHandle,
        flowID,
        QOSSetOutgoingRate,
        sizeof(flowRate),
        &flowRate,
        0,
        NULL);

    if (result == FALSE) {
        printf("%s:%d - QOSSetFlow failed (%d)\n",
            __FILE__, __LINE__, GetLastError());
        exit(1);
    }

    // Allocate a waitable timer. We will use this timer to periodically
    // awaken and output statistics.
    timerEvent = CreateWaitableTimer(NULL, FALSE, NULL);

    if (timerEvent == NULL) {
        printf("%s:%d - CreateWaitableTimer failed (%d)\n",
            __FILE__, __LINE__, GetLastError());
        exit(1);
    }

    //
    // Set the sampling time to 1 second
    timerAwakenTime.QuadPart = -10000000; // 1 second

    if (FALSE == SetWaitableTimer(timerEvent,
        &timerAwakenTime,
        1000,
        NULL,
        NULL,
        FALSE)) {

        printf("%s:%d - SetWaitableTimer failed (%d)\n",
            __FILE__, __LINE__, GetLastError());
        exit(1);
    }

    // Prepare the support overlapped structures to detect congestion,
    // notifications of bandwidth change and the completion of our send 
    // routines.
    ZeroMemory(&congestionOverlapped, sizeof(congestionOverlapped));
    congestionOverlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

    if (congestionOverlapped.hEvent == NULL) {
        printf("%s:%d - CreateEvent failed (%d)\n",
            __FILE__, __LINE__, GetLastError());
        exit(1);
    }

    ZeroMemory(&availableOverlapped, sizeof(availableOverlapped));
    availableOverlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

    if (availableOverlapped.hEvent == NULL) {
        printf("%s:%d - CreateEvent failed (%d)\n",
            __FILE__, __LINE__, GetLastError());
        exit(1);
    }

    ZeroMemory(&sendOverlapped, sizeof(sendOverlapped));
    sendOverlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

    if (sendOverlapped.hEvent == NULL) {
        printf("%s:%d - CreateEvent failed (%d)\n",
            __FILE__, __LINE__, GetLastError());
        exit(1);
    }

    if (estimatesAvailable == TRUE) {
        // If estimates are available, we request a notification 
        // for congestion. This notification will complete whenever network
        // congestion is detected.
        result = QOSNotifyFlow(qosHandle,
            flowID,
            QOSNotifyCongested,
            NULL,
            NULL,
            0,
            &congestionOverlapped);

        if (result == FALSE) {
            DWORD lastError;

            lastError = GetLastError();
            if (lastError != ERROR_IO_PENDING) {
                printf("%s:%d - QOSNotifyFlow failed (%d)\n",
                    __FILE__, __LINE__, lastError);
                exit(1);
            }
        }
    }

    printf("----------------------------------"
        "----------------------------------\n"
        " # packets | # of bits  |  Bottleneck "
        "|  Available |  Congestion   \n"
        "----------------------------------"
        "----------------------------------\n");

    // Initialize our counters to 0
    achievedBitRate = achievedPacketRate = 0;
    congestion = FALSE;

    do {
        BOOL sendNextGroup;

        // Send the first burst of packets
        result = (*transmitPacketsFn)(socket,
            transmitEl,
            currentPacketRate,
            0xFFFFFFFF,
            &sendOverlapped,
            TF_USE_KERNEL_APC);

        if (result == FALSE) {
            DWORD lastError;

            lastError = WSAGetLastError();
            if (lastError != ERROR_IO_PENDING) {
                printf("%s:%d - TransmitPackets failed (%d)\n",
                    __FILE__, __LINE__, GetLastError());
                exit(1);
            }
        }

        // Increase the counter of sent data
        achievedPacketRate += currentPacketRate;
        achievedBitRate += currentPacketRate * ARRAYSIZE(dataBuffer) * 8;
        sendNextGroup = FALSE;

        do {
            HANDLE          waitEvents[3];
            DWORD           waitResult;

            // Wait for any of the 3 events to complete

            // The 1 second periodic timer
            waitEvents[0] = timerEvent;

            if (congestion)
                // Notification of available bandwidth
                waitEvents[1] = availableOverlapped.hEvent;
            else
                // Notification of congestion
                waitEvents[1] = congestionOverlapped.hEvent;

            // Notification that the send operation has completed
            waitEvents[2] = sendOverlapped.hEvent;

            waitResult = WaitForMultipleObjects(ARRAYSIZE(waitEvents),
                (PHANDLE)waitEvents,
                FALSE,
                INFINITE);

            switch (waitResult) {
            case WAIT_OBJECT_0: {
                // The event for the periodic timer is set

                printf("%10d | ", achievedPacketRate);
                printf("%10d | ", achievedBitRate);

                if (estimatesAvailable) {
                    // If estimates are available for the network path
                    // query for estimates and output to the console along
                    // with our counters.

                    // Ascertained through QOSQueryFlow
                    BOOL estimateIndicatesCongestion;

                    // Default is unknown
                    estimateIndicatesCongestion = FALSE;

                    ZeroMemory(&flowFund, sizeof(flowFund));

                    temp = sizeof(flowFund);
                    QOSQueryFlow(qosHandle,
                        flowID,
                        QOSQueryFlowFundamentals,
                        &temp,
                        &flowFund,
                        0,
                        NULL);

                    if (flowFund.BottleneckBandwidthSet)
                        printf("%10d  | ", flowFund.BottleneckBandwidth);
                    else
                        printf("   NO DATA  | ");

                    if (flowFund.AvailableBandwidthSet) {
                        if (flowFund.AvailableBandwidth == 0)
                            estimateIndicatesCongestion = TRUE;

                        printf("%10d | ", flowFund.AvailableBandwidth);
                    }
                    else
                        printf("   NO DATA | ");

                    if (estimateIndicatesCongestion)
                        printf(" CONGESTION\n");
                    else
                        printf("\n");
                }
                else {
                    // Bandwidth estimates are not available
                    printf("    N/A     |     N/A    |\n");
                }

                //
                // Reset the counters
                achievedPacketRate = achievedBitRate = 0;
                break;
            }
            case WAIT_OBJECT_0 + 1: {
                // This is either a notification for congestion or 
                // for bandwidth available 

                if (congestion == FALSE) {
                    UINT64 targetBandwidthWithOverhead;
                    ULONG  bufferSize;
                    //
                    // Congestion
                    //
                    printf("----------------------------------"
                        "----------------------------------\n"
                        "CONGESTION\n"
                        "----------------------------------"
                        "----------------------------------\n");

                    //
                    // Reduce the current rate to one-tenth of the initial rate
                    // Insure you're always sending at least 1 packet per
                    // burst.
                    if (currentPacketRate < 10)
                        currentPacketRate = 1;
                    else
                        currentPacketRate /= 10;

                    // Calculate the new bit rate we'll be using
                    currentBitRate = BURSTS_PER_SECOND
                        * currentPacketRate
                        * ARRAYSIZE(dataBuffer)
                        * 8;


                    // Update the shaping characteristics on our QOS flow 
                    // to smooth out our bursty traffic.
                    flowRate.Bandwidth = QOS_ADD_OVERHEAD(
                        addressFamily,
                        IPPROTO_UDP,
                        ARRAYSIZE(dataBuffer),
                        currentBitRate);

                    flowRate.ShapingBehavior = QOSShapeAndMark;
                    flowRate.Reason = QOSFlowRateCongestion;

                    result = QOSSetFlow(qosHandle,
                        flowID,
                        QOSSetOutgoingRate,
                        sizeof(flowRate),
                        &flowRate,
                        0,
                        NULL);

                    if (result == FALSE) {
                        printf("%s:%d - QOSSetFlow failed (%d)\n",
                            __FILE__, __LINE__, GetLastError());
                        exit(1);
                    }

                    // Request a notification for when there is enough bandwidth
                    // to return to our previous targeted bit rate.
                    // This will complete only when the network is no longer
                    // congested and bandwidth is available.
                    targetBandwidthWithOverhead = QOS_ADD_OVERHEAD(
                        addressFamily,
                        IPPROTO_UDP,
                        ARRAYSIZE(dataBuffer),
                        targetBitRate);

                    bufferSize = sizeof(targetBandwidthWithOverhead);

                    result = QOSNotifyFlow(qosHandle,
                        flowID,
                        QOSNotifyAvailable,
                        &bufferSize,
                        (PVOID)&targetBandwidthWithOverhead,
                        0,
                        &availableOverlapped);

                    if (result == FALSE) {
                        DWORD lastError;

                        lastError = GetLastError();
                        if (lastError != ERROR_IO_PENDING) {
                            printf("%s:%d - QOSNotifyFlow failed (%d)\n",
                                __FILE__, __LINE__, lastError);
                            exit(1);
                        }
                    }

                    congestion = TRUE;
                }
                else {
                    //
                    // End of congestion
                    //
                    printf("----------------------------------"
                        "----------------------------------\n"
                        "END OF CONGESTION\n"
                        "----------------------------------"
                        "----------------------------------\n");

                    //
                    // Reset the current packet rate to the initial target rate
                    currentPacketRate = targetPacketRate;

                    // Reset the current bit rate to the initial target rate
                    currentBitRate = targetBitRate;


                    // Update the shaping characteristics on our QOS flow 
                    // to smooth out our bursty traffic.
                    flowRate.Bandwidth = QOS_ADD_OVERHEAD(addressFamily,
                        IPPROTO_UDP,
                        ARRAYSIZE(dataBuffer),
                        targetBitRate);

                    flowRate.ShapingBehavior = QOSShapeAndMark;
                    flowRate.Reason = QOSFlowRateCongestion;
                    result = QOSSetFlow(qosHandle,
                        flowID,
                        QOSSetOutgoingRate,
                        sizeof(flowRate),
                        &flowRate,
                        0,
                        NULL);

                    if (result == FALSE) {
                        printf("%s:%d - QOSSetFlow failed (%d)\n",
                            __FILE__, __LINE__, GetLastError());
                        exit(1);
                    }

                    // Request a notification for the next network congestion
                    result = QOSNotifyFlow(qosHandle,
                        flowID,
                        QOSNotifyCongested,
                        NULL,
                        NULL,
                        0,
                        &congestionOverlapped);

                    if (result == FALSE) {
                        DWORD lastError;

                        lastError = GetLastError();
                        if (lastError != ERROR_IO_PENDING) {
                            printf("%s:%d - QOSNotifyFlow failed (%d)\n",
                                __FILE__, __LINE__, lastError);
                            exit(1);
                        }
                    }

                    congestion = FALSE;
                }
                break;
            }
            case WAIT_OBJECT_0 + 2: {
                // The transmit packet has completed its send, 
                // If it did so successfully, it's time to send the next 
                // burst of packets.
                BOOL    overlappedResult;
                DWORD   ignoredNumOfBytesSent;
                DWORD   ignoredFlags;

                overlappedResult = WSAGetOverlappedResult(
                    socket,
                    &sendOverlapped,
                    &ignoredNumOfBytesSent,
                    FALSE,
                    &ignoredFlags);

                if (overlappedResult == FALSE) {
                    printf("%s:%d - TransmitPackets failed (%d)\n",
                        __FILE__, __LINE__, WSAGetLastError());
                    exit(1);
                }

                // Time to send out the next bunch of packets
                sendNextGroup = TRUE;
                break;
            }
            default:
                // The wait call failed.
                printf("%s:%d - WaitForMultipleObjects failed (%d)\n",
                    __FILE__, __LINE__, GetLastError());
                exit(1);
            }

        } while (sendNextGroup == FALSE);
    } while (TRUE);
}

//******************************************************************************
// Routine: 
//      server
//
// Description:
//      This routine creates a socket through which it will receive
//      any datagram sent by the client. It counts the number of packet 
//      and the number of bytes received. Periodically, it outputs this 
//      information to the console.
//
//******************************************************************************
VOID
server()
{
    HANDLE              timerEvent;
    LARGE_INTEGER       timerAwakenTime;

    // Return value of WSAStartup
    WSADATA             wsaData;

    // The socket used to receive data
    SOCKET              socket;

    // IPv6 wildcard address and port number 40007 to listen on at the server
    SOCKADDR_IN6 IPv6ListeningAddress = { AF_INET6,
                                            PORT,
                                            0,
                                            IN6ADDR_ANY_INIT,
                                            0 };

    // Result value from the various API calls
    DWORD               result;

    // Used to specify an option to setsocktopt
    ULONG               optionValue;

    // Overlapped structure used to post asynchronous receive calls
    WSAOVERLAPPED       recvOverlapped;

    // Counters of the number of bytes and packets received over 
    // a period of time.
    DWORD               numPackets, numBytes;



    // Initialize Winsock
    result = WSAStartup(MAKEWORD(2, 2), &wsaData);

    if (result != 0) {
        printf("%s:%d - WSAStartup failed (%d)\n",
            __FILE__, __LINE__, GetLastError());
        exit(1);
    }

    // Create an IPv6 datagram socket
    socket = WSASocket(AF_INET6,
        SOCK_DGRAM,
        0,
        NULL,
        0,
        WSA_FLAG_OVERLAPPED);

    if (socket == INVALID_SOCKET) {
        printf("%s:%d - WSASocket failed (%d)\n",
            __FILE__, __LINE__, WSAGetLastError());
        exit(1);
    }

    // Set IPV6_V6ONLY to FALSE before the bind operation
    // This will permit us to receive both IPv6 and IPv4 traffic on the socket.
    optionValue = FALSE;
    result = setsockopt(socket,
        IPPROTO_IPV6,
        IPV6_V6ONLY,
        (PCHAR)&optionValue,
        sizeof(optionValue));

    if (SOCKET_ERROR == result) {
        printf("%s:%d - setsockopt failed (%d)\n",
            __FILE__, __LINE__, WSAGetLastError());
        exit(1);
    }

    // Bind the socket
    result = bind(socket,
        (PSOCKADDR)&IPv6ListeningAddress,
        sizeof(IPv6ListeningAddress));

    if (result != NO_ERROR) {
        printf("%s:%d - bind failed (%d)\n",
            __FILE__, __LINE__, WSAGetLastError());
        exit(1);
    }

    // Create an event to be used for the overlapped of our
    // receive operations. The event is initialized to FALSE and set
    // to auto-reset.
    recvOverlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

    if (recvOverlapped.hEvent == NULL) {
        printf("%s:%d - CreateEvent failed (%d)\n",
            __FILE__, __LINE__, WSAGetLastError());
        exit(1);
    }

    // Create a timer event on which we will be able to wait.
    // We wish to be awaken every second to print out the count of packets
    // and number of bytes received.
    timerEvent = CreateWaitableTimer(NULL, FALSE, NULL);
    if (timerEvent == NULL) {
        printf("%s:%d - CreateWaitableTimer failed (%d)\n",
            __FILE__, __LINE__, WSAGetLastError());
        exit(1);
    }

    // Awaken first in 1 second
    timerAwakenTime.QuadPart = -10000000; // 1 second

    if (FALSE == SetWaitableTimer(timerEvent,
        &timerAwakenTime,
        1000, // Awaken every second
        NULL,
        NULL,
        FALSE)) {

        printf("%s:%d - SetWaitableTimer failed (%d)\n",
            __FILE__, __LINE__, WSAGetLastError());
        exit(1);
    }

    // Initialize the counters to 0
    numPackets = 0;
    numBytes = 0;

    printf("-------------------------\n"
        " # packets |  # of bits |\n"
        "-------------------------\n");
    do {
        // Array of events for WaitForMultipleObjects
        HANDLE  waitEvents[2];

        // Used for WSARecv
        DWORD   numberOfBytesReceived;

        // Used for WSARecv
        DWORD   dwFlags;

        // Used for WSARecv
        WSABUF  buf;

        // The buffer is always the same global array
        buf.len = sizeof(dataBuffer);
        buf.buf = (PCHAR)dataBuffer;

        // No flags.
        dwFlags = 0;

        // Post a receive operation
        // We only have one receive outstanding at a time. 
        result = WSARecv(socket,
            &buf,
            1,
            &numberOfBytesReceived,
            &dwFlags,
            &recvOverlapped,
            NULL);

        if (result != 0) {
            // The receive call failed. This could be because the
            // call will be completed asynchronously (WSA_IO_PENDING) or
            // it could be a legitimate error
            DWORD errorCode;

            errorCode = WSAGetLastError();

            if (errorCode != WSA_IO_PENDING) {
                printf("%s:%d - WSARecv failed (%d)\n",
                    __FILE__, __LINE__, errorCode);
                exit(1);
            }

            // If the error was WSA_IO_PENDING the call will be completed
            // asynchronously.
        }

        // Prepare our array of events to wait on. We will wait on:
        // 1) The event from the receive operation
        // 2) The event for the timer
        waitEvents[0] = recvOverlapped.hEvent;
        waitEvents[1] = timerEvent;

        // We wait for either event to complete
        result = WaitForMultipleObjects(ARRAYSIZE(waitEvents),
            (PHANDLE)waitEvents,
            FALSE,
            INFINITE);

        switch (result) {
        case WAIT_OBJECT_0: {
            // The receive operation completed.
            // Determine the true result of the receive call.
            BOOL overlappedResult;

            overlappedResult = WSAGetOverlappedResult(socket,
                &recvOverlapped,
                &numberOfBytesReceived,
                FALSE,
                &dwFlags);

            if (overlappedResult == FALSE) {

                // The receive call failed.

                printf("%s:%d - WSARecv failed (%d)\n",
                    __FILE__, __LINE__, WSAGetLastError());
                exit(1);
            }

            // The receive call succeeded
            // Increase our counters and post a new receive.
            numPackets++;
            numBytes += numberOfBytesReceived;
            break;
        }
        case WAIT_OBJECT_0 + 1: {
            // The timer event fired: our 1 second period has gone by.
            // Print the average to the console

            printf("%10d | %10d |\n", numPackets, numBytes * 8);

            // Reset the counters
            numPackets = numBytes = 0;
            break;
        }
        default:
            // The wait call failed.
            printf("%s:%d - WaitForMultipleObjects failed (%d)\n",
                __FILE__, __LINE__, GetLastError());
            exit(1);
        }

        // We continue this loop until the process is forceably stopped
        // through Ctrl-C.
    } while (TRUE);

}

//******************************************************************************
// Routine: 
//      help
//
// Description:
//      This routine prints out the usage information for the program
//
//******************************************************************************
VOID
help()
{
    printf("USAGE:\n"
        "\tSERVER: qossample -s\n"
        "\tCLIENT: qossample -c IP-ADDRESS BIT-RATE\n\n"
        "\tIn the following example, the application would try to send\n"
        "\t20 Mb of traffic to the host at 10.0.0.1:\n"
        "\tqossample -c 10.0.0.1 20000000\n");
    return;
}

我测试时客户端和服务端是在不同的子网下,所以不支持带宽估计。运行结果如下:

客户端:

注意事项:

  • 服务端和客户端运行在同一台机器上好像不行,我这边实测QOSAddSocketToFlow方法会报Element Not Found错误。参考:winapi - QOSAddSocketToFlow returns error - ELEMENT NOT FOUND - Stack Overflow
  •  QOSAddSocketToFlow方法第5个参数传0会报50 Not support错误。怀疑可能是因为客户端和服务端未在同一个子网所致,实际未验证。

TCP版本

TCP代码:

流程和UDP一样,代码中的注释可以忽略。

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
   ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
   PARTICULAR PURPOSE.

   Copyright (c) Microsoft Corporation. All rights reserved.

Module Name:

    qossample.c

Abstract:

    This program implements a simple traffic generator which uses the QOS2
    api to not overload the network path between the source and its destination.

    Although the application can be run as a receiver to process the traffic
    generated, the client side of the application is the important part of this
    sample. This code is in the Client function.

    The code for the client works as follows:

    1) A socket is connected to the destination IP address
    2) The QOS subsystem is initialized by a call to QOSCreateHandle
    3) A flow is created for the socket by a call to QOSAddSocketToFlow
    4) The characteristics of the path between the source host and destination
       host are queried through QOSQueryFlow. The two estimates used are:

        - the bottleneck bandwidth: the maximum theoretical throughput;
        - and the available bandwidth: the currently available throughput for
          new traffic

       Depending on your network configuration:

       A) It may be impossible to run network experiments to the destination.
          For example, the destination might be on another subnet. If so, the
          client will only shape traffic and mark priority with DSCP based on
          the specified traffic type, but will never be able to receive
          notifications or estimates of available bandwidth and outgoing traffic
          would be marked per the DLNA specification.
       B) The QOS subsystem may not have had enough time to estimate conditions
          to the destination host. If this happens, the client will sleep for 1s
          before retrying. In a production application you could minize the
          likelihood of this happening by calling, before the start of your
          A/V traffic, the QOSStartTrackingClient API. It is suggested that this
          API always be used when the destination is on the same subnet as the
          client.

    5) If the estimate of available bandwidth is less than the requested
       bandwidth, the client revises the requested bandwidth.
    6) The client sets a shaping rate for our flow that will limit the outgoing
       traffic to the requested bandwidth. To do so, it accounts for the wire
       overhead given both the address family as well as the protocol used, UDP.
    7) The client registers for notifications of congestion.
    8) Traffic is sent at the desired rate using the shaping mechanism as the
       control loop. The application uses the API call TransmitPackets to
       send the packet bunches. The client uses the fact that the shaper will
       only complete this Winsock transmit operation when all packets have left
       the host to determine how many packets to send at a time.
    9) A congestion notification will be received whenever external conditions
       push the network into a congested state. The client, as a reaction, will
       lower the targeted bit rate to one-tenth of the initial value. Moreover,
       the client will request notification when the bandwidth available is
       enough to return to the previous bit rate.

Environment:

    User-Mode only

Revision History:

------------------------------------------------------------------------------*/

#include <winsock2.h>
#include <mswsock.h>
#include <stdlib.h>
#include <stdio.h>
#include <qos2.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "QWave.lib")
#pragma warning(disable:4127)   // condition expression is constant

//******************************************************************************
// Forward declarations
//******************************************************************************
VOID
client(
    __in                int     argc,
    __in_ecount(argc)   char* argv[]
);

VOID
server();

VOID
help();

//******************************************************************************
// Global defines
//******************************************************************************

// Size of the data part of each datagram that is exchanged between the
// client and the server. This does not count the IP and UDP header.
#define DATAGRAM_SIZE       1400

// Number of bursts we are aiming for per second
#define BURSTS_PER_SECOND   40

// Port to be used for communication, 
// 40007 in hex in network byte order 
#define PORT                0x479c

//******************************************************************************
// Global variables
//******************************************************************************

// Array used by both the client routine and the server routine to
// send and receive data
CHAR                dataBuffer[DATAGRAM_SIZE];

// Version of the QOS subsystem we wish to use
QOS_VERSION QosVersion = { 1 , 0 };

//******************************************************************************
// Routine: 
//      main
//
// Description:
//      Entry point. Verifies that the number of parameters is enough to 
//      ascertain whether this instance is to be a client or server.
// 
//******************************************************************************
int
_cdecl
main(
    __in                int argc,
    __in_ecount(argc)   char* argv[]
)
{
    // Verify the number of command line arguments
    if (argc < 2) {
        help();
        exit(0);
    }

    if (strcmp(argv[1], "-s") == 0) {
        // If the call was "qossample -s" run as a server
        server();
    }
    else if (strcmp(argv[1], "-c") == 0) {
        // If the call was "qossample -c ..." run as a client
        client(argc, argv);
    }

    // Else, run the help routine
    help();
    return 1;
}

//******************************************************************************
// Routine: 
//      socketCreate
//
// Description:
//      This routine prepares the socket for the client instance. To do so,
//      it converts the address parameter from string to IP address. Then
//      it creates a UDP socket which it connects to this destination.
//
//      Since we will use TransmitPackets for our send operations, the function
//      pointer is obtained from Winsock.
// 
//******************************************************************************
VOID
socketCreate(
    __in    PCHAR                       destination,
    __out   SOCKET* socket,
    __out   ADDRESS_FAMILY* addressFamily,
    __out   LPFN_TRANSMITPACKETS* transmitPackets
) {
    // Return value of WSAStartup
    WSADATA             wsaData;

    // Temporarily used to represent the destination IP address
    SOCKADDR_STORAGE    destAddr;

    // Result of the various Winsock API calls
    INT                 returnValue;

    // Used by WSAStringToAddressA
    INT                 sockaddrLen;

    // Used by WSAIoctl
    DWORD               bytesReturned;

    // GUID of the TransmitPacket Winsock2 function which we will
    // use to send the traffic at the client side.
    GUID TransmitPacketsGuid = WSAID_TRANSMITPACKETS;

    // Start Winsock
    returnValue = WSAStartup(MAKEWORD(2, 2), &wsaData);

    if (returnValue != 0) {
        printf("%s:%d - WSAStartup failed (%d)\n",
            __FILE__, __LINE__, returnValue);
        exit(1);
    }

    // First attempt to convert the string to an IPv4 address
    sockaddrLen = sizeof(destAddr);
    destAddr.ss_family = AF_INET;
    returnValue = WSAStringToAddressA(destination,
        AF_INET,
        NULL,
        (LPSOCKADDR)&destAddr,
        &sockaddrLen);

    if (returnValue != ERROR_SUCCESS) {
        // If this fails,
        // Attempt to convert the string to an IPv6 address
        sockaddrLen = sizeof(destAddr);
        destAddr.ss_family = AF_INET6;
        returnValue = WSAStringToAddressA(destination,
            AF_INET6,
            NULL,
            (LPSOCKADDR)&destAddr,
            &sockaddrLen);

        if (returnValue != ERROR_SUCCESS) {
            // If this again fails, exit the application
            // But print out the help information first.
            printf("%s:%d - WSAStringToAddressA failed (%d)\n",
                __FILE__, __LINE__, WSAGetLastError());
            help();
            exit(1);
        }
    }

    // Set the destination port.
    SS_PORT((PSOCKADDR)&destAddr) = PORT;

    // Copy the address family back to caller
    *addressFamily = destAddr.ss_family;

    // Create a UDP socket
    *socket = WSASocket(destAddr.ss_family,
        SOCK_STREAM,
        0,
        NULL,
        0,
        WSA_FLAG_OVERLAPPED);

    if (*socket == INVALID_SOCKET) {
        printf("%s:%d - WSASocket failed (%d)\n",
            __FILE__, __LINE__, WSAGetLastError());
        exit(1);
    }

    // Connect the new socket to the destination
    returnValue = WSAConnect(*socket,
        (PSOCKADDR)&destAddr,
        sizeof(destAddr),
        NULL,
        NULL,
        NULL,
        NULL);

    if (returnValue != NO_ERROR) {
        printf("%s:%d - WSAConnect failed (%d)\n",
            __FILE__, __LINE__, WSAGetLastError());
        exit(1);
    }

    // Query the function pointer for the TransmitPacket function
    returnValue = WSAIoctl(*socket,
        SIO_GET_EXTENSION_FUNCTION_POINTER,
        &TransmitPacketsGuid,
        sizeof(GUID),
        transmitPackets,
        sizeof(PVOID),
        &bytesReturned,
        NULL,
        NULL);

    if (returnValue == SOCKET_ERROR) {
        printf("%s:%d - WSAIoctl failed (%d)\n",
            __FILE__, __LINE__, WSAGetLastError());
        exit(1);
    }
}

//******************************************************************************
// Routine: 
//      client
//
// Description:
//      This routine creates a datagram socket which it connects to the remote
//      IP address. This socket is then added to a QOS flow. The application
//      uses the flow to rate control its outgoing traffic. Packets on this flow
//      will be prioritized if the network path between the client and receiver
//      support prioritization. Specifically, if the receiver is:
//
//      A) On-link (same subnet), both 802.1Q and DSCP are applied subject to
//         available bandwidth and network support.
//      B) Off-link (different subnet), only DSCP is applied regardless of
//         available bandwidth.
//
//      Moreover, the application queries the characteristics of the network 
//      path for the socket. If estimates are available, the application:
//
//      1) may readjust its throughput so as to not cause congestion on the 
//         network.
//      2) will be notified when the network enters congestion. As a result,
//         it will lower it's throughput to 1/10 the targeted rate.
//      3) will be notified when the network is no longer congested and enough
//         bandwidth is available for the application to return to its targeted
//         rate.
//
//******************************************************************************
VOID
client(
    __in                int     argc,
    __in_ecount(argc)   char* argv[]
) {
    // Address family of the datagram socket
    ADDRESS_FAMILY              addressFamily;

    // Socket for our traffic experiment
    SOCKET                      socket;

    // Function pointer to the TransmitPacket Winsock2 function
    LPFN_TRANSMITPACKETS        transmitPacketsFn;
    // Array of transmit elements
    LPTRANSMIT_PACKETS_ELEMENT  transmitEl;

    // Target packet rate and bit rate this application will aim to send
    // If there is no congestion
    ULONG                       targetPacketRate, targetBitRate;
    // Current rate at which the application is sending. If the network is
    // congested, this will be less than the target rate
    ULONG                       currentPacketRate, currentBitRate;
    // Counters for the achieved packet rate and achieved bit rate
    ULONG                       achievedPacketRate, achievedBitRate;

    // Timer; used to periodically output to the screen our counters
    HANDLE                      timerEvent;
    LARGE_INTEGER               timerAwakenTime;

    // Handle to the QOS subsystem
    // Returned by QOSCreateHandle
    HANDLE                      qosHandle;
    // ID of the QOS flow we will create for the socket. 
    // Returned by QOSAddSocketToFlow
    QOS_FLOWID                  flowID;
    // Result of QOSQueryFlow
    QOS_FLOW_FUNDAMENTALS       flowFund;
    // Parameter to QOSSetFlow
    QOS_FLOWRATE_OUTGOING       flowRate;
    // If TRUE, the QOS subsystem is running network experiments on the
    // network path to the destination. If false, estimates are not available.
    // The flow is still marked and shaped.
    BOOL                        estimatesAvailable;
    // Result of the QOS API calls.
    BOOL                        result;

    // Overlapped operation used for TransmitPackets
    WSAOVERLAPPED               sendOverlapped;
    // Overlapped operation used for QOSNotifyFlow to be notified
    // of network congestions.
    OVERLAPPED                  congestionOverlapped;
    // Overlapped operation used for QOSNotifyFlow to be notified when, 
    // after congestion, there is enough bandwidth for the target rate
    OVERLAPPED                  availableOverlapped;
    // TRUE if the network is currently congested
    BOOL                        congestion;

    ULONG                       temp;

    // Verify the number of command line arguments
    if (argc != 4) {
        help();
        exit(1);
    }

    // Identify what destination IP address we're trying to talk to and
    // connect a UDP socket to it
    socketCreate(argv[2],
        &socket,
        &addressFamily,
        &transmitPacketsFn);

    printf("socket resut %d\n", socket);

    if (FALSE == QOSCreateHandle(&QosVersion, &qosHandle)) {
        printf("%s:%d - QOSCreateHandle failed (%d)\n",
            __FILE__, __LINE__, GetLastError());
        exit(1);
    }

    // The Flow ID parameter MUST be set to 0 as input to QOSAddSocketToFlow
    flowID = 0;

    // Create a flow for our socket
    result = QOSAddSocketToFlow(qosHandle,
        socket,
        NULL,
        QOSTrafficTypeExcellentEffort,
        QOS_NON_ADAPTIVE_FLOW,
        &flowID);

    if (result == FALSE) {
        printf("%s:%d - QOSAddSocketToFlow failed (%d)\n",
            __FILE__, __LINE__, GetLastError());
        exit(1);
    }

    // Read the data rate in bits/s passed on the command line
    targetBitRate = atol(argv[3]);

    if (targetBitRate == 0) {
        help();
        exit(1);
    }

    // Convert from bits to bytes
    targetBitRate /= 8;

    // Calculate how many packets we need; round up.
    targetPacketRate = targetBitRate / ARRAYSIZE(dataBuffer);
    if (targetBitRate % ARRAYSIZE(dataBuffer) != 0)
        targetPacketRate++;

    // Calculate the number of packets per bursts; round up
    if (targetPacketRate % BURSTS_PER_SECOND != 0) {
        targetPacketRate /= BURSTS_PER_SECOND;
        targetPacketRate++;
    }
    else {
        targetPacketRate /= BURSTS_PER_SECOND;
    }

    // Calculate the final bit rate, targetBitRate, in bps that the application
    // will send.
    targetBitRate = BURSTS_PER_SECOND
        * targetPacketRate
        * ARRAYSIZE(dataBuffer)
        * 8;

    //
    // Allocate an array of transmit elements big enough to send 
    // targetPacketRate packets every burst.
    transmitEl = static_cast<LPTRANSMIT_PACKETS_ELEMENT>(HeapAlloc(GetProcessHeap(),
        HEAP_ZERO_MEMORY,
        sizeof(TRANSMIT_PACKETS_ELEMENT)
        * targetPacketRate));

    if (transmitEl == NULL) {
        printf("%s:%d - HeapAlloc failed (%d)\n",
            __FILE__, __LINE__, GetLastError());
        exit(1);
    }


    // For the purpose of this application, we only use one data buffer for all 
    // of our packets.
    ZeroMemory(&dataBuffer, sizeof(dataBuffer));

    //
    // Initialize each of these transmit element to point to the same zeroed out
    // data buffer
    for (temp = 0; temp < targetPacketRate; temp++) {
        transmitEl[temp].dwElFlags = TP_ELEMENT_MEMORY | TP_ELEMENT_EOP;
        transmitEl[temp].pBuffer = dataBuffer;
        transmitEl[temp].cLength = sizeof(dataBuffer);
    }

    // Print out what we'll be doing
    printf("----------------------------------"
        "----------------------------------\n"
        "This instance of the QOS sample program is aiming to:\n"
        "\t - Send %d bits of UDP traffic per second\n"
        "\t - In packets of %d bytes\n"
        "\t - In bursts of %d packets every %d milliseconds\n\n"
        "----------------------------------"
        "----------------------------------\n",
        targetBitRate,
        ARRAYSIZE(dataBuffer),
        targetPacketRate,
        1000 / BURSTS_PER_SECOND);

    // Assume, by default, that estimates are not available
    estimatesAvailable = FALSE;

    printf("Querying fundamentals about the network path: ");
    do {
        temp = sizeof(flowFund);

        // Query the flow fundamentals for the path to the destination.
        // This will return estimates of the bottleneck bandwidth, available
        // bandwidth and average RTT.
        result = QOSQueryFlow(qosHandle,
            flowID,
            QOSQueryFlowFundamentals,
            &temp,
            &flowFund,
            0,
            NULL);

        if (result == FALSE) {
            DWORD lastError;

            lastError = GetLastError();

            if (lastError == ERROR_NO_DATA) {
                // If the call failed, this could be because the QOS subsystem 
                // has not yet gathered enough data for estimates. If so, the
                // QOS2 api returns ERROR_NO_DATA.
                printf(".");
                Sleep(1000);
            }
            else if (lastError == ERROR_NOT_SUPPORTED)
            {
                // If the call failed, this could be because the QOS subsystem 
                // cannot run network experiments on the network path to the
                // destination. If so, the API returns ERROR_NOT_SUPPORTED.
                printf("NOT SUPPORTED\n"
                    "\t   Network conditions to this destination could not\n"
                    "\t   be detected as your network configuration is not\n"
                    "\t   supported for network experiments\n");
                break;
            }
            else {
                printf("%s:%d - QOSQueryFlow failed (%d)\n",
                    __FILE__, __LINE__, lastError);
                exit(1);
            }
        }
        else if ((flowFund.BottleneckBandwidthSet == FALSE)
            || (flowFund.AvailableBandwidthSet == FALSE)) {
            // If the call succeeded but bottleneck bandwidth or 
            // available bandwidth are not known then estimates are still
            // processing; query again in 1 second.
            printf(".");
            Sleep(1000);
        }
        else {
            // Estimate where available.
            double bottleneck;
            double available;

            estimatesAvailable = TRUE;

            // Convert the bottleneck bandwidth from bps to mbps
            bottleneck = (double)flowFund.BottleneckBandwidth;
            bottleneck /= 1000000.0;

            // Convert available bandwidth from bps to mbps
            available = (double)flowFund.AvailableBandwidth;
            available /= 1000000.0;

            printf("DONE\n"
                "\t - Bottleneck bandwidth is approximately %4.2f Mbps\n"
                "\t - Available bandwidth is approximately  %4.2f Mbps\n",
                bottleneck,
                available);

            break;
        }
    } while (TRUE);

    if (estimatesAvailable == TRUE) {
        UINT64 targetBitRateWithHeaders;

        printf("\nNOTE: the accuracy of the QOS estimates can be impacted by\n"
            "any of the following,\n\n"
            "\t - Both the network interface at the client\n"
            "\t   and at the server must be using NDIS 6 drivers.\n"
            "\t - If the server is not a Windows Vista host, verify that \n"
            "\t   it implements the LLD2 networking protocol. You can\n"
            "\t   find more information at http://www.microsoft.com.\n"
            "\t - IPSec, VPNs and enterprise class networking equipment\n"
            "\t   may interfere with network experiments.\n\n");

        // Calculate what our target bit rate, with protocol headers for 
        // IP(v4/v6) and UDP will be.
        targetBitRateWithHeaders = QOS_ADD_OVERHEAD(addressFamily,
            IPPROTO_UDP,
            ARRAYSIZE(dataBuffer),
            targetBitRate);

        if (flowFund.AvailableBandwidth < targetBitRateWithHeaders) {
            // If the estimate of available bandwidth is not sufficient for the
            // target bit rate (with headers), drop to a lesser throughput
            UINT64 availableBandwidth;

            // The estimate returned does not account for headers
            // Remove the estimated overhead for our address family and UDP.
            availableBandwidth = QOS_SUBTRACT_OVERHEAD(
                addressFamily,
                IPPROTO_UDP,
                ARRAYSIZE(dataBuffer),
                flowFund.AvailableBandwidth);

            // Calculate the rate of packets we can realistically send
            targetPacketRate = (LONG)availableBandwidth / 8;
            targetPacketRate /= ARRAYSIZE(dataBuffer);
            targetPacketRate /= BURSTS_PER_SECOND;

            // Calculate the real bit rate we'll be using
            targetBitRate = BURSTS_PER_SECOND
                * targetPacketRate
                * ARRAYSIZE(dataBuffer)
                * 8;

            printf("Not enough available bandwidth for the requested bitrate.\n"
                "Downgrading to lesser bitrate - %d.\n", targetBitRate);
        }
    }

    // Our starting rate is this target bit rate
    currentBitRate = targetBitRate;
    currentPacketRate = targetPacketRate;

    // Ask the QOS subsystem to shape our traffic to this bit rate. Note that
    // the application needs to account for the size of the IP(v4/v6) 
    // and UDP headers.

    // Calculate the real bandwidth we will need to be shaped to.
    flowRate.Bandwidth = QOS_ADD_OVERHEAD(addressFamily,
        IPPROTO_UDP,
        ARRAYSIZE(dataBuffer),
        currentBitRate);

    // Set shaping characteristics on our QOS flow to smooth out our bursty
    // traffic.
    flowRate.ShapingBehavior = QOSShapeAndMark;

    // The reason field is not applicable for the initial call.
    flowRate.Reason = QOSFlowRateNotApplicable;
    result = QOSSetFlow(qosHandle,
        flowID,
        QOSSetOutgoingRate,
        sizeof(flowRate),
        &flowRate,
        0,
        NULL);

    if (result == FALSE) {
        printf("%s:%d - QOSSetFlow failed (%d)\n",
            __FILE__, __LINE__, GetLastError());
        exit(1);
    }

    // Allocate a waitable timer. We will use this timer to periodically
    // awaken and output statistics.
    timerEvent = CreateWaitableTimer(NULL, FALSE, NULL);

    if (timerEvent == NULL) {
        printf("%s:%d - CreateWaitableTimer failed (%d)\n",
            __FILE__, __LINE__, GetLastError());
        exit(1);
    }

    //
    // Set the sampling time to 1 second
    timerAwakenTime.QuadPart = -10000000; // 1 second

    if (FALSE == SetWaitableTimer(timerEvent,
        &timerAwakenTime,
        1000,
        NULL,
        NULL,
        FALSE)) {

        printf("%s:%d - SetWaitableTimer failed (%d)\n",
            __FILE__, __LINE__, GetLastError());
        exit(1);
    }

    // Prepare the support overlapped structures to detect congestion,
    // notifications of bandwidth change and the completion of our send 
    // routines.
    ZeroMemory(&congestionOverlapped, sizeof(congestionOverlapped));
    congestionOverlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

    if (congestionOverlapped.hEvent == NULL) {
        printf("%s:%d - CreateEvent failed (%d)\n",
            __FILE__, __LINE__, GetLastError());
        exit(1);
    }

    ZeroMemory(&availableOverlapped, sizeof(availableOverlapped));
    availableOverlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

    if (availableOverlapped.hEvent == NULL) {
        printf("%s:%d - CreateEvent failed (%d)\n",
            __FILE__, __LINE__, GetLastError());
        exit(1);
    }

    ZeroMemory(&sendOverlapped, sizeof(sendOverlapped));
    sendOverlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

    if (sendOverlapped.hEvent == NULL) {
        printf("%s:%d - CreateEvent failed (%d)\n",
            __FILE__, __LINE__, GetLastError());
        exit(1);
    }

    if (estimatesAvailable == TRUE) {
        // If estimates are available, we request a notification 
        // for congestion. This notification will complete whenever network
        // congestion is detected.
        result = QOSNotifyFlow(qosHandle,
            flowID,
            QOSNotifyCongested,
            NULL,
            NULL,
            0,
            &congestionOverlapped);

        if (result == FALSE) {
            DWORD lastError;

            lastError = GetLastError();
            if (lastError != ERROR_IO_PENDING) {
                printf("%s:%d - QOSNotifyFlow failed (%d)\n",
                    __FILE__, __LINE__, lastError);
                exit(1);
            }
        }
    }

    printf("----------------------------------"
        "----------------------------------\n"
        " # packets | # of bits  |  Bottleneck "
        "|  Available |  Congestion   \n"
        "----------------------------------"
        "----------------------------------\n");

    // Initialize our counters to 0
    achievedBitRate = achievedPacketRate = 0;
    congestion = FALSE;

    do {
        BOOL sendNextGroup;

        // Send the first burst of packets
        result = (*transmitPacketsFn)(socket,
            transmitEl,
            currentPacketRate,
            0xFFFFFFFF,
            &sendOverlapped,
            TF_USE_KERNEL_APC);

        if (result == FALSE) {
            DWORD lastError;

            lastError = WSAGetLastError();
            if (lastError != ERROR_IO_PENDING) {
                printf("%s:%d - TransmitPackets failed (%d)\n",
                    __FILE__, __LINE__, GetLastError());
                exit(1);
            }
        }

        // Increase the counter of sent data
        achievedPacketRate += currentPacketRate;
        achievedBitRate += currentPacketRate * ARRAYSIZE(dataBuffer) * 8;
        sendNextGroup = FALSE;

        do {
            HANDLE          waitEvents[3];
            DWORD           waitResult;

            // Wait for any of the 3 events to complete

            // The 1 second periodic timer
            waitEvents[0] = timerEvent;

            if (congestion)
                // Notification of available bandwidth
                waitEvents[1] = availableOverlapped.hEvent;
            else
                // Notification of congestion
                waitEvents[1] = congestionOverlapped.hEvent;

            // Notification that the send operation has completed
            waitEvents[2] = sendOverlapped.hEvent;

            waitResult = WaitForMultipleObjects(ARRAYSIZE(waitEvents),
                (PHANDLE)waitEvents,
                FALSE,
                INFINITE);

            switch (waitResult) {
            case WAIT_OBJECT_0: {
                // The event for the periodic timer is set

                printf("%10d | ", achievedPacketRate);
                printf("%10d | ", achievedBitRate);

                if (estimatesAvailable) {
                    // If estimates are available for the network path
                    // query for estimates and output to the console along
                    // with our counters.

                    // Ascertained through QOSQueryFlow
                    BOOL estimateIndicatesCongestion;

                    // Default is unknown
                    estimateIndicatesCongestion = FALSE;

                    ZeroMemory(&flowFund, sizeof(flowFund));

                    temp = sizeof(flowFund);
                    QOSQueryFlow(qosHandle,
                        flowID,
                        QOSQueryFlowFundamentals,
                        &temp,
                        &flowFund,
                        0,
                        NULL);

                    if (flowFund.BottleneckBandwidthSet)
                        printf("%10d  | ", flowFund.BottleneckBandwidth);
                    else
                        printf("   NO DATA  | ");

                    if (flowFund.AvailableBandwidthSet) {
                        if (flowFund.AvailableBandwidth == 0)
                            estimateIndicatesCongestion = TRUE;

                        printf("%10d | ", flowFund.AvailableBandwidth);
                    }
                    else
                        printf("   NO DATA | ");

                    if (estimateIndicatesCongestion)
                        printf(" CONGESTION\n");
                    else
                        printf("\n");
                }
                else {
                    // Bandwidth estimates are not available
                    printf("    N/A     |     N/A    |\n");
                }

                //
                // Reset the counters
                achievedPacketRate = achievedBitRate = 0;
                break;
            }
            case WAIT_OBJECT_0 + 1: {
                // This is either a notification for congestion or 
                // for bandwidth available 

                if (congestion == FALSE) {
                    UINT64 targetBandwidthWithOverhead;
                    ULONG  bufferSize;
                    //
                    // Congestion
                    //
                    printf("----------------------------------"
                        "----------------------------------\n"
                        "CONGESTION\n"
                        "----------------------------------"
                        "----------------------------------\n");

                    //
                    // Reduce the current rate to one-tenth of the initial rate
                    // Insure you're always sending at least 1 packet per
                    // burst.
                    if (currentPacketRate < 10)
                        currentPacketRate = 1;
                    else
                        currentPacketRate /= 10;

                    // Calculate the new bit rate we'll be using
                    currentBitRate = BURSTS_PER_SECOND
                        * currentPacketRate
                        * ARRAYSIZE(dataBuffer)
                        * 8;


                    // Update the shaping characteristics on our QOS flow 
                    // to smooth out our bursty traffic.
                    flowRate.Bandwidth = QOS_ADD_OVERHEAD(
                        addressFamily,
                        IPPROTO_UDP,
                        ARRAYSIZE(dataBuffer),
                        currentBitRate);

                    flowRate.ShapingBehavior = QOSShapeAndMark;
                    flowRate.Reason = QOSFlowRateCongestion;

                    result = QOSSetFlow(qosHandle,
                        flowID,
                        QOSSetOutgoingRate,
                        sizeof(flowRate),
                        &flowRate,
                        0,
                        NULL);

                    if (result == FALSE) {
                        printf("%s:%d - QOSSetFlow failed (%d)\n",
                            __FILE__, __LINE__, GetLastError());
                        exit(1);
                    }

                    // Request a notification for when there is enough bandwidth
                    // to return to our previous targeted bit rate.
                    // This will complete only when the network is no longer
                    // congested and bandwidth is available.
                    targetBandwidthWithOverhead = QOS_ADD_OVERHEAD(
                        addressFamily,
                        IPPROTO_UDP,
                        ARRAYSIZE(dataBuffer),
                        targetBitRate);

                    bufferSize = sizeof(targetBandwidthWithOverhead);

                    result = QOSNotifyFlow(qosHandle,
                        flowID,
                        QOSNotifyAvailable,
                        &bufferSize,
                        (PVOID)&targetBandwidthWithOverhead,
                        0,
                        &availableOverlapped);

                    if (result == FALSE) {
                        DWORD lastError;

                        lastError = GetLastError();
                        if (lastError != ERROR_IO_PENDING) {
                            printf("%s:%d - QOSNotifyFlow failed (%d)\n",
                                __FILE__, __LINE__, lastError);
                            exit(1);
                        }
                    }

                    congestion = TRUE;
                }
                else {
                    //
                    // End of congestion
                    //
                    printf("----------------------------------"
                        "----------------------------------\n"
                        "END OF CONGESTION\n"
                        "----------------------------------"
                        "----------------------------------\n");

                    //
                    // Reset the current packet rate to the initial target rate
                    currentPacketRate = targetPacketRate;

                    // Reset the current bit rate to the initial target rate
                    currentBitRate = targetBitRate;


                    // Update the shaping characteristics on our QOS flow 
                    // to smooth out our bursty traffic.
                    flowRate.Bandwidth = QOS_ADD_OVERHEAD(addressFamily,
                        IPPROTO_UDP,
                        ARRAYSIZE(dataBuffer),
                        targetBitRate);

                    flowRate.ShapingBehavior = QOSShapeAndMark;
                    flowRate.Reason = QOSFlowRateCongestion;
                    result = QOSSetFlow(qosHandle,
                        flowID,
                        QOSSetOutgoingRate,
                        sizeof(flowRate),
                        &flowRate,
                        0,
                        NULL);

                    if (result == FALSE) {
                        printf("%s:%d - QOSSetFlow failed (%d)\n",
                            __FILE__, __LINE__, GetLastError());
                        exit(1);
                    }

                    // Request a notification for the next network congestion
                    result = QOSNotifyFlow(qosHandle,
                        flowID,
                        QOSNotifyCongested,
                        NULL,
                        NULL,
                        0,
                        &congestionOverlapped);

                    if (result == FALSE) {
                        DWORD lastError;

                        lastError = GetLastError();
                        if (lastError != ERROR_IO_PENDING) {
                            printf("%s:%d - QOSNotifyFlow failed (%d)\n",
                                __FILE__, __LINE__, lastError);
                            exit(1);
                        }
                    }

                    congestion = FALSE;
                }
                break;
            }
            case WAIT_OBJECT_0 + 2: {
                // The transmit packet has completed its send, 
                // If it did so successfully, it's time to send the next 
                // burst of packets.
                BOOL    overlappedResult;
                DWORD   ignoredNumOfBytesSent;
                DWORD   ignoredFlags;

                overlappedResult = WSAGetOverlappedResult(
                    socket,
                    &sendOverlapped,
                    &ignoredNumOfBytesSent,
                    FALSE,
                    &ignoredFlags);

                if (overlappedResult == FALSE) {
                    printf("%s:%d - TransmitPackets failed (%d)\n",
                        __FILE__, __LINE__, WSAGetLastError());
                    exit(1);
                }

                // Time to send out the next bunch of packets
                sendNextGroup = TRUE;
                break;
            }
            default:
                // The wait call failed.
                printf("%s:%d - WaitForMultipleObjects failed (%d)\n",
                    __FILE__, __LINE__, GetLastError());
                exit(1);
            }

        } while (sendNextGroup == FALSE);
    } while (TRUE);
}

//******************************************************************************
// Routine: 
//      server
//
// Description:
//      This routine creates a socket through which it will receive
//      any datagram sent by the client. It counts the number of packet 
//      and the number of bytes received. Periodically, it outputs this 
//      information to the console.
//
//******************************************************************************
VOID
server()
{
    HANDLE              timerEvent;
    LARGE_INTEGER       timerAwakenTime;

    // Return value of WSAStartup
    WSADATA             wsaData;



    // IPv6 wildcard address and port number 40007 to listen on at the server
    SOCKADDR_IN6 IPv6ListeningAddress = { AF_INET6,
                                            PORT,
                                            0,
                                            IN6ADDR_ANY_INIT,
                                            0 };

    // Result value from the various API calls
    DWORD               result;

    // Used to specify an option to setsocktopt
    ULONG               optionValue;

    // Overlapped structure used to post asynchronous receive calls
    WSAOVERLAPPED       recvOverlapped;

    // Counters of the number of bytes and packets received over 
    // a period of time.
    DWORD               numPackets, numBytes;



    // Initialize Winsock
    result = WSAStartup(MAKEWORD(2, 2), &wsaData);

    if (result != 0) {
        printf("%s:%d - WSAStartup failed (%d)\n",
            __FILE__, __LINE__, GetLastError());
        exit(1);
    }

    // 创建 TCP 监听套接字
    SOCKET listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (listener == INVALID_SOCKET) {
        printf("Error creating socket: %ld\n", WSAGetLastError());
        WSACleanup();
        exit(1);
    }

    // 绑定到本地地址和端口
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = PORT;
    int res = bind(listener, (struct sockaddr*)&addr, sizeof(addr));
    if (res == SOCKET_ERROR) {
        printf("Error binding socket: %ld\n", WSAGetLastError());
        closesocket(listener);
        WSACleanup();
        exit(1);
    }
    else {
        printf("begin listen on %d", PORT);
    }

    // 开始监听传入的连接
    res = listen(listener, SOMAXCONN);
    if (res == SOCKET_ERROR) {
        printf("Error listening on socket: %ld\n", WSAGetLastError());
        closesocket(listener);
        WSACleanup();
        exit(1);
    }

    // 接受连接并处理请求
    while (1) {
        struct sockaddr_in client_addr;
        int client_addr_len = sizeof(client_addr);
        SOCKET client = accept(listener, (struct sockaddr*)&client_addr, &client_addr_len);
        if (client == INVALID_SOCKET) {
            printf("Error accepting client: %ld\n", WSAGetLastError());
            closesocket(listener);
            WSACleanup();
            exit(1);
        }

        while (true) {
            // 读取请求数据
            char buf[1024];
            int bytes_read = recv(client, buf, sizeof(buf), 0);
            if (bytes_read == SOCKET_ERROR)
            {
                printf("Error read: %ld\n", WSAGetLastError());
                break;
            }
            // 打印请求数据
            printf("Received request:%d \n", bytes_read);
        }
        printf("client closed!\n");
        // 关闭连接
        closesocket(client);
    }
    // 关闭监听套接字和清理 Winsock
    closesocket(listener);
    WSACleanup();
}

//******************************************************************************
// Routine: 
//      help
//
// Description:
//      This routine prints out the usage information for the program
//
//******************************************************************************
VOID
help()
{
    printf("USAGE:\n"
        "\tSERVER: qossample -s\n"
        "\tCLIENT: qossample -c IP-ADDRESS BIT-RATE\n\n"
        "\tIn the following example, the application would try to send\n"
        "\t20 Mb of traffic to the host at 10.0.0.1:\n"
        "\tqossample -c 10.0.0.1 20000000\n");
    return;
}

 Rust写的TCP版本

这段代码是一个 Rust 程序,它使用 Windows 系统的 QoS API 来实现一个简单的客户端,连接到指定的服务器并发送一条消息。

程序的主要步骤如下:

1. 连接到服务器,创建一个 TCP 流。
2. 获取流的原始套接字句柄。
3. 使用 QOSCreateHandle 函数创建一个 QoS 句柄。
4. 使用 QOSAddSocketToFlow 函数将套接字添加到 QoS 流中,指定流量类型为音视频流量。
5. 发送一条消息到服务器。
6. 从服务器接收一条消息。
7. 使用 QOSCloseHandle 函数关闭 QoS 句柄。

在程序中,使用了 Rust 的标准库中的 io、net 和 os 模块,以及第三方库 windows-sys,该库提供了对 Windows 系统 API 的 Rust 绑定。

程序使用了 unsafe 关键字,这是因为它调用了 Windows 系统的 API,这些 API 不是 Rust 的安全代码。程序还使用了 panic! 宏来处理错误情况,这将导致程序崩溃并打印错误消息。

总的来说,这段代码演示了如何使用 Rust 和 Windows 系统的 QoS API 来实现一个简单的客户端,连接到服务器并发送一条消息。这条消息的DSCP值是被设置过的。

// 导入必要的库
use std::io::{Read, Write};
use std::net::TcpStream;
use std::os::windows::io::AsRawSocket;

// 导入 Windows API
use windows_sys::Win32::Foundation::{GetLastError, FALSE, HANDLE};
use windows_sys::Win32::NetworkManagement::QoS::{
    QOSAddSocketToFlow, QOSCloseHandle, QOSCreateHandle, QOSTrafficTypeAudioVideo,
    QOS_NON_ADAPTIVE_FLOW, QOS_VERSION,
};
use windows_sys::Win32::Networking::WinSock::SOCKET;

fn main() -> std::io::Result<()> {
    // 连接到服务器
    let mut stream = TcpStream::connect("118.24.148.75:8080")?; // 连接到服务器
    let socket = stream.as_raw_socket(); // 获取 socket

    // 创建 QoS 句柄
    let version = QOS_VERSION {
        MajorVersion: 1,
        MinorVersion: 0,
    };
    let mut qos_handle: HANDLE = 0;
    let res = unsafe { QOSCreateHandle(&version, &mut qos_handle) }; // 创建 QoS 句柄
    if res == FALSE {
        let error = unsafe { GetLastError() };
        panic!("Error creating QoS handle: {}, {}", res, error);
    }
    let mut flowid = 0;

    //会将dscp值设置为0x28
    let res = unsafe {
        QOSAddSocketToFlow(
            qos_handle,
            socket as SOCKET,
            std::ptr::null_mut(),
            QOSTrafficTypeAudioVideo,
            QOS_NON_ADAPTIVE_FLOW,
            &mut flowid,
        )
    };
    if res == FALSE {
        panic!("Error adding socket to flow: {}", res);
    }

    // 发送数据
    stream.write_all(b"Hello, server!")?;

    // 接收数据
    let mut buf = [0; 512];
    let size = stream.read(&mut buf)?;
    let response = String::from_utf8_lossy(&buf[..size]);
    println!("Received: {}", response);

    let ret = unsafe { QOSCloseHandle(qos_handle) };
    if ret == FALSE {
        panic!("Error closing QoS handle: {}", ret);
    }

    Ok(())
}

Cargo.toml如下

[package]
name = "hello"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
#rand = "0.7"
#bytes = "1"
#tokio = { version = "1.0", features = ["full"] }
#socket2 = {}
#once_cell = "1.8"
#signal-hook = {}
#nix = {features = ["net"]}
windows-sys = {version = "*",features = ["Win32_Foundation","Win32_Networking_WinSock","Win32_NetworkManagement","Win32_NetworkManagement_QoS"]}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1201742.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

手把手教你数据流图如何画,轻松搞定!

数据流图是一种强大的工具&#xff0c;用于可视化和分析系统中的数据流动和处理过程。它不仅能够帮助我们更好地理解系统的功能和流程&#xff0c;还能够帮助我们发现和解决潜在的问题。在本篇文章中&#xff0c;我们将手把手教你掌握数据流图。 一、数据流图的概念和构成元素 …

合合信息亮相新加坡科技周——Big Data AI World Expo展示AI驱动文档数字化的前沿能力

展会规模背景&#xff1a; 2023年10月11日-12日&#xff0c;合合信息在TECH WEEK SINGAPORE&#xff08;新加坡科技周&#xff09;亮相&#xff0c;并在人工智能世界博览会&#xff08;Big Data & AI World&#xff09;展示合合信息核心人工智能文字识别技术能力。合合信息…

cadence virtuoso 导出电路图

去掉网格&#xff1a;option-display file-export image

FPGA高端项目:图像采集+GTX+UDP架构,高速接口以太网视频传输,提供2套工程源码加QT上位机源码和技术支持

目录 1、前言免责声明本项目特点 2、相关方案推荐我这里已有的 GT 高速接口解决方案我这里已有的以太网方案 3、设计思路框架设计框图视频源选择OV5640摄像头配置及采集动态彩条视频数据组包GTX 全网最细解读GTX 基本结构GTX 发送和接收处理流程GTX 的参考时钟GTX 发送接口GTX …

如何在电脑和手机设备上编辑只读 PDF

我们大多数人更喜欢以 PDF 格式共享和查看文件&#xff0c;因为它更专业、更便携。但是&#xff0c;通常情况下您被拒绝访问除查看之外的内容编辑、复制或评论。如果您希望更好地控制您的 PDF 或更灵活地编辑它&#xff0c;请弄清楚为什么您的 PDF 是只读的&#xff0c;然后使用…

Final Cut Pro X for Mac:打造专业级视频剪辑的终极利器

随着数字媒体技术的不断发展&#xff0c;视频剪辑已经成为各行各业不可或缺的一部分。Final Cut Pro X for Mac作为一款专业的视频剪辑软件&#xff0c;凭借其强大的功能和易用性&#xff0c;已经成为Mac用户的首选。本文将向您详细介绍Final Cut Pro X for Mac的优势、功能以及…

思维导图软件 Xmind mac中文版软件特点

XMind mac是一款思维导图软件&#xff0c;可以帮助用户创建各种类型的思维导图和概念图。 XMind mac软件特点 - 多样化的导图类型&#xff1a;XMind提供了多种类型的导图&#xff0c;如鱼骨图、树形图、机构图等&#xff0c;可以满足不同用户的需求。 - 强大的功能和工具&#…

C++——const成员

这里先用队列举例&#xff1a; #define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include <assert.h> using namespace std; class SeqList { public:void pushBack(int data){if (_size _capacity){int* tmp (int*)realloc(a, sizeof(int) * 4);if (tm…

Linux虚拟机的安装

文章目录 1. 准备虚拟机2. 安装所需软件3. 上传项目文件4. 配置项目环境5. 安装项目依赖6. 数据库设置7. 启动项目8. 测试项目9. 设置域名和DNS&#xff08;可选&#xff09;10. 定期维护11. 使用反向代理&#xff08;可选&#xff09;12. 安全性加固13. 使用容器化技术&#x…

操作系统概念

一、是什么 操作系统&#xff08;Operating System&#xff0c;缩写&#xff1a;OS&#xff09;是一组主管并控制计算机操作、运用和运行硬件、软件资源和提供公共服务来组织用户交互的相互关联的系统软件程序&#xff0c;同时也是计算机系统的内核与基石 简单来讲&#xff0…

springboot rocketmq 延时消息、延迟消息

rocketmq也有延迟消息&#xff0c;经典的应用场景&#xff1a;订单30分钟未支付&#xff0c;则取消的场景 其他博客提到从rocketmq5.0开始&#xff0c;支持自定义延迟时间&#xff0c;4.x只支持预定义延迟时间&#xff0c;安装rocketmq可参考RocketMq简介及安装、docker安装ro…

1.jvm基本知识

目录 概述jvm虚拟机三问jvm是什么&#xff1f;java 和 jvm 的关系 为什么学jvm怎么学习为什么jvm调优?什么时候jvm调优调优调什么 结束 概述 jvm虚拟机三问 jvm是什么&#xff1f; 广义上指的是一种规范&#xff0c;狭义上是 jdk 中的 jvm 虚拟机(实际上是各厂商实现的虚拟…

PCBA表面污染的分类及处理方法

NO.1 引言 在PCBA生产过程中&#xff0c;锡膏和助焊剂会产生残留物质&#xff0c;残留物中包含的有机酸和电离子&#xff0c;前者易腐蚀PCBA&#xff0c;后者会造成焊盘间短路故障。且近年来&#xff0c;用户对产品的清洁度要求越来越严格&#xff0c;PCBA清洗工艺逐渐被电子组…

Rust图形界面:从零开始创建eGUi项目

文章目录 创建组件show函数 egui系列&#xff1a;初步 创建 首先&#xff0c;用cargo创建一个新项目&#xff0c;并添加eframe cargo new hello cd hello cargo add eframe cargo run尽管默认创建的项目只实现了输出Hello world功能&#xff0c;但添加了eframe库&#xff0c…

高频SQL50题(基础班)-4

文章目录 主要内容一.SQL练习题1.1789-员工的直属部门代码如下&#xff08;示例&#xff09;: 2.610-判断三角形代码如下&#xff08;示例&#xff09;: 3.180-连续出现的数字代码如下&#xff08;示例&#xff09;: 4.1164-指定日期的产品价格代码如下&#xff08;示例&#x…

Nginx:Windows详细安装部署教程

一、Nginx简介 Nginx (engine x) 是一个高性能的HTTP和反向代理服务器&#xff0c;也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔赛索耶夫为俄罗斯访问量第二的Rambler.ru站点&#xff08;俄文&#xff1a;Рамблер&#xff09;开发的。 它也是一种轻量级的Web服务器…

SQL 存储过程优化

问题&#xff1a;一个复杂的6个表的left jion 语句&#xff0c;发现设置为定时任务后最高时长为18分钟 1、原因分析&#xff1a;对复杂SQL 进行拆分验证 发现是合同明细表和 产品表的left jion 时间过长&#xff0c;发现 合同明细表每天为3w条&#xff0c;之前做过优化 对每个…

⑤ 【MySQL】DCL语句 —— 用户管理、权限控制

个人简介&#xff1a;Java领域新星创作者&#xff1b;阿里云技术博主、星级博主、专家博主&#xff1b;正在Java学习的路上摸爬滚打&#xff0c;记录学习的过程~ 个人主页&#xff1a;.29.的博客 学习社区&#xff1a;进去逛一逛~ MySQL用户与权限 ⑤ 【MySQL】DCL语句 —— 用…

mindspore mindcv图像分类算法;昇腾NPU加速使用;模型保存与加载

参考&#xff1a; https://www.mindspore.cn/tutorials/en/r1.3/save_load_model.html https://github.com/mindspore-lab/mindcv/blob/main/docs/zh/tutorials/finetune.md 1、mindspore mindcv图像分类算法 import os from mindcv.utils.download import DownLoad import o…

算法通关村第十五关白银挑战——海量数据场景下的热门算法题

大家好&#xff0c;我是怒码少年小码。 最近超级忙&#xff0c;很多实验报告&#xff0c;已经四五天没搞了&#xff0c;但是我还是回来了&#xff01; 海量数据场景下的热门算法题 本篇的题目不要求写代码&#xff0c;面试的时候能很清楚的说出思路就可以了。 1. 从40个亿中…