#include <time.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/socket.h> #define MAXLINE 4096 char *sock_ntop(const struct sockaddr *, socklen_t); void daemon_inetd(const char *, int); void err_msg(const char *, ...); int main(int argc, char **argv) { socklen_t len; struct sockaddr *cliaddr; char buff[MAXLINE]; time_t ticks; daemon_inetd(argv[0], 0); cliaddr = malloc(sizeof(struct sockaddr_storage)); len = sizeof(struct sockaddr_storage); getpeername(0, cliaddr, &len); err_msg("connection from %s", sock_ntop(cliaddr, len)); ticks = time(NULL); snprintf(buff, sizeof(buff), "%.24s ", ctime(&ticks)); write(0, buff, strlen(buff)); close(0); /* close TCP connection */ exit(0); }