基于poll实现的echo服务器

/*
poll实现的echo服务器.
Author:thinkmay
*/
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<unistd.h>

#include
<poll.h>
#include
<sys/socket.h>
#include
<netinet/in.h>
#include
<arpa/inet.h>
#include
<sys/types.h>
#include
<errno.h>

#define OPEN_MAX 512
#define BUFSIZE 1024

int main(int argc, char **argv)
{
//判断运行格式是否正确
if(argc != 2)
{
printf(
"usage: ./poll <port>");
exit(
-1);
}

int listensock, connsock;
struct sockaddr_in servaddr;
struct pollfd fds[OPEN_MAX];//定义pollfd结构体
int i, count = 0, ready, reval;
char readbuf[BUFSIZE];

//首先对pollfd结构数组的fd都赋值为-1
for(i = 0; i < OPEN_MAX; i++)
{
fds[i].fd
= -1;//循环将fd赋值为-1.
}

//创建TCP套接字
if((listensock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror(
"socket error");
exit(
-1);
}

memset(
&servaddr, 0, sizeof(servaddr));//套接字地址变量清0
servaddr.sin_family = AF_INET;
servaddr.sin_family
= htonl(INADDR_ANY);
servaddr.sin_port
= htons(atoi(argv[1]));

//绑字套接字和套接字地址变量
if( bind(listensock, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)
{
perror(
"bind error");
exit(
-1);
}

//监听套接字
if(listen(listensock, 5) < 0)
{
perror(
"listen error");
exit(
-1);
}

//将0和listensock加入到pollfd结构体中
fds[0].fd = 0; //将标准输入添加到fd中
fds[0].events = POLLIN;//指明触发事件状态
count++;
fds[
1].fd = listensock;
fds[
1].events = POLLIN;
count
++;

for(;;)
{
memset(readbuf,
0, BUFSIZE);
ready
= poll(fds, count, 0);//定义poll准备
for(i = 0; i < count; i++)
{
//revents返回触发状态
if(fds[i].revents & POLLIN)//判断POLLIN是否触发
{
if(fds[i].fd == 0)
{
fgets(readbuf, BUFSIZE, stdin);
printf(
"read buf:%s\n", readbuf);
}
else if(fds[i].fd == listensock)
{
connsock
= accept(listensock, NULL, NULL);
printf(
"client connect\n");
fds[count].fd
= connsock;
fds[count].events
= POLLIN;
count
++;
}
else
{
reval
= recv(fds[i].fd, readbuf, BUFSIZE, 0);
printf(
"recv buf:%s\n", readbuf);
send(fds[i].fd, readbuf, reval,
0);
}
}
}

}
return 0;
}

  

posted @ 2011-08-31 10:57  hnrainll  阅读(413)  评论(0编辑  收藏  举报