9.0
Table Of Contents
- VMCI Sockets Programming Guide
- Contents
- About This Book
- About VMCI Sockets
- Porting to VMCI Sockets
- Creating Stream VMCI Sockets
- Creating Datagram VMCI Sockets
- Security of the VMCI Device
- Appendix: Learning More About Sockets
- Index
VMware, Inc. 17
Chapter 3 Creating Stream VMCI Sockets
Having the Client Request a Connection
Atthetopofyourapplication,includevmci_sockets.handdeclareaconstantforthesocketbuffersize.In
theexamplebelow,BUFSIZEdefinesthesocketbuffersize.ItisnotbasedonthesizeofaTCPpacket.
#include "vmci_sockets.h"
#define BUFSIZE 4096
TocompileonWindows,youmustcalltheWinsockWSAStartup()function.See“PreparingtheServ erfora
Connection”onpage 14forsamplecode.
Socket() Function
InaVMCIsocketsapplication,obtainthenewaddressfamily(domain)toreplaceAF_INET.
int afVMCI = VMCISock_GetAFValue();
if ((sockfd = socket(afVMCI, SOCK_STREAM, 0)) == -1) {
perror("socket");
goto exit;
}
VMCISock_GetAFValue()returnsadescriptorfortheVMCIsocketsaddressfamilyifavailable.
Connect() Function
Theconnect()callrequestsasocketconnectiontotheserverspecifiedbyCIDinthesockaddr_vmstructure,
insteadofbytheIPaddressinthesockaddr_instructure.
struct sockaddr_vm their_addr = {0};
their_addr.svm_family = afVMCI;
their_addr.svm_cid = SERVER_CID;
their_addr.svm_port = SERVER_PORT;
if ((connect(sockfd, (struct sockaddr *) &their_addr, sizeof their_addr)) == -1) {
perror("connect");
goto close;
}
Thesockaddr_vmstructurecontainsanelementforthecontextID(CID)tospecifythevirtualmachineorhost.
TheclientmakingaconnectionshouldprovidetheCIDofaremotevirtualmachineorhost.
Theportnumberisarbitrary,althoughserver(listener)andclient(connector)mustusethesamenumber,
whichmustde
signateaportnotalreadyinuse.Onlyprivilegedprocessescanuseports<1024.
Theconnect()callallowsyoutousesend()andrecv()functionsinsteadofsendto()andrecvfrom().
Theconnect()callisnotnecessaryfordatagramsockets.
Send() Function
Thesend()callwritesdatatotheserverapplication.Theclientandservercancommunicatethelengthofdata
transmitted,ortheservercanterminateitsrecv()loopwhentheclientclosesitsconnection.
char send_buf[BUFSIZE];
/* Initialize send_buf with your data. */
if ((numbytes = send(sockfd, send_buf, sizeof send_buf, 0)) == -1) {
perror("send");
goto close;
}