#include #include #include #include #include #include #include #include #define PORT 11011 int main () { struct sockaddr_in address = {0}; address.sin_family = AF_INET; address.sin_addr.s_addr = htonl(INADDR_ANY); address.sin_port = htons(PORT); int sfd = socket(PF_INET, SOCK_STREAM, 0); int optval = 1; if (sfd < 0 || setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval) < 0 || bind(sfd, (struct sockaddr *) &address, sizeof address) < 0 || listen(sfd, SOMAXCONN) < 0) { perror("socket"); exit(1); } int fd; while ((fd = accept(sfd, 0, 0)) >= 0) { char timebuf[32]; time_t clock; time(&clock); ctime_r(&clock, timebuf, sizeof timebuf); write(fd, timebuf, strlen(timebuf)); close(fd); } }