9.0

Table Of Contents
VMCI Sockets Programming Guide
28 VMware, Inc.
Thisissimilartotheaccept()systemcall,exceptthattheclientdoesnothavetobindalocaladdresstothe
socketdescriptorbeforecallingconnect().Theserveraddresspointedtobysrv_addrmustexist.
Forexample:
#define SERV_PORT 5432
unsigned long inet_addr(char *ptr);
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERV_PORT):
serv_addr.sin_addr.s_addr = inet_addr(SERV_HOST_ADDR);
connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
Socket Read and Write
SocketsusethesamereadandwritesystemcallsasforfileI/O.
Thefirstparameteristhesocketdescriptorfromthesocket()call,sockfd.
Thesecondparameteristhereadorwritebuffer.
Thethirdparameteristhenumberofbytestoread.
UnlikefileI/O,areadorwritesystemcallonastreamsocketmayresultinfewerbytesthanrequested.Itisthe
programmerʹsresponsibilitytoaccountforvaryingnumberofbytesreadorwrittenonthesocket.
Forexample:
nleft = nbytes;
while (nleft > 0) {
if ((nread = read(sockfd, buf, nleft)) < 0)
return(nread); /* error */
else if (nread == 0)
break; /* EOF */
/* nread > 0. update nleft and buf pointer */
nleft - = nread;
buf += nread;
}