Introduction to the C ++ Manual

Source: Internet
Author: User
Tags socket error

There is no end to learning. I recently learned some knowledge about the C ++ manual and wrote two pieces of code. I hope someone will comment on it and point out the error or the location for improvement. in the process of writing, I encountered a lot of problems. If you don't want to ask for help, I am posting for help.

View the C ++ manual and view the online tutorials. however, I still don't understand many basic C ++ knowledge. you are learning. due to busy work, the learning progress is really poor, and I have made a lot of C ++ language basics. I learned a lot there. if you are planning to learn about the C ++ manual, join them.

 
 
  1. # Include<Stdlib. h>
  2. # Include<Stdio. h>
  3. # Include<Errno. h>
  4. # Include<String. h>
  5. # Include<Netdb. h>
  6. # Include<Sys/Types. h>
  7. # Include<Netinet/In. h>
  8. # Include<Sys/Socket. h>
  9. # Include<Syslog. h>
  10. # Include<Unistd. h>
  11. # Include<Netinet/In. h>
  12. # Include<Sys/Socket. h>
  13. # Include<Arpa/Inet. h>
  14. # Include<Errno. h>
  15. # Include<Sys/Ipc. h>
  16. # Include<Sys/Shm. h>
  17. /* Create a genie process */
  18. Int daemon_init (void)
  19. {Pid_t pid;
  20. If ((Pid=Fork())< 0) Return (-1 );
  21. Else if (pid! = 0) exit (0);/* parent exit */
  22. /* Child continues */
  23. Setsid ();/* become session leader */
  24. Chdir ("// tmp");/* change working directory */
  25. Umask (0);/* clear file mode creation mask */
  26. Close (0);/* close stdin */
  27. Close (1);/* close stdout */
  28. Close (2);/* close stderr */
  29. Return (0 );}
  30. /* Read file data */
  31. Void myread (char * buff)
  32. {
  33. Char buf [1024];
  34. FILE * fp;
  35. Fp=Fopen("/Proc/net/dev", "r ");
  36. If (! Fp)
  37. {
  38. Perror ("fopen ");
  39. Exit (2 );
  40. }
  41. Fgets (buf, 1024, fp );
  42. Fgets (buf, 1024, fp );
  43. Fgets (buf, 1024, fp );
  44. Fgets (buf, 1024, fp );
  45. Fscanf (fp, "% s", buf);/* read from the third row */
  46. Snprintf (buff, 100, "% s", buf );
  47. Fclose (fp );
  48. Printf ("% s \ n", buf );
  49. }
  50. Int main (int argc, char * argv [])
  51. {
  52. Int sockfd, new_fd;
  53. Struct sockaddr_in server_addr;
  54. Struct sockaddr_in client_addr;
  55. Int sin_size, portnumber;
  56. Char hello [2, 1024];
  57. /* The server starts to establish the socket descriptor */
  58. If ((Sockfd=Socket(AF_INET, SOCK_STREAM, 0) =-1)
  59. {
  60. Printf ("Socket error: % s \ n \ a", strerror (errno ));
  61. Exit (1 );
  62. }
  63. /* Fill in the sockaddr_in structure on the server */
  64. Bzero (& server_addr, sizeof (struct sockaddr_in ));
  65. Server_addr.sin_family=AF_INET;
  66. Server_addr.sin_addr.s_addr=Inet_addr("Local IP Address ");
  67. Server_addr.sin_port=Htons(10240);/* convert the port number to the network byte order */
  68. /* Bind the sockfd descriptor */
  69. If (bind (sockfd, (struct sockaddr *) (& server_addr), sizeof (struct sockaddr) =
  70. -1)
  71. {
  72. Printf ("Bind error: % s \ n \ a", strerror (errno ));
  73. Exit (1 );
  74. }
  75. /* Listen to the sockfd descriptor */
  76. If (listen (sockfd, 5) =-1)/* 5 indicates the maximum number of requests in the Request queue */
  77. {
  78. Printf ("Listen error: % s \ n \ a", strerror (errno ));
  79. Exit (1 );
  80. }
  81. While (1)
  82. {
  83. /* Server blocking until the Client Program establishes a connection */
  84. Sin_size=Sizeof(Struct sockaddr_in );
  85. If ((New_fd=Accept(Sockfd, (struct sockaddr *) (& client_addr), & sin_size
  86. ) =-1)
  87. {
  88. Printf ("Accept error: % s \ n \ a", strerror (errno ));
  89. Exit (1 );
  90. }
  91. /* Inet_ntoa is used to convert a 32-bit Ipv4 address to a corresponding dot-decimal number string */
  92. Printf ("Server get connection from % s \ n", inet_ntoa (client_addr.sin_addr ));
  93. /* Send the content of the hello character array to the client */
  94. Myread (hello );
  95. If (write (new_fd, hello, strlen (hello) =-1)
  96. {
  97. Printf ("Write Error: % s \ n", strerror (errno ));
  98. Exit (1 );
  99. }
  100. /* The communication has ended */
  101. Close (new_fd );
  102. }/* At the end of while */
  103. Close (sockfd );
  104. Exit (0 );
  105. }
  1. Differences between standard input implementation methods in C and C ++
  2. How does the C ++ compiler allocate storage space for Const constants?
  3. Basic Conception and method of C ++ Class Library Design
  4. Several Methods for converting C ++ Language
  5. How to better compile C ++ code

Related Article

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.