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. 21
Chapter 4 Creating Datagram VMCI Sockets
Getsockname() Function
Thegetsockname()functionretrievesthelocaladdressassociatedwithasocket.
my_addr_size = sizeof my_addr;
if (getsockname(sockfd, (struct sockaddr *) &my_addr, &my_addr_size) == -1) {
perror("getsockname");
goto close;
}
Recvfrom() Function
Therecvfrom()callreadsdatafromtheclientapplication.Serverandclientcancommunicatethelengthof
datatransmitted,ortheservercanterminateitsrecvfrom()loopwhentheclientclosesitsconnection.
if ((numbytes = recvfrom(sockfd, buf, sizeof buf, 0,
(struct sockaddr *) &their_addr, &my_addr_size)) == -1) {
perror("recvfrom");
goto close;
}
Sendto() Function
Thesendto()calloptionallywritesdatabacktotheclientapplication.See“Sendto()Function”onpage 22.
Close() Function
Theclose()callshutsdowntransmission,giventheoriginalsocketdescriptorobtainedfromthesocket()
call.Someserverapplicationscloseimmediatelyafterreceivingclientdata,whileotherswaitforadditional
connections.TocompileonWindows,youmustcalltheWinsockclosesocket()insteadofclose().
#ifdef _WIN32
return closesocket(sockfd);
#else
return close(sockfd);
#endif
Having the Client Request a Connection
Atthetopofyourapplication,includevmci_sockets.handdeclareaconstantforbuffersize.Thisdoesnot
havetobebasedonthesizeofaUDPdatagram.
#include "vmci_sockets.h"
#define BUFSIZE 4096
TocompileonWindows,youmustcalltheWinsockWSAStartup()function.See“PreparingtheServ erfora
Connection”onpage 20forsamplecode.
Socket() Function
ToalteraUDPsocketprogramforVMCIsockets,obtainthenewaddressfamilytoreplaceAF_INET.
int afVMCI = VMCISock_GetAFValue();
if ((sockfd = socket(afVMCI, SOCK_DGRAM, 0)) == -1) {
perror("socket");
goto exit;
}