学无止境,最近学习了daemon,socket连接的一些知识,写了二段代码.希望有人来点评,指出其中的错误,或需要改进的地点.在写的过程当中,遇到很多问题,不段的问人,在贴求助.查看C手册,查看网上教程才得以写出. 但C的许多基础知识我还是不懂.并不段的在学习.  由于工作繁忙,学习进度实在是小得可怜.. 子 孑 [url]http://zhangjunhd.blog.51cto.com/[/url] 发了很多C语言的基础.在那里我学到了许多知识. 如果打算学习C语言的大家一起吧..我好有个伴.嘿嘿....
 
我的意图:读取/proc//net/dev的数据,在服务器端开启守护进程,当客户端通过socket 进行连接时,输出/proc/net/dev 的流量数据.(数据经过筛选)
 
server.c
InBlock.gif#include <stdlib.h>  
InBlock.gif#include <stdio.h>  
InBlock.gif#include <errno.h>  
InBlock.gif#include < string.h>  
InBlock.gif#include <netdb.h>  
InBlock.gif#include <sys/types.h>  
InBlock.gif#include <netinet/ in.h>  
InBlock.gif#include <sys/socket.h>  
InBlock.gif#include <syslog.h>  
InBlock.gif#include <unistd.h>  
InBlock.gif#include <netinet/ in.h>    
InBlock.gif#include <sys/socket.h>      
InBlock.gif#include <arpa/inet.h>      
InBlock.gif#include <errno.h>  
InBlock.gif#include <sys/ipc.h>      
InBlock.gif#include <sys/shm.h>  
InBlock.gif /*建立精灵进程*/  
InBlock.gif int daemon_init( void)  
InBlock.gif{ pid_t pid;  
InBlock.gif if((pid = fork()) < 0) return(-1);  
InBlock.gif else if(pid != 0) exit(0); /* parent exit */  
InBlock.gif /* child continues */  
InBlock.gifsetsid(); /* become session leader */  
InBlock.gifchdir( "//tmp"); /* change working directory */  
InBlock.gifumask(0); /* clear file mode creation mask */  
InBlock.gifclose(0); /* close stdin */  
InBlock.gifclose(1); /* close stdout */  
InBlock.gifclose(2); /* close stderr */  
InBlock.gifreturn(0); }  
InBlock.gif/*读取文件数据*/  
InBlock.gifvoid myread(char *buff)  
InBlock.gif{  
InBlock.gifchar buf[1024];  
InBlock.gifFILE *fp;  
InBlock.giffp = fopen("/proc/net/dev", "r");  
InBlock.gifif(!fp)  
InBlock.gif {  
InBlock.gifperror("fopen");  
InBlock.gifexit(2);  
InBlock.gif}  
InBlock.giffgets(buf, 1024, fp);  
InBlock.giffgets(buf, 1024, fp);  
InBlock.giffgets(buf, 1024, fp);  
InBlock.giffgets(buf, 1024, fp);  
InBlock.giffscanf(fp, "%s", buf);   /*从第三行开始读*/  
InBlock.gifsnprintf(buff, 100, "%s", buf);  
InBlock.giffclose(fp);  
InBlock.gifprintf("%s\n", buf);  
InBlock.gif}  
InBlock.gif  
InBlock.gifint main(int argc, char *argv[])  
InBlock.gif{  
InBlock.gifint sockfd,new_fd;  
InBlock.gifstruct sockaddr_in server_addr;  
InBlock.gifstruct sockaddr_in client_addr;  
InBlock.gifint sin_size,portnumber;  
InBlock.gifchar hello[1024];  
InBlock.gif/* 服务器端开始建立socket描述符 */  
InBlock.gifif((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)  
InBlock.gif{  
InBlock.gifprintf("Socket error:%s\n\a",strerror(errno));  
InBlock.gifexit(1);  
InBlock.gif}  
InBlock.gif/* 服务器端填充 sockaddr_in结构 */  
InBlock.gifbzero(&server_addr,sizeof(struct sockaddr_in));  
InBlock.gifserver_addr.sin_family=AF_INET;  
InBlock.gifserver_addr.sin_addr.s_addr=inet_addr("本机IP");      
InBlock.gifserver_addr.sin_port=htons(10240); /*端口号转换为网络字节序*/  
InBlock.gif/* 捆绑sockfd描述符 */  
InBlock.gifif(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==  
InBlock.gif-1)  
InBlock.gif{  
InBlock.gifprintf("Bind error:%s\n\a",strerror(errno));  
InBlock.gifexit(1);  
InBlock.gif}  
InBlock.gif/* 监听sockfd描述符 */  
InBlock.gifif(listen(sockfd,5)==-1) /*5为请求队列的最大请求数*/  
InBlock.gif{  
InBlock.gifprintf("Listen error:%s\n\a",strerror(errno));  
InBlock.gifexit(1);  
InBlock.gif}  
InBlock.gifwhile(1)  
InBlock.gif{  
InBlock.gif/* 服务器阻塞,直到客户程序建立连接 */  
InBlock.gifsin_size=sizeof(struct sockaddr_in);  
InBlock.gifif((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size  
InBlock.gif))==-1)  
InBlock.gif{  
InBlock.gifprintf("Accept error:%s\n\a",strerror(errno));  
InBlock.gifexit(1);  
InBlock.gif}  
InBlock.gif/*inet_ntoa的作用是将一个32位Ipv4地址转换为相应的点分十进制数串*/  
InBlock.gifprintf("Server get connection from %s\n",inet_ntoa(client_addr.sin_addr));  
InBlock.gif/*向客户端发送hello字符数组的内容*/  
InBlock.gifmyread(hello);  
InBlock.gifif(write(new_fd,hello,strlen(hello))==-1)  
InBlock.gif{  
InBlock.gifprintf("Write Error:%s\n",strerror(errno));  
InBlock.gifexit(1);  
InBlock.gif}  
InBlock.gif/* 这个通讯已经结束 */  
InBlock.gifclose(new_fd);  
InBlock.gif}/* while结尾处*/  
InBlock.gifclose(sockfd);  
InBlock.gifexit(0);  
InBlock.gif}  
InBlock.gif
InBlock.gif  

client.c 
InBlock.gif#include <stdlib.h>  
InBlock.gif#include <stdio.h>  
InBlock.gif#include <errno.h>  
InBlock.gif#include < string.h>                  
InBlock.gif#include <netdb.h>  
InBlock.gif#include <sys/types.h>  
InBlock.gif#include <netinet/ in.h>  
InBlock.gif#include <sys/socket.h>  
InBlock.gif#include <netdb.h>  
InBlock.gif int main( int argc, char *argv[])  
InBlock.gif{  
InBlock.gif int sockfd;  
InBlock.gif char buffer[1024];  
InBlock.gif struct sockaddr_in server_addr;  
InBlock.gif struct hostent *host;  
InBlock.gif int portnumber,nbytes;  
InBlock.gif  
InBlock.gif /* 客户程序开始建立 sockfd描述符 */  
InBlock.gif if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)  
InBlock.gif{  
InBlock.gifprintf( "Socket Error:%s\a\n",strerror(errno));  
InBlock.gifexit(1);  
InBlock.gif}  
InBlock.gif /* 客户程序填充服务端的资料 */  
InBlock.gifbzero(&server_addr, sizeof(server_addr));  
InBlock.gifserver_addr.sin_family=AF_INET;  
InBlock.gifserver_addr.sin_port=htons(10240); /*主机字节序转换为网络字节序*/  
InBlock.gifserver_addr.sin_addr.s_addr=inet_addr( "server端IP");  
InBlock.gif /* 客户程序发起连接请求 */  
InBlock.gif if(connect(sockfd,( struct sockaddr *)(&server_addr), sizeof( struct sockaddr))==-1)  
InBlock.gif{  
InBlock.gifprintf( "Connect Error:%s\a\n",strerror(errno));  
InBlock.gifexit(1);  
InBlock.gif}  
InBlock.gif /* 连接成功了 */  
InBlock.gif if((nbytes=read(sockfd,buffer,1024))==-1)  
InBlock.gif{  
InBlock.gifprintf( "Read Error:%s\n",strerror(errno));  
InBlock.gifexit(1);  
InBlock.gif}  
InBlock.gifbuffer[nbytes]='\0';  
InBlock.gifprintf( "%s\n",buffer);  
InBlock.gif /* 结束通讯 */  
InBlock.gifclose(sockfd);  
InBlock.gifexit(0);  
InBlock.gif}

 
 
欢迎大家进行测试.并指正错误!!!
谢谢!!!!