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. 15
Chapter 3 Creating Stream VMCI Sockets
Bind() Function
Thisbind()callassociatesthestreamsocketwiththenetworksettingsinthesockaddr_vmstructure,instead
ofthesockaddr_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),whichspecifiesthevirtualmachine.
FortheclientthisisthelocalCID.Fortheserver(listener),thiscouldbeanyconnectingvirtualmachine.Both
VMADDR_CID_ANYandVMADDR_PORT_ANYarepredefinedsothatatbindorconnectiontime,theappropriate
CIDandportnu
mberarefilledinfromtheclient.VMADDR_CID_ANYisreplacedwiththeCIDofthevirtual
machineandVMADDR_PORT_ANYprovidesanephemeralportfromthenonreservedrange(>=1024).
Theclient(connector)canobtainitslocalCIDbycallingVMCISock_GetLocalCID().
Thebind()functionisthesameasforaregu
larTCPsocketsapplication.
Listen() Function
Thelisten()callpreparestoacceptincomingclientconnections.TheBACKLOGmacropredefinesthenumber
ofincomingconnectionrequeststhatthesystemacceptsbeforerejectingnewones.Thisfunctionisthesame
aslisten()inaregularTCPsocketsapplication.
if (listen(sockfd, BACKLOG) == -1) {
perror("listen");
goto close;
}
Accept() Function
Theaccept()callwaitsindefinitelyforanincomingconnectiontoarrive,creating anewsocket(andstream
descriptornewfd)whenitdoes.Thestructuretheir_addrgetsfilledwithconnectioninformation.
struct sockaddr_vm their_addr;
socklen_t their_addr_len = sizeof their_addr;
if ((newfd = accept(sockfd, (struct sockaddr *) &their_addr, &their_addr_len)) == -1) {
perror("accept");
goto close;
}
Select() Function
Theselect()callenablesaprocesstowaitforeventsonmultiplefiledescriptorssimultaneously.This
functionhibernates,wakinguptheprocesswhenaneventoccurs.Youcanspecifyatimeoutinsecondsor
microseconds.Aftertimeout,thefunctionreturnszero.Youcanspecifytheread,write,andexceptionfile
descriptorsasNULLiftheprogramcansafelyig
norethem.
if ((select(nfds, &readfd, &writefds, &exceptfds, &timeout) == -1) {
perror("select");
goto close;
}