虛擬機(jī)交流接口VMCI(The Virtual Machine Communication Interface)是一個(gè)在一個(gè)或多個(gè)虛擬機(jī)與宿主機(jī)之間提供高速高效交流的基本組件(infrastructure)。
創(chuàng)新互聯(lián)于2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元河津做網(wǎng)站,已為上家服務(wù),為河津各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575
以前,VMware提供了VMCI SDK用于VMCI開發(fā),現(xiàn)在用VMCI套接字庫(VMCI Sockets library)代替了SDK。
下面給出根據(jù)VMCI Sockets library編寫的示例代碼:
// client.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#include "stdafx.h"
#include
#include
#include
#include
#define BUFSIZE 4096
#pragma comment(lib, "ws2_32")
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData = { 0 };
struct sockaddr_vm their_addr = { 0 };
struct sockaddr_vm my_addr = { 0 };
int err = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (err != 0) {
printf("Could not register with Winsock DLL.\n");
goto cleanup;
}
SOCKET sockfd = INVALID_SOCKET;
int afVMCI = VMCISock_GetAFValue();
if ((sockfd = socket(afVMCI, SOCK_STREAM, 0)) == -1) {
printf("socket");
goto exit;
}
their_addr.svm_family = afVMCI;
// their_addr.svm_cid = VMCISock_GetLocalCID(); // 獲取本機(jī)的CID
their_addr.svm_cid = 2; // 2是筆者在實(shí)體機(jī)上獲取的CID,讀者可自行調(diào)用VMCISock_GetLocalCID獲取指定機(jī)器的CID
their_addr.svm_port = 12345;
if ((connect(sockfd, (struct sockaddr *) &their_addr, sizeof their_addr)) == -1) {
printf("connect err %d\n", WSAGetLastError());
goto close;
}
char send_buf[BUFSIZE];
strcpy_s(send_buf, BUFSIZE, "hello\n");
while (true)
{
int numbytes = 0;
/* Initialize send_buf with your data. */
if ((numbytes = send(sockfd, send_buf, strlen(send_buf), 0)) == -1) {
printf("send err %d\n", WSAGetLastError());
goto close;
}
printf("send %s\n", send_buf);
Sleep(5 * 1000);
}
close:
closesocket(sockfd);
exit:
cleanup :
WSACleanup();
return 0;
}
// server.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#include "stdafx.h"
#include
#include
#include
#include
#define BUFSIZE 4096
#pragma comment(lib, "ws2_32")
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData = { 0 };
struct sockaddr_vm my_addr = { 0 };
int err = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (err != 0) {
printf("Could not register with Winsock DLL.\n");
goto cleanup;
}
int afVMCI = VMCISock_GetAFValue();
SOCKET sockfd = INVALID_SOCKET;
if ((sockfd = socket(afVMCI, SOCK_STREAM, 0)) == -1) {
perror("socket");
goto cleanup;
}
my_addr.svm_family = afVMCI;
my_addr.svm_cid = VMADDR_CID_ANY;
my_addr.svm_port = 12345;
if (bind(sockfd, (struct sockaddr *) &my_addr, sizeof my_addr) == -1) {
printf("bind");
goto close;
}
if (listen(sockfd, 5) == -1) {
perror("listen");
goto close;
}
struct sockaddr_vm their_addr;
socklen_t their_addr_len = sizeof their_addr;
SOCKET newfd = INVALID_SOCKET;
if ((newfd = accept(sockfd, (struct sockaddr *) &their_addr, &their_addr_len)) == -1) {
printf("accept err %d\n", WSAGetLastError());
goto close;
}
while (true)
{
char recv_buf[BUFSIZE];
int numbytes = 0;
if ((numbytes = recv(newfd, recv_buf, sizeof recv_buf, 0)) == -1) {
printf("recv err %d\n", WSAGetLastError());
goto close;
}
recv_buf[numbytes] = '\0';
printf(recv_buf);
if ((numbytes = send(newfd, recv_buf, numbytes, 0)) == -1) {
printf("send err %d\n", WSAGetLastError());
goto close;
}
}
close:
closesocket(sockfd);
closesocket(newfd);
cleanup:
WSACleanup();
return 0;
}