9.0

Table Of Contents
VMCI Sockets Programming Guide
20 VMware, Inc.
Preparing the Server for a Connection
Atthetopofyourapplication,includevmci_sockets.handdeclareaconstantforthesocketbuffersize.In
theexamplebelow,BUFSIZEdefinesthesocketbuffersize.Thenumber4096isagoodchoiceforefficiencyon
multipleplatforms.ItisnotbasedonthesizeofaUDPdatagram.
#include "vmci_sockets.h"
#define BUFSIZE 4096
TocompileonWindows,youmustcalltheWinsockWSAStartup()function.
err = WSAStartup(versionRequested, &wsaData);
if (err != 0) {
printf(stderr, "Could not register with Winsock DLL.\n");
goto exit;
}
ThisisnotnecessaryonnonWindowssystems.
Socket() Function
ToalteraUDPsocketprogramforVMCIsockets,obtainthenewaddressfamilytoreplaceAF_INET.
int afVMCI = VMCISock_GetAFValue();
if ((sockfd_dgram = socket(afVMCI, SOCK_DGRAM, 0)) == -1) {
perror("socket");
goto exit;
}
VMCISock_GetAFValue()returnsadescriptorfortheVMCIsocketsaddressfamilyifavailable.
Thiscallissimilartotheoneforstreamsockets,buthasSOCK_DGRAMinsteadofSOCK_STREAM.
Socket Options
CurrentlyVMCIsocketsoffersnooptionsfordatagramconnections.
Bind() Function
Thebind()callassociatesthedatagramsocketwiththenetworksettingsinthesockaddr_vmstructure,
insteadofthesockaddr_instructure.
struct sockaddr_vm my_addr = {0};
my_addr.svm_family = afVMCI;
my_addr.svm_cid = VMADDR_CID_ANY;
my_addr.svm_port = VMADDR_PORT_ANY;
if (bind(sockfd, (struct sockaddr *) &my_addr, sizeof my_addr) == -1) {
perror("bind");
goto close;
}
Thesockaddr_vmstructurecontainsanelementforthecontextID(CID)tospecifythevirtualmachine.For
theclient(connector)thisisthelocalCID.Fortheserver(listener),itcouldbeanyconnectingvirtualmachine.
VMADDR_CID_ANYandVMADDR_PORT_ANYarepredefinedsothatatbindorconnectiontime,theappropriate
CIDandportnu
mberarefilledinfromtheclient.VMADDR_CID_ANYisreplacedwiththeCIDofthevirtual
machineandVMADDR_PORT_ANYprovidesanephemeralportfromthenonreservedrange(>=1024).
Theclient(connector)canobtainitslocalCIDbycallingVMCISock_GetLocalCID().
TheVMCIsocketsbind()functionisthesameasforaUDPdata
gramapplication.