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.
- # Include<Stdlib. h>
- # Include<Stdio. h>
- # Include<Errno. h>
- # Include<String. h>
- # Include<Netdb. h>
- # Include<Sys/Types. h>
- # Include<Netinet/In. h>
- # Include<Sys/Socket. h>
- # Include<Syslog. h>
- # Include<Unistd. h>
- # Include<Netinet/In. h>
- # Include<Sys/Socket. h>
- # Include<Arpa/Inet. h>
- # Include<Errno. h>
- # Include<Sys/Ipc. h>
- # Include<Sys/Shm. h>
- /* Create a genie process */
- Int daemon_init (void)
- {Pid_t pid;
- If ((Pid=Fork())< 0) Return (-1 );
- Else if (pid! = 0) exit (0);/* parent exit */
- /* Child continues */
- Setsid ();/* become session leader */
- Chdir ("// tmp");/* change working directory */
- Umask (0);/* clear file mode creation mask */
- Close (0);/* close stdin */
- Close (1);/* close stdout */
- Close (2);/* close stderr */
- Return (0 );}
- /* Read file data */
- Void myread (char * buff)
- {
- Char buf [1024];
- FILE * fp;
- Fp=Fopen("/Proc/net/dev", "r ");
- If (! Fp)
- {
- Perror ("fopen ");
- Exit (2 );
- }
- Fgets (buf, 1024, fp );
- Fgets (buf, 1024, fp );
- Fgets (buf, 1024, fp );
- Fgets (buf, 1024, fp );
- Fscanf (fp, "% s", buf);/* read from the third row */
- Snprintf (buff, 100, "% s", buf );
- Fclose (fp );
- Printf ("% s \ n", buf );
- }
- Int main (int argc, char * argv [])
- {
- Int sockfd, new_fd;
- Struct sockaddr_in server_addr;
- Struct sockaddr_in client_addr;
- Int sin_size, portnumber;
- Char hello [2, 1024];
- /* The server starts to establish the socket descriptor */
- If ((Sockfd=Socket(AF_INET, SOCK_STREAM, 0) =-1)
- {
- Printf ("Socket error: % s \ n \ a", strerror (errno ));
- Exit (1 );
- }
- /* Fill in the sockaddr_in structure on the server */
- Bzero (& server_addr, sizeof (struct sockaddr_in ));
- Server_addr.sin_family=AF_INET;
- Server_addr.sin_addr.s_addr=Inet_addr("Local IP Address ");
- Server_addr.sin_port=Htons(10240);/* convert the port number to the network byte order */
- /* Bind the sockfd descriptor */
- If (bind (sockfd, (struct sockaddr *) (& server_addr), sizeof (struct sockaddr) =
- -1)
- {
- Printf ("Bind error: % s \ n \ a", strerror (errno ));
- Exit (1 );
- }
- /* Listen to the sockfd descriptor */
- If (listen (sockfd, 5) =-1)/* 5 indicates the maximum number of requests in the Request queue */
- {
- Printf ("Listen error: % s \ n \ a", strerror (errno ));
- Exit (1 );
- }
- While (1)
- {
- /* Server blocking until the Client Program establishes a connection */
- Sin_size=Sizeof(Struct sockaddr_in );
- If ((New_fd=Accept(Sockfd, (struct sockaddr *) (& client_addr), & sin_size
- ) =-1)
- {
- Printf ("Accept error: % s \ n \ a", strerror (errno ));
- Exit (1 );
- }
- /* Inet_ntoa is used to convert a 32-bit Ipv4 address to a corresponding dot-decimal number string */
- Printf ("Server get connection from % s \ n", inet_ntoa (client_addr.sin_addr ));
- /* Send the content of the hello character array to the client */
- Myread (hello );
- If (write (new_fd, hello, strlen (hello) =-1)
- {
- Printf ("Write Error: % s \ n", strerror (errno ));
- Exit (1 );
- }
- /* The communication has ended */
- Close (new_fd );
- }/* At the end of while */
- Close (sockfd );
- Exit (0 );
- }
- Differences between standard input implementation methods in C and C ++
- How does the C ++ compiler allocate storage space for Const constants?
- Basic Conception and method of C ++ Class Library Design
- Several Methods for converting C ++ Language
- How to better compile C ++ code