HP-UX IPv6 Porting Guide (September 2004)

Table Of Contents
Sample Client/Server Programs
IPv4 TCP Client Code Fragment
Chapter 15 61
IPv4 TCP Client Code Fragment
This code fragment is part of the same IPv4 client program that ships in the HP-UX 11i IPv6
/usr/lib/demos/networking/socket directory.
The client requests a service called “example.” Add an entry to the /etc/services for
example”. Assign any unused port number, such as 22375, to the service example for a port
address. The host running the server must also have the same port number assigned to
example” in the /etc/services file.
struct sockaddr_in peeraddr_in; /* for peer socket address */
memset ((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in));
hp = gethostbyname (argv[1]);
if (hp == NULL) {
fprintf(stderr, "%s: %s not found in /etc/hosts\n",
argv[0], argv[1]);
exit(1);
}
peeraddr_in.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
/* Find the information for the "example" server
* in order to get the needed port number.
*/
sp = getservbyname ("example", "tcp");
if (sp == NULL) {
fprintf(stderr, "%s: example not found in /etc/services\n argv[0]);
exit(1);
}
peeraddr_in.sin_port = sp->s_port;
/* Create the socket. */
s = socket (AF_INET, SOCK_STREAM, 0);
if (s == -1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to create socket\n", argv[0]);
exit(1);
}
/* Try to connect to the remote server at the address put in peeraddr.
*/
if (connect(s, &peeraddr_in, sizeof(struct sockaddr_in)) == -1{