TCP/IP Programming Manual
#include <signal.h>
#include <stdlib.h>
#include <inet.h>
#include <nameser.h>
#include <cextdecs(FILE_CLOSE_)>
#define SERVER_PORT 7639
#define CLIENT_PORT 7739
#define MAXBUFSIZE 4096
int main (
int argc,
char **argv )
{
int s;
char databuf[MAXBUFSIZE];
int dcount;
char addrbuf[INET6_ADDRSTRLEN];
char node[MAXDNAME];
char service[MAXDNAME];
int ni;
int err;
int serveraddrlen;
char *server;
struct addrinfo *server_info;
struct addrinfo *cur_info;
struct addrinfo hints;
/* Declare the sockaddr_in6 structure. The use of this type of
structure is dictated by the communication domain of the
socket (AF_INET6), which implies communication using the IPv6
protocol. If you wanted to write a protocol-independent program,
you would declare a sockaddr_storage structure. */
struct sockaddr_in6 serveraddr;
char request[MAXBUFSIZE] = "This is the client's request";
if (argc < 2) {
printf("Usage: client <server>\n");
exit (0);
}
server = argv[1];
/* Clear the hints structure and set up hints variables. The hints
structure contains values that direct the getaddrinfo processing.
In this case, AF_INET6 returns IPv6 addresses. The AI_ADDRCONFIG
and AI_V4MAPPED values return AAAA records if an IPv6 address is
configured, and if none are found, return A records if an IPv4
address is configured. */
bzero((char *) &hints, sizeof(hints));
hints.ai_family = AF_INET6;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_ADDRCONFIG | AI_V4MAPPED;
sprintf(service, "%d", SERVER_PORT);
/* Obtains the server address. A call to getaddrinfo returns
IPv6-formatted addresses in one or more structures of type
addrinfo. */
err = getaddrinfo(server, service, &hints, &server_info);
if (err != 0) {
printf("%s\n", gai_strerror(err));
if (err == EAI_SYSTEM)
perror("getaddrinfo");
exit(2);
}
cur_info = server_info;
/* Create an AF_INET6 socket. The socket type is specified in
236 Sample Programs