一个小并发服务器的代码优化
2014-03-26 15:19
288 查看
while(1){
//服务器阻塞,直到客户程序建立连接
sin_size=sizeof(struct sockaddr_in);
if ((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),(socklen_t *)&sin_size))==-1)
{
fprintf(stderr,"Accept error:%s\n\a",strerror(errno));
exit(1);
}
fprintf(stdout,"Server get connection from %s\n", inet_ntoa(client_addr.sin_addr));
//子进程处理接入的TCP连接
//父进程继续监听
pid_t pid = fork();
if (pid < 0){
perror("call to fork");
exit(1);
}
else if (0 == pid){
while(1){
//主动上传变化的数据报文
//dataup_104frame(new_fd);
/****************************************************************/
**对于非正常断开的连接,子进程还不能够退出,导致子进程越来越多
**getsockopt函数是否可以获得socket状态???
//检查TCP连接状态,断开已中断的连接
//处理接收到的104报文
if(!new_fd){
exit(1);
}
if((recbytes=recv(new_fd, buf, 1024, 0)) <= 0){
perror("call to recv");
close(new_fd);
exit(1);
}
/*****************************************************************/
process_104frame(new_fd, buf, recbytes);
}
}
else{
close(new_fd);
fprintf(stdout, "IEC104 server continues listening\n\r");
}
}
//服务器阻塞,直到客户程序建立连接
sin_size=sizeof(struct sockaddr_in);
if ((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),(socklen_t *)&sin_size))==-1)
{
fprintf(stderr,"Accept error:%s\n\a",strerror(errno));
exit(1);
}
fprintf(stdout,"Server get connection from %s\n", inet_ntoa(client_addr.sin_addr));
//子进程处理接入的TCP连接
//父进程继续监听
pid_t pid = fork();
if (pid < 0){
perror("call to fork");
exit(1);
}
else if (0 == pid){
while(1){
//主动上传变化的数据报文
//dataup_104frame(new_fd);
/****************************************************************/
**对于非正常断开的连接,子进程还不能够退出,导致子进程越来越多
**getsockopt函数是否可以获得socket状态???
//检查TCP连接状态,断开已中断的连接
//处理接收到的104报文
if(!new_fd){
exit(1);
}
if((recbytes=recv(new_fd, buf, 1024, 0)) <= 0){
perror("call to recv");
close(new_fd);
exit(1);
}
/*****************************************************************/
process_104frame(new_fd, buf, recbytes);
}
}
else{
close(new_fd);
fprintf(stdout, "IEC104 server continues listening\n\r");
}
}
相关文章推荐
- 服务器并发由200到4000并发的一个优化
- 4、搭建一个后台服务器--服务端代码(异步,大并发)
- 搭建一个后台服务器--服务端代码(异步,大并发)
- JavaScript 代码性能优化总结 && 一个最简单的web服务器
- 搭建一个后台服务器--服务端代码(异步,大并发)
- TCP并发服务器(七)——可动态增减的线程池,主线程accept——基于UNP代码修改
- 一个选择最快的服务器转向代码
- 刚接触java,写的一个并发访问网站计时的程序,总是捕捉到服务器返回不正确的状态码
- 关于搭建一个高性能网站的服务器的架设思路以及nginx测试的并发
- 一个页面显示2个数据库的查询内容 代码优化
- 好文转贴(3) —— 每个应用程序都有一个恐怖地下室 & 两个最容易被人忽略的基本代码优化技术
- Linux(Centos )的网络内核参数优化来提高服务器并发处理能力【转】
- MarioTCP:一个单机可日30亿的百万并发长连接服务器
- 模块化设计,面向接口开发。不断重构设计和代码,一个大幅提高开发效率的好方法:不断重构既有代码和设计,为复用准备。同时也是优化。
- 服务器高并发优化
- 优化Linux的内核参数来提高服务器并发处理能力
- Node.js用6行代码1个JS文件搭建一个HTTP静态服务器
- 记录一个关于互联网、网页设计、Web开发、服务器运维优化、项目管理、网站运营、网站安全的网站
- 从数据库、代码和服务器对PHP网站Mysql做性能优化
- 优化Linux的内核参数来提高服务器并发处理能力