博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
从零开始学习C语言(二)之daemon,socket连接
阅读量:7036 次
发布时间:2019-06-28

本文共 3786 字,大约阅读时间需要 12 分钟。

学无止境,最近学习了daemon,socket连接的一些知识,写了二段代码.希望有人来点评,指出其中的错误,或需要改进的地点.在写的过程当中,遇到很多问题,不段的问人,在贴求助.查看C手册,查看网上教程才得以写出. 但C的许多基础知识我还是不懂.并不段的在学习.  由于工作繁忙,学习进度实在是小得可怜.. 子 孑  发了很多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}
 
 
欢迎大家进行测试.并指正错误!!!
谢谢!!!!
本文转自守住每一天51CTO博客,原文链接:http://blog.51cto.com/liuyu/64075,如需转载请自行联系原作者
你可能感兴趣的文章
DeepLearning.ai学习笔记(四)卷积神经网络 -- week2深度卷积神经网络 实例探究
查看>>
ixchariot安装、测试IPV6
查看>>
Unity 优化之25种在渲染中不能合批的原因
查看>>
架构师杂谈JVM之JIT
查看>>
.NET Core实战项目之CMS 第四章 入门篇-Git的快速入门及实战演练
查看>>
Keras和TensorFlow之争何时休?
查看>>
WPF 一个弧形手势提示动画
查看>>
约三分之二的 DDoS 攻击指向通信服务提供商
查看>>
优化 Join 运算的系列方法(1)
查看>>
超简单 图解 三级域名解析
查看>>
App Annie:2016年全球移动应用市场数据解读
查看>>
Docker - 通往新世界的大门
查看>>
Linux磁盘高速缓存
查看>>
甲骨文第二财季SAAS和PAAS云业务收入上升81% 按非GAAP固定汇率计算上升89%
查看>>
前端打包/自动化构建工具:gulp
查看>>
Ubuntu 16.04中iptables的工具简介(iptables/iptables-restore/iptables-xml/iptables-apply/iptables-save)...
查看>>
野心不小?国美首家专业VR影院落地北京
查看>>
DT时代:力维云数据驱动高效运营
查看>>
用 Weave Scope 监控集群 - 每天5分钟玩转 Docker 容器技术(175)
查看>>
poj 1046 Color Me Less(水题)
查看>>