Example of running in chapter 16 of apue16-14

Source: Internet
Author: User
Tags syslog

Reference: http://blog.csdn.net/andyxie407/article/details/1672325

Today, I encountered a lot of problems running on the 16-14 (client) and 16-15 (server) In chapter 16th of apue, run the method thanks to the andyxie407 article for a good reference (originally the same, not for reference), but still cannot run the results, the results found the wrong place of the two programs (forgive me, sometimes, I think the code will be knocked out at a glance.) I write it all here, and I will show the painful running results. The Code is as follows:

 

[CPP]View plaincopy
  1. // 16-14.c is the client
  2. # Include "apue. H"
  3. # Include <netdb. h>
  4. # Include <errno. h>
  5. # Include <sys/socket. h>
  6. # Define maxaddrlen 256
  7. # Define buflen 128
  8. # Define maxsllep 128
  9. Int connect_retry (INT sockfd, struct sockaddr * ADDR, socklen_t Alen)
  10. {
  11. Int nsec;
  12. For (nsec = 1; nsec <maxsllep; nsec <= 1) {// nsec <= 1 equal to nsec = nsec <1 equal to nsec/= 2;
  13. If (connect (sockfd, ADDR, Alen) = 0)
  14. Return 0;
  15. If (nsec <= maxsllep/2)
  16. Sleep (nsec );
  17. }
  18. Return-1;
  19. }
  20. Void print_uptime (INT sockfd)
  21. {
  22. Char Buf [buflen];
  23. Int N;
  24. While (n = Recv (sockfd, Buf, buflen, 0)> 0)
  25. Write (stdout_fileno, Buf, N );
  26. If (n <0)
  27. Err_sys ("Recv error ");
  28. }
  29. Int main (INT argc, char * argv [])
  30. {
  31. Struct addrinfo * AIP, * ailist;
  32. Struct addrinfo hint;
  33. Int err, sockfd;
  34. If (argc! = 2)
  35. Err_quit ("Usage: ruptime hostname ");
  36. Hint. ai_family = 0;
  37. Hint. ai_flags = 0; // ai_canonname;
  38. Hint. ai_socktype = sock_stream;
  39. Hint. ai_protocol = 0;
  40. Hint. ai_addrlen = 0;
  41. Hint. ai_canonname = NULL;
  42. Hint. ai_addr = NULL;
  43. Hint. ai_next = NULL;
  44. If (ERR = getaddrinfo (argv [1], "ruptime", & hint, & ailist ))! = 0)
  45. Err_quit ("getaddrinfo: % s", gai_strerror (ERR ));
  46. For (AIP = ailist; AIP! = NULL; AIP = AIP-> ai_next ){
  47. If (sockfd = socket (AIP-> ai_family, sock_stream, 0) <0 ){
  48. Err = errno;
  49. }
  50. If (connect_retry (sockfd, AIP-> ai_addr, AIP-> ai_addrlen) <0)
  51. Err = errno;
  52. Else {
  53. Printf ("connect to ruptime server successfully! \ N ");
  54. Print_uptime (sockfd );
  55. Exit (0 );
  56. }
  57. }
  58. Fprintf (stderr, "cannot connect to % s: % s \ n", argv [1], strerror (ERR ));
  59. Exit (1); // err happened
  60. }
[CPP]View plaincopy
  1. // 16-15.c is the server
  2. # Include "apue. H"
  3. # Include <netdb. h>
  4. # Include <syslog. h>
  5. # Include <errno. h>
  6. # Include <sys/socket. h>
  7. # Define buflen 128
  8. # Define qlen 10
  9. # Ifndef host_name_max
  10. # Define host_name_max 256
  11. # Endif
  12. Int init_server (INT type, struct sockaddr * ADDR, socklen_t Alen, int qlen)
  13. {
  14. Int FD, err = 0;
  15. If (FD = socket (ADDR-> sa_family, type, 0) <0)
  16. Return-1;
  17. If (BIND (FD, ADDR, Alen) <0 ){
  18. Err = errno;
  19. Goto errout;
  20. }
  21. If (type = sock_stream | type = sock_seqpacket ){
  22. If (Listen (FD, qlen) <0 ){
  23. Err = errno;
  24. Goto errout;
  25. }
  26. }
  27. Return FD; // successful when prog arrive here.
  28. Errout:
  29. Close (FD); // exit with failuare
  30. Errno = err;
  31. Return-1;
  32. }
  33. Void serve (int fd)
  34. {
  35. Char Buf [buflen];
  36. Int clfd;
  37. File * FP;
  38. For (;;){
  39. If (clfd = accept (FD, null, null) <0 ){
  40. Syslog (log_err, "ruptime: accept error: % s", strerror (errno ));
  41. Exit (1 );
  42. }
  43. If (FP = popen ("/usr/bin/uptime", "R") = NULL ){
  44. Sprintf (BUF, "error: % s \ n", strerror (errno ));
  45. Send (clfd, Buf, strlen (BUF), 0 );
  46. } Else {
  47. While (fgets (BUF, buflen, FP )! = NULL)
  48. Send (clfd, Buf, strlen (BUF), 0 );
  49. Pclose (FP );
  50. }
  51. Close (clfd); // send end
  52. }
  53. }
  54. Int main (INT argc, char * argv [])
  55. {
  56. Struct addrinfo * ailist, * AIP;
  57. Struct addrinfo hint;
  58. Int err, sockfd, N;
  59. Char * Host;
  60. If (argc! = 1)
  61. Err_quit ("Usage: Service ");
  62. # Ifdef _ SC _host_name_max
  63. N = sysconf (_ SC _host_name_max );
  64. If (n <0)
  65. # Endif
  66. N = host_name_max;
  67. Host = malloc (N );
  68. If (! Host)
  69. Err_sys ("malloc error ");
  70. If (gethostname (host, n) <0)
  71. Err_sys ("gethostname ");
  72. Printf ("host name is: % s \ n", host );
  73. Daemonize ("ruptimed ");
  74. Hint. ai_flags = ai_canonname;
  75. Hint. ai_family = 0;
  76. Hint. ai_socktype = sock_stream;
  77. Hint. ai_protocol = 0;
  78. Hint. ai_addrlen = 0;
  79. Hint. ai_canonname = NULL;
  80. Hint. ai_addr = NULL;
  81. Hint. ai_next = NULL;
  82. If (ERR = getaddrinfo (host, "ruptime", & hint, & ailist ))! = 0 ){
  83. Syslog (log_err, "ruptime: getaddrinfo error: % s", gai_strerror (ERR ));
  84. Exit (1 );
  85. }
  86. For (AIP = ailist; AIP! = NULL; AIP = AIP-> ai_next ){
  87. Sockfd = init_server (sock_stream, AIP-> ai_addr, AIP-> ai_addrlen, qlen );
  88. If (sockfd> = 0 ){
  89. Serve (sockfd );
  90. Exit (0 );
  91. }
  92. }
  93. Exit (1 );
  94. }

Compile the file into an executable file, and then run the server./ruptimed. You can see the error through/var/log/syslog.

Servname not supported for ai_socktype.

Solution: First Add the following content to/etc/services:

Ruptime 4000/tcp

Save and exit, but make sure that port 4000 is not occupied by other services. Otherwise, use another port. Run

$./Ruptimed

This is the name compiled in 16-15. You can see through PS-EF that the program has become a background program and then run it.

$./Ruptime Ubuntu

The previous one is the name compiled in 16-14, and the later one is the user/Host Name (as mentioned in the previous article, but localhost or 127.0.0.1 cannot be used here

The result is displayed.

12:06:45 up, 2 users, load average: 0.00, 0.01, 0.05

Very excited!

You do not need to change/etc/services. You need to change the service name in the program to the port number and then modify other content. For more information, see references!

By the way, remind yourself that when you cannot ensure the correctness of your program (even if the program is compiled, it cannot indicate that there is no error or something missing), do not look for any reason other than the program, remember!

Example of running in chapter 16 of apue16-14

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.