通过VMCI实现VMware虚拟机与实体机、虚拟机与虚拟机的通信
2016-06-03 17:08
661 查看
虚拟机交流接口VMCI(The Virtual Machine Communication Interface)是一个在一个或多个虚拟机与宿主机之间提供高速高效交流的基本组件(infrastructure)。
以前,VMware提供了VMCI SDK用于VMCI开发,现在用VMCI套接字库(VMCI Sockets library)代替了SDK。
下面给出根据VMCI Sockets library编写的示例代码:
// client.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <vmci_sockets.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
#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(); // 获取本机的CID
their_addr.svm_cid = 2; // 2是笔者在实体机上获取的CID,读者可自行调用VMCISock_GetLocalCID获取指定机器的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 : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <vmci_sockets.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
#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;
}
以前,VMware提供了VMCI SDK用于VMCI开发,现在用VMCI套接字库(VMCI Sockets library)代替了SDK。
下面给出根据VMCI Sockets library编写的示例代码:
// client.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <vmci_sockets.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
#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(); // 获取本机的CID
their_addr.svm_cid = 2; // 2是笔者在实体机上获取的CID,读者可自行调用VMCISock_GetLocalCID获取指定机器的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 : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <vmci_sockets.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
#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;
}
相关文章推荐
- 在 Linux 上安装 VMware 工具
- 把Chrome OS安装在VMWare下及Chrome OS的关键特性和一些截图
- 安装 VMware Tools 时报 客户机操作系统已将 CD-ROM 门锁定,并且可能正在使用CD-ROM
- 企业云 2.0:VMware 眼中的企业 IT 的未来
- Windows 8虚拟机不能全屏的解决方法
- 使用VMware Workstation虚拟机保护数据
- VMware Workstation 5.5.3 Build 34685 汉化补丁
- Vmware虚拟机的安装及配置方法
- nasm实现的用vmware运行自做的linux启动盘的引导代码
- Install ESX Server 3.5/3i onto ESX Server安装方法
- Java连接Vmware中的redis
- vmware下安装rhel6.7
- 【笔记】CentOS
- [教程] VMware Workstation 9安装MAC OS 10.8全程图解
- VMware Hadoop2.5.2分布式环境搭建
- vmware 打开虚拟机时提示“该虚拟机似乎正在被使用”解决
- VMware NAT端口映射 外网可以访问内网虚拟机
- OS X Mavericks 10.9.2 Retail VMware Image
- VMware tools的用途及安装
- win7 + 虚拟机 + Mac