IPv6 Daytime Client
Author: Eva M. Castro eva arroba gsyc.es |
The TCP and UDP versions of the daytime client program use a common function to create the client socket, the connect_client function. This function generates a client socket from a hostname, the service, socket family (IPv4 or IPv6) and socket type (TCP or UDP) parameters.
File "connect_client.h"
File "connect_client.cpp"#ifndef connect__client__h__ #define connect__client__h__ int connect_client (const char *hostname, const char *service, int family, int socktype); #endif
The TCP daytime server uses connect_client with the following input parameter values: SOCK_STREAM, PF_UNSPEC, the hostname and the port where TCP daytime server is listening. The client socket connects to this server using IPv4 or IPv6 depending on if the server hostname is resolved to an IPv4 or an IPv6 address. Clients will wait for the daytime answer from the server.#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include "connect_client.h" int connect_client (const char *hostname, const char *service, int family, int socktype) { struct addrinfo hints, *res, *ressave; int error, sockfd; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = family; hints.ai_socktype = socktype; error = getaddrinfo(hostname, service, &hints, &res); if (error != 0) { fprintf(stderr, "getaddrinfo error:: [%s]\n", gai_strerror(error)); return -1; } ressave = res; sockfd=-1; while (res) { sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (sockfd >= 0) { if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0) break; close(sockfd); sockfd=-1; } res=res->ai_next; } freeaddrinfo(ressave); return sockfd; }
File "tcp_daytime_client.cpp"
The UDP daytime server uses connect_client with the following input parameter values: SOCK_DGRAM, PF_UNSPEC, the hostname and the port where TCP daytime server is listening. And the same as in the TCP case, the client socket connects to this server using IPv4 or IPv6 depending on if the server hostname is resolved to an IPv4 or an IPv6 address. Clients will wait for the daytime answer from the server.#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <errno.h> #include <unistd.h> #include "connect_client.h" const char *DAYTIME_PORT="13"; int main(int argc, char *argv[]) { int connfd; char *myhost; char timeStr[256]; myhost = "localhost"; if (argc > 1) myhost = argv[1]; connfd= connect_client(myhost, DAYTIME_PORT, AF_UNSPEC, SOCK_STREAM); if (connfd < 0) { fprintf(stderr, "client error:: could not create connected socket " "socket\n"); return -1; } memset(timeStr, 0, sizeof(timeStr)); while (read(connfd, timeStr, sizeof(timeStr)) > 0) printf("%s", timeStr); close(connfd); return 0; }
File "udp_daytime_client.cpp"
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <errno.h> #include <unistd.h> #include "connect_client.h" const char *DAYTIME_PORT="13"; int main(int argc, char *argv[]) { int connfd, n, m; char *myhost; char timeStr[256]; char letter; myhost = "localhost"; if (argc > 1) myhost=argv[1]; connfd = connect_client(myhost, DAYTIME_PORT, AF_UNSPEC, SOCK_DGRAM); if (connfd < 0) { fprintf(stderr, "client error:: could not create connected socket " "socket\n"); return -1; } letter = '1'; m= write(connfd, &letter, sizeof(letter)); memset(timeStr, 0, sizeof(timeStr)); n = read(connfd, timeStr, sizeof(timeStr)); printf("%s\n", timeStr); close(connfd); return 0; }
Author: Eva M. Castro eva arroba gsyc.es |