HP-UX IPv6 Porting Guide (February 2007)
Sample Client/Server Programs
IPv4 TCP Server Code Fragment
Chapter 15 61
IPv4 TCP Server Code Fragment
This code fragment is part of the same example IPv4 server program that ships in the HP-UX
11i v2 /usr/lib/demos/networking/socket directory.
struct sockaddr_in6 peeraddr_in6; /* for peer socket address */
sp = getservbyname ("example", "tcp");
if (sp == NULL) {
fprintf(stderr, "%s: example not found in /etc/services\n",argv[0]);
exit(1);
}
myaddr_in.sin_port = sp->s_port;
/* Create the listen socket. */
ls = socket (AF_INET, SOCK_STREAM, 0);
if (ls == -1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to create socket\n", argv[0]);
exit(1);
}
/* Bind the listen address to the socket. */
if (bind(ls, &myaddr_in, sizeof(struct sockaddr_in)) == -1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to bind address\n", argv[0]);
exit(1);
}
/* Initiate the listen on the socket so remote users
* can connect. The listen backlog is set to 5, which
* is within the supported range of 1 to 20.
*/
if (listen(ls, 5) == -1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to listen on socket\n", argv[0]);
exit(1);
}