Linux下的echo服务器
epoll模式下的echo服务器,忘记从哪个网页上粘贴过来的了,学习一下
/*
* main.cc
*
* Created on: 2009-11-30
* Author: liheyuan
* Describe: epoll实现阻塞模式服务器(Echo服务器)
*
* Last Date: 2009-11-30
* CopyRight: 2009 @ ICT LiHeyuan
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include <errno.h> #define EPOLL_SIZE 10
#define EVENT_ARR 20
#define BACK_QUEUE 10
#define PORT 18001
#define BUF_SIZE 16 void setnonblocking(int sockFd) {
int opt; //获取sock原来的flag
opt = fcntl(sockFd, F_GETFL);
if (opt < 0) {
printf("fcntl(F_GETFL) fail.");
exit(-1);
} //设置新的flag,非阻塞
opt |= O_NONBLOCK;
if (fcntl(sockFd, F_SETFL, opt) < 0) {
printf("fcntl(F_SETFL) fail.");
exit(-1);
}
} int main() { int serverFd; //创建服务器fd
serverFd = socket(AF_INET, SOCK_STREAM, 0);
setnonblocking(serverFd); //创建epoll,并把serverFd放入监听队列
int epFd = epoll_create(EPOLL_SIZE);
struct epoll_event ev, evs[EVENT_ARR];
ev.data.fd = serverFd;
ev.events = EPOLLIN | EPOLLET;
epoll_ctl(epFd, EPOLL_CTL_ADD, serverFd, &ev); //绑定服务器端口
struct sockaddr_in serverAddr;
socklen_t serverLen = sizeof(struct sockaddr_in);
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddr.sin_port = htons(PORT);
if (bind(serverFd, (struct sockaddr *) &serverAddr, serverLen)) {
printf("bind() fail.\n");
exit(-1);
} //打开监听
if (listen(serverFd, BACK_QUEUE)) {
printf("Listen fail.\n");
exit(-1);
} //死循环处理
int clientFd;
sockaddr_in clientAddr;
socklen_t clientLen;
char buf[BUF_SIZE];
while (1) {
//等待epoll事件的到来,最多取EVENT_ARR个事件
int nfds = epoll_wait(epFd, evs, EVENT_ARR, -1);
//处理事件
for (int i = 0; i < nfds; i++) {
if (evs[i].data.fd == serverFd && evs[i].data.fd & EPOLLIN) {
//如果是serverFd,表明有新连接连入
if ((clientFd = accept(serverFd,
(struct sockaddr *) &clientAddr, &clientLen)) < 0) {
printf("accept fail.\n");
}
printf("Connect from %s:%d\n", inet_ntoa(clientAddr.sin_addr),
htons(clientAddr.sin_port));
setnonblocking(clientFd);
//注册accept()到的连接
ev.data.fd = clientFd;
ev.events = EPOLLIN | EPOLLET;
epoll_ctl(epFd, EPOLL_CTL_ADD, clientFd, &ev);
} else if (evs[i].events & EPOLLIN) {
//如果不是serverFd,则是client的可读
if ((clientFd = evs[i].data.fd) > 0) {
//先进行试探性读取
int len = read(clientFd, buf, BUF_SIZE);
if (len > 0) {
//有数据可以读,Echo写入
do {
if (write(clientFd, buf, len) < 0) {
printf("write() fail.\n");
}
len = read(clientFd, buf, BUF_SIZE);
} while (len > 0);
} else if (len == 0) {
//出发了EPOLLIN事件,却没有可以读取的,表示断线
printf("Client closed at %d\n", clientFd);
epoll_ctl(epFd, EPOLL_CTL_DEL, clientFd, &ev);
close(clientFd);
evs[i].data.fd = -1;
break;
} else if (len == EAGAIN) {
continue;
} else {
//client读取出错
printf("read() fail.");
}
}
} else {
printf("other event.\n");
}
}
} return 0;
}
由于网页问题,粘贴过来的时候带着很多行号
写了个程序解决这个问题
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader; /**
* @author 作者 E-mail:
* @version 创建时间:2015-10-30 下午02:38:17 类说明 处理从网页上粘贴过来的代码
*/
public class Test
{
public static void main(String[] args) throws IOException
{
FileInputStream inputStream = new FileInputStream("source"); FileOutputStream outputStream = new FileOutputStream("res"); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); int linenum = 0;
final StringBuilder builder = new StringBuilder();
String tempstr = null;
String subStr = null;
while ((tempstr = br.readLine()) != null)
{ subStr = tempstr.substring(getSize(++linenum));
builder.append(subStr + '\n');
} outputStream.write(builder.toString().getBytes("gbk"));
} public static int getSize(int lineNum)
{
int res = 1;
while (lineNum / 10 > 0)
{
lineNum = lineNum / 10;
res++;
}
return res;
}
}
Linux下的echo服务器的更多相关文章
- Linux下的SVN服务器搭建
Linux下的SVN服务器搭建 鉴于在搭建时,参考网上很多资料,网上资料在有用的同时,也坑了很多人 本文的目的,也就是想让后继之人在搭建svn服务器时不再犯错,不再被网上漫天的坑爹作品所坑害,故此总 ...
- Linux下安装Tomcat服务器和部署Web应用
一.上传Tomcat服务器
- Linux下部署FTP服务器
Linux下部署FTP服务器 下载安装包 在这里介绍的是离线部署FTP,首先下载对应的rpm包,下载链接为: 下载vsftpd服务 下载FTP客户端 安装ftp服务器 关闭防火墙 service ip ...
- Linux下配置Tomcat服务器
Linux下配置Tomcat服务器和Windows下其实差不多,可以去官网下载安装包释放或者在线下载,只是当时下载的windows.zip文件,现在下载.tar.gz格式的即可,下面使用命令行的方式安 ...
- 转】Linux下安装Tomcat服务器和部署Web应用
原博文出自于: http://www.cnblogs.com/xdp-gacl/p/4097608.html 感谢! 一.上传Tomcat服务器
- Linux下登陆mysql服务器不需要输入账号密码信息
linux下登录mysql服务器一般都是在命令行手动输入链接信息 [root@localhost ~]# mysql -hlocalhost -uroot -p11111 而在mysql 5.6之后版 ...
- Linux下查看Web服务器当前的并发连接数和TCP连接状态
对于web服务器(Nginx.Apache等)来说,并发连接数是一个比较重要的参数,下面就通过netstat命令和awk来查看web服务器的并发连接数以及TCP连接状态. $ netstat -n | ...
- Linux 下 简单客户端服务器通讯模型(TCP)
原文:Linux 下 简单客户端服务器通讯模型(TCP) 服务器端:server.c #include<stdio.h> #include<stdlib.h> #include ...
- Linux下的SVN服务器搭建(转)
Linux下的SVN服务器搭建 鉴于在搭建时,参考网上很多资料,网上资料在有用的同时,也坑了很多人 本文的目的,也就是想让后继之人在搭建svn服务器时不再犯错,不再被网上漫天的坑爹作品所坑害,故此 ...
随机推荐
- PAT---1050. String Subtraction (20)
#include<iostream> #include<string.h> #include<stdio.h> using namespace std; #defi ...
- 在storyboard中的静态UITableView中拖入 UISearchBar and Search Display Controller出现的奇怪问题
近期学习过程中想模拟一下新浪微博"发现"界面. 我在storyboard中拖入一个UITableViewController,设置这个UITableViewCo ...
- GridView控件
GridView是ASP.NET 1.x的DataGrid控件的后继者.它提供了同样的基本功能集,同一时候添加�了大量扩展和改进.如前所述,DataGrid(ASP.NET 2.0仍然全然支持)是一个 ...
- Spring Data Redis简介以及项目Demo,RedisTemplate和 Serializer详解
一.概念简介: Redis: Redis是一款开源的Key-Value数据库,运行在内存中,由ANSI C编写,详细的信息在Redis官网上面有,因为我自己通过google等各种渠道去学习Redis, ...
- GDB的non-stop模式
线程调试必杀技 - GDB的non-stop模式 作者:破砂锅 开源的GDB被广泛使用在Linux.OSX.Unix和各种嵌入式系统(例如手机),这次它又带给我们一个惊喜. 多线程调试之痛 调试器 ...
- Coordinate System
Coordinate System Introduction of Different Coordinate Systems Cartesian Coordinate System UI Coordi ...
- 注释驱动的 Spring cache 缓存介绍--转载
概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...
- 所有Mac用户都需要知道的9个实用终端命令行<转>
转自 http://www.macx.cn/thread-2075903-1-1.html 通常情况下,只有高端用户才会经常用到终端应用.这并不意味着命令行非常难学,有的时候命令行可以轻松.快速的解决 ...
- jstl中添加自定义的函数
由于jstl中提供的函数未必能够满足我们的要求,而我们又希望能够像jstl提供的函数那样能够轻松方便使用,那么可以通过自定义函数补充jsltl函数.给jstl添加自定义函数需要以下步骤: 定义一个st ...
- python 入门1
python的历史 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言. Python由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年. 像Per ...