Complete Hyper-V Socket Client Code

#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <stdafx.h>
#include <ws2tcpip.h>
#include <objbase.h>

#define DEFAULT_BUFLEN 512

// Vm ID: {CDEE5C88-C5A5-4CC8-9D2D-C15816A01349}
DEFINE_GUID(VmId,
    0xcdee5c88, 0xc5a5, 0x4cc8, 0x9d, 0x2d, 0xc1, 0x58, 0x16, 0xa0, 0x13, 0x49);

// Service GUID: {8BA03980-174A-43E7-852A-E5EC79A671C7}
DEFINE_GUID(ServiceId,
    0x8ba03980, 0x174a, 0x43e7, 0x85, 0x2a, 0xe5, 0xec, 0x79, 0xa6, 0x71, 0xc7);

int __cdecl main(void)
{
    WSADATA wsaData;
    SOCKET ConnectSocket = INVALID_SOCKET;
    struct addrinfo hints;
    SOCKADDR_HV clientService;
    char *sendbuf = &amp;quot;this is a test&amp;quot;;
    char recvbuf[DEFAULT_BUFLEN];
    int iResult;
    int recvbuflen = DEFAULT_BUFLEN;

    // add your own input here
    CONST GUID *vmId = &amp;amp;VmId;
    CONST GUID *serviceId = &amp;amp;ServiceId;

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &amp;amp;wsaData);
    if (iResult != 0) {
        printf(&amp;quot;WSAStartup failed with error: %d\n&amp;quot;, iResult);
        return 1;
    }

    ZeroMemory(&amp;amp;clientService, sizeof(clientService));
    clientService.Family = AF_HYPERV;
    clientService.VmId = *vmId;
    clientService.ServiceId = *serviceId;

    ZeroMemory(&amp;amp;hints, sizeof(hints));
    hints.ai_family = AF_HYPERV;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = HV_PROTOCOL_RAW;

    hints.ai_addrlen = sizeof(SOCKADDR_HV);
    hints.ai_addr = reinterpret_cast&amp;lt;SOCKADDR *&amp;gt;(&amp;amp;clientService);

    // Create a SOCKET for connecting to server
    ConnectSocket = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol);
    if (ConnectSocket == INVALID_SOCKET) {
        printf(&amp;quot;socket failed with error: %ld\n&amp;quot;, WSAGetLastError());
        WSACleanup();
        return 1;
    }

    // Connect to server.
    iResult = connect(ConnectSocket, hints.ai_addr, (int)hints.ai_addrlen);
    if (iResult == SOCKET_ERROR) {
        closesocket(ConnectSocket);
        ConnectSocket = INVALID_SOCKET;
    }

    if (ConnectSocket == INVALID_SOCKET) {
        printf(&amp;quot;Unable to connect to server!\n&amp;quot;);
        WSACleanup();
        return 1;
    }

    // Send an initial buffer
    iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
    if (iResult == SOCKET_ERROR) {
        printf(&amp;quot;send failed with error: %d\n&amp;quot;, WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    printf(&amp;quot;Bytes Sent: %ld\n&amp;quot;, iResult);

    // shutdown the connection since no more data will be sent
    iResult = shutdown(ConnectSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
        printf(&amp;quot;shutdown failed with error: %d\n&amp;quot;, WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    // Receive until the peer closes the connection
    do {

        iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
        if (iResult &amp;gt; 0)
            printf(&amp;quot;Bytes received: %d\n&amp;quot;, iResult);
        else if (iResult == 0)
            printf(&amp;quot;Connection closed\n&amp;quot;);
        else
            printf(&amp;quot;recv failed with error: %d\n&amp;quot;, WSAGetLastError());

    } while (iResult &amp;gt; 0);

    // cleanup
    closesocket(ConnectSocket);
    WSACleanup();

    return 0;
}
Advertisements