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
VMCI Sockets Programming Guide
22 VMware, Inc.
Sendto() Function
Becausethisisaconnectionlessprotocol,youpassthesocketaddressstructuretheir_addrasaparameterto
thesendto()call.
struct sockaddr_vm their_addr = {0};
their_addr.svm_family = afVMCI;
their_addr.svm_cid = SERVER_CID;
their_addr.svm_port = SERVER_PORT;
if ((numbytes = sendto(sockfd, buf, BUFIZE, 0,
(struct sockaddr *) &their_addr, sizeof their_addr)) == -1) {
perror("sendto");
goto close;
}
Thesockaddr_vmstructurecontainsanelementfortheCIDtospecifythevirtualmachine.Fortheclient
makingaconnection,theVMCISock_GetLocalCID()functionreturnstheCIDofthevirtualmachine.
Theportnumberisarbitrary,althoughtheserver(listener)andclient(connector)mustusethesamenumber,
whichmustdesignateaportno
talreadyinuse.Onlyprivilegedprocessescanuseports<1024.
Connect() and Send()
Evenwiththisconnectionlessprotocol,applicationscancalltheconnect()functiononcetosettheaddress,
andcallthesend()functionrepeatedlywithouthavingtospecifythesendto()addresseachtime.
if ((connect(sockfd, (struct sockaddr *) &their_addr, sizeof their_addr)) == -1) {
perror("connect");
goto close;
}
if ((numbytes = send(sockfd, send_buf, BUFSIZE, 0)) == -1) {
perror("send");
goto close;
}
Recvfrom() Function
Therecvfrom()calloption allyreadsdatafromtheserverapplication.See“Recvfrom()Function”onpage 21.
Close() Function
Theclose()callshutsdownaconnection,giventheoriginalsocketdescriptorobtainedfromthesocket()
function.TocompileonWindows,calltheWinsockclosesocket(),asshownin“Close()Function”on
page 21.