- 论坛徽章:
- 0
|
《Linux程序设计》第三版,第15章最后关于通过UDP获取系统时间的例子。下面是完整代码:
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- int main(int argc, char *argv[])
- {
- char *host;
- int sockfd;
- int len, result;
- struct sockaddr_in address;
- struct hostent *hostinfo;
- struct servent *servinfo;
- char buffer[128];
- if(argc == 1)
- {
- host = "localhost";
- }
- else
- {
- host = argv[1];
- }
- hostinfo = gethostbyname(host);
- if(!hostinfo)
- {
- fprintf(stderr, "no host: %s\n", host);
- exit(1);
- }
- servinfo = getservbyname("daytime", "udp");
- if(!servinfo)
- {
- fprintf(stderr, "no daytime service\n");
- exit(1);
- }
- printf("daytime port is %d\n", ntohs(servinfo->s_port));
- sockfd = socket(AF_INET, SOCK_DGRAM, 0);
- address.sin_family = AF_INET;
- address.sin_port = servinfo->s_port;
- address.sin_addr = *(struct in_addr *)*hostinfo->h_addr_list;
- len = sizeof(address);
- result = sendto(sockfd, buffer, 1, 0, (struct sockaddr *)&address, len);
- result = recvfrom(sockfd, buffer, sizeof(buffer), 0,
- (struct sockaddr *)&address, &len);
- buffer[result] = '\0';
- printf("read %d bytes: %s\n",result, buffer);
- close(sockfd);
- exit(0);
- }
复制代码
按照书上的说明,在/etc/inetd.conf中加入daytime UDP服务,加入下面这行
daytime dgram udp wait root internal
但是程序一直阻塞在recvfrom处。。。。为什么会这样? |
|