#include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int sock; struct sockaddr_in saddr; struct hostent *shost; char *host, buf[256]; char *p; if (argc == 1) { if ((host = getenv("DAYTIMESERVER")) == NULL) host = "localhost"; } else { host = argv[1]; } shost = gethostbyname(host); if (shost) { saddr.sin_family = AF_INET; saddr.sin_port = htons(13); memcpy(&saddr.sin_addr, shost->h_addr, shost->h_length); } else { fprintf(stderr, "%s: host name lookup failed.\n", host); return 1; } if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { fprintf(stderr, "can't create socket.\n"); return 1; } if (connect(sock, (struct sockaddr *)&saddr, sizeof(struct sockaddr)) < 0) { fprintf(stderr, "%s: unable to connect: %s\n", host, strerror(errno)); return 1; } read(sock, buf, sizeof(buf)); for (p = buf; *p; p++) { if (*p == '\n') { *p = '\0'; break; } } printf("%s\n", buf); close(sock); return 0; }