The Linux Programming Interface

(nextflipdebug5) #1
Sockets: Internet Domains 1225

/* Connect failed: close this socket and try next address */

close(cfd);
}

if (rp == NULL)
fatal("Could not connect socket to any address");

freeaddrinfo(result);

/* Send requested sequence length, with terminating newline */

t reqLenStr = (argc > 2)? argv[2] : "1";
if (write(cfd, reqLenStr, strlen(reqLenStr)) != strlen(reqLenStr))
fatal("Partial/failed write (reqLenStr)");
if (write(cfd, "\n", 1) != 1)
fatal("Partial/failed write (newline)");

/* Read and display sequence number returned by server */

y numRead = readLine(cfd, seqNumStr, INT_LEN);
if (numRead == -1)
errExit("readLine");
if (numRead == 0)
fatal("Unexpected EOF from server");

u printf("Sequence number: %s", seqNumStr); /* Includes '\n' */

exit(EXIT_SUCCESS); /* Closes 'cfd' */
}
––––––––––––––––––––––––––––––––––––––––––––––––––––sockets/is_seqnum_cl.c

59.12 An Internet Domain Sockets Library


In this section, we use the functions presented in Section 59.10 to implement a
library of functions to perform tasks commonly required for Internet domain sockets.
(This library abstracts many of the steps shown in the example programs presented
in Section 59.11.) Since these functions employ the protocol-independent
getaddrinfo() and getnameinfo() functions, they can be used with both IPv4 and IPv6.
Listing 59-8 shows the header file that declares these functions.
Many of the functions in this library have similar arguments:

z The host argument is a string containing either a hostname or a numeric
address (in IPv4 dotted-decimal, or IPv6 hex-string notation). Alternatively,
host can be specified as a NULL pointer to indicate that the loopback IP address is
to be used.
z The service argument is either a service name or a port number specified as a
decimal string.
z The type argument is a socket type, specified as either SOCK_STREAM or SOCK_DGRAM.
Free download pdf