9.0

Table Of Contents
VMCI Sockets Programming Guide
16 VMware, Inc.
Recv() Function
Therecv()callreadsdatafromtheclientapplication.Theserverandclientcancommunicatethelengthof
datatransmitted,ortheservercanterminateitsrecv()loopwhentheclientclosesitsconnection.
char recv_buf[BUFSIZE];
if ((numbytes = recv(sockfd, recv_buf, sizeof recv_buf, 0)) == -1) {
perror("recv");
goto close;
}
Send() Function
Thesend()callwritesdatatotheclientapplication.Serverandclientmustcommunicatethelengthofdata
transmitted,oragreebeforehandonasize.Oftentheserversendsonlyflowcontrolinformationtotheclient.
char send_buf[BUFSIZE];
if ((numbytes = send(newfd, send_buf, sizeof send_buf, 0)) == -1) {
perror("send");
goto close;
}
Close() Function
Giventheoriginalsocketdescriptorobtainedfromthesocket()call,theclose()callclosesthesocketand
terminatestheconnectionifitisstillopen.Someserverapplicationscloseimmediatelyafterreceivingclient
data,whileotherswaitforadditionalconnections.TocompileonWindows,youmustcalltheWinsock
closesocket()insteadofclose
().
#ifdef _WIN32
return closesocket(sockfd);
#else
return close(sockfd);
#endif
Theshutdown()functionislikeclose(),butshutsdowntheconnection.
Poll() Information
Notallsocketbasednetworkingprogramsusepoll(),butiftheydo,nochangesarerequired.Thepoll()
functionislikeselect().See“Select()Function”onpage 15forrelatedinformation.
Read() and Write()
Theread()andwrite()socketcallsareprovidedforconvenience.Theyprovidethesamefunctionalityas
recv()andsend().
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;
}