获取服务器时间

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #define HOSTNAMELEN 40 #define BUFLEN 1024 #define PORT 13 int main(int argc, char *argv[]) { int rc; int sockfd; char buf[BUFLEN+1]; char *pc; struct sockaddr_in sa; struct hostent *hen; if (argc < 2) { fprintf(stderr, "missing host name "); exit(1); } hen = gethostbyname(argv[1]); if (!hen) { perror("could not resolve host name"); exit(1); } memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_port = htons(PORT); memcpy(&sa.sin_addr.s_addr, hen->h_addr_list[0], hen->h_length); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("socket()"); exit(1); } rc = connect(sockfd, (struct sockaddr*)&sa, sizeof(sa)); if (rc < 0) { perror("connect()"); exit(1); } pc = buf; while (rc = read(sockfd, pc, BUFLEN - (pc-buf))) { pc += rc; } close(sockfd); *pc = '