9.0

Table Of Contents
VMCI Sockets Programming Guide
14 VMware, Inc.
Preparing the Server for a Connection
Atthetopofyourapplication,includevmci_sockets.handdeclareaconstantforthesocketbuffersize.In
theexamplebelow,BUFSIZEdefinesthesocketbuffersize.Thenumber4096isagoodchoiceforefficiencyon
multipleplatforms.ItisnotbasedonthesizeofaTCPpacket,whichisusu
allysmaller.
#include "vmci_sockets.h"
#define BUFSIZE 4096
TocompileonWindows,youmustalsocalltheWinsockWSAStartup()function.
err = WSAStartup(versionRequested, &wsaData);
if (err != 0) {
printf(stderr, "Could not register with Winsock DLL.\n");
goto cleanup;
}
ThisisnotnecessaryonnonWindowssystems.
Socket() Function
InaVMCIsocketsapplication,obtainthenewaddressfamily(domain)toreplaceAF_INET.
int afVMCI = VMCISock_GetAFValue();
if ((sockfd = socket(afVMCI, SOCK_STREAM, 0)) == -1) {
perror("socket");
goto cleanup;
}
VMCISock_GetAFValue()returnsadescriptorfortheVMCIsocketsaddressfamilyifavailable.
Set and Get Socket Options
VMCIsocketsallowsyoutosettheminimum,maximum,anddefaultsizeofcommunicatingstreambuffers.
Namesforthethreeoptionsare:
SO_VMCI_BUFFER_SIZEDefaultsizeofcommunicatingbuffers;65536bytesifnotset.
SO_VMCI_BUFFER_MIN_SIZEMinimumsizeofcommunicatingbuffers;defaultsto128bytes.
SO_VMCI_BUFFER_MAX_SIZEMaximumsizeofcommunicatingbuffers;defaultsto262144bytes.
Tosetanewvalueforasocketoption,callthesetsockopt()function.Togetavalue,callgetsockopt().
Forexample,tohalvethesizeofthecommunicationsbuffersfrom65536to32768,andverifythatthesetting
tookeffect,insertthefollowingcod
e:
uint64 setBuf = 32768, getBuf;
/* reduce buffer to above size and check */
if (setsockopt(sockfd, afVMCI, SO_VMCI_BUFFER_SIZE, (void *)&setBuf, sizeof setBuf) == -1) {
perror(“setsockopt”);
goto close;
}
if (getsockopt(sockfd, afVMCI, SO_VMCI_BUFFER_SIZE, (void *)&getBuf, sizeof getBuf) == -1) {
perror(“getsockopt”);
goto close;
}
if (getBuf != setBuf) {
printf(stderr, “SO_VMCI_BUFFER_SIZE not set to size requested.\n”);
goto close;
}
ParameterssetBufandgetBufmustbedeclared64bit,evenon32bitsystems.
Tohaveaneffect,socketoptionsmustbesetbeforeestablishingaconnection.Thebuffersizeisnegotiated
beforetheconnectionisestablishedandstaysconsistentuntiltheconnectionisclosed.Foraserversocket,set
optionsbeforeanycl
ientestablishesaconnection.Tobesurethatthisappliestoallsockets,setoptionsbefore
callinglisten().Foraclientsocket,setoptionsbeforecallingconnect().