学无止境,最近学习了daemon,socket连接的一些知识,写了二段代码.希望有人来点评,指出其中的错误,或需要改进的地点.在写的过程当中,遇到很多问题,不段的问人,在贴求助.查看C手册,查看网上教程才得以写出. 但C的许多基础知识我还是不懂.并不段的在学习. 由于工作繁忙,学习进度实在是小得可怜.. 子 孑
[url]http://zhangjunhd.blog.51cto.com/[/url]
发了很多C语言的基础.在那里我学到了许多知识. 如果打算学习C语言的大家一起吧..我好有个伴.嘿嘿....
我的意图:读取/proc//net/dev的数据,在服务器端开启守护进程,当客户端通过socket 进行连接时,输出/proc/net/dev 的流量数据.(数据经过筛选)
server.c
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <stdlib.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <stdio.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <errno.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <
string.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <netdb.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <sys/types.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <netinet/
in.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <sys/socket.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <syslog.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <unistd.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <netinet/
in.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <sys/socket.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <arpa/inet.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <errno.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <sys/ipc.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <sys/shm.h>
/*建立精灵进程*/
int daemon_init(
void)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
{ pid_t pid;
if((pid = fork()) < 0)
return(-1);
else
if(pid != 0) exit(0);
/* parent exit */
/* child continues */
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
setsid();
/* become session leader */
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
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); }
/*读取文件数据*/
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];
/* 服务器端开始建立socket描述符 */
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("Socket error:%s\n\a",strerror(errno));
exit(1);
}
/* 服务器端填充 sockaddr_in结构 */
bzero(&server_addr,sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=inet_addr("本机IP");
server_addr.sin_port=htons(10240); /*端口号转换为网络字节序*/
/* 捆绑sockfd描述符 */
if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==
-1)
{
printf("Bind error:%s\n\a",strerror(errno));
exit(1);
}
/* 监听sockfd描述符 */
if(listen(sockfd,5)==-1) /*5为请求队列的最大请求数*/
{
printf("Listen error:%s\n\a",strerror(errno));
exit(1);
}
while(1)
{
/* 服务器阻塞,直到客户程序建立连接 */
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的作用是将一个32位Ipv4地址转换为相应的点分十进制数串*/
printf("Server get connection from %s\n",inet_ntoa(client_addr.sin_addr));
/*向客户端发送hello字符数组的内容*/
myread(hello);
if(write(new_fd,hello,strlen(hello))==-1)
{
printf("Write Error:%s\n",strerror(errno));
exit(1);
}
/* 这个通讯已经结束 */
close(new_fd);
}/* while结尾处*/
close(sockfd);
exit(0);
}
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
client.c
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <stdlib.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <stdio.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <errno.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <
string.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <netdb.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <sys/types.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <netinet/
in.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <sys/socket.h>
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
#include <netdb.h>
int main(
int argc,
char *argv[])
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
{
int sockfd;
char buffer[1024];
struct sockaddr_in server_addr;
struct hostent *host;
int portnumber,nbytes;
/* 客户程序开始建立 sockfd描述符 */
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
{
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
printf(
"Socket Error:%s\a\n",strerror(errno));
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
exit(1);
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
}
/* 客户程序填充服务端的资料 */
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
bzero(&server_addr,
sizeof(server_addr));
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
server_addr.sin_family=AF_INET;
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
server_addr.sin_port=htons(10240);
/*主机字节序转换为网络字节序*/
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
server_addr.sin_addr.s_addr=inet_addr(
"server端IP");
/* 客户程序发起连接请求 */
if(connect(sockfd,(
struct sockaddr *)(&server_addr),
sizeof(
struct sockaddr))==-1)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
{
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
printf(
"Connect Error:%s\a\n",strerror(errno));
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
exit(1);
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
}
/* 连接成功了 */
if((nbytes=read(sockfd,buffer,1024))==-1)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
{
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
printf(
"Read Error:%s\n",strerror(errno));
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
exit(1);
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
}
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
buffer[nbytes]='\0';
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
printf(
"%s\n",buffer);
/* 结束通讯 */
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
close(sockfd);
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
exit(0);
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
}
欢迎大家进行测试.并指正错误!!!
谢谢!!!!
转载于:https://blog.51cto.com/liuyu/64075