對(duì)C++手冊(cè)內(nèi)容簡(jiǎn)介
學(xué)無(wú)止境,最近學(xué)習(xí)了C++手冊(cè)的一些知識(shí),寫了二段代碼.希望有人來(lái)點(diǎn)評(píng),指出其中的錯(cuò)誤,或需要改進(jìn)的地點(diǎn).在寫的過(guò)程當(dāng)中,遇到很多問(wèn)題,不段的問(wèn)人,在貼求助。
查看C++手冊(cè),查看網(wǎng)上教程才得以寫出. 但C++的許多基礎(chǔ)知識(shí)我還是不懂.并不段的在學(xué)習(xí). 由于工作繁忙,學(xué)習(xí)進(jìn)度實(shí)在是小得可憐,發(fā)了很多C++語(yǔ)言的基礎(chǔ).在那里我學(xué)到了許多知識(shí). 如果打算學(xué)習(xí)C++手冊(cè)的成員們就一起吧。
- #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>
 - /*建立精靈進(jìn)程*/
 - 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("http://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); }
 - /*讀取文件數(shù)據(jù)*/
 - 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); /*從第三行開始讀*/
 - 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[1024];
 - /* 服務(wù)器端開始建立socket描述符 */
 - if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
 - {
 - printf("Socket error:%s\n\a",strerror(errno));
 - exit(1);
 - }
 - /* 服務(wù)器端填充 sockaddr_in結(jié)構(gòu) */
 - bzero(&server_addr,sizeof(struct sockaddr_in));
 - server_addr.sin_family=AF_INET;
 - server_addr.sin_addr.s_addr=inet_addr("本機(jī)IP");
 - server_addr.sin_port=htons(10240); /*端口號(hào)轉(zhuǎn)換為網(wǎng)絡(luò)字節(jié)序*/
 - /* 捆綁sockfd描述符 */
 - if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==
 - -1)
 - {
 - printf("Bind error:%s\n\a",strerror(errno));
 - exit(1);
 - }
 - /* 監(jiān)聽sockfd描述符 */
 - if(listen(sockfd,5)==-1) /*5為請(qǐng)求隊(duì)列的***請(qǐng)求數(shù)*/
 - {
 - printf("Listen error:%s\n\a",strerror(errno));
 - exit(1);
 - }
 - while(1)
 - {
 - /* 服務(wù)器阻塞,直到客戶程序建立連接 */
 - 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的作用是將一個(gè)32位Ipv4地址轉(zhuǎn)換為相應(yīng)的點(diǎn)分十進(jìn)制數(shù)串*/
 - printf("Server get connection from %s\n",inet_ntoa(client_addr.sin_addr));
 - /*向客戶端發(fā)送hello字符數(shù)組的內(nèi)容*/
 - myread(hello);
 - if(write(new_fd,hello,strlen(hello))==-1)
 - {
 - printf("Write Error:%s\n",strerror(errno));
 - exit(1);
 - }
 - /* 這個(gè)通訊已經(jīng)結(jié)束 */
 - close(new_fd);
 - }/* while結(jié)尾處*/
 - close(sockfd);
 - exit(0);
 - }
 
【編輯推薦】















 
 
 
 
 
 
 