http://blog.csdn.net/y396397735/article/details/50680359
本例实现如下功能:
支持多客户端与一个服务端进行通信,客户端给服务端发送字符串数据,服务端将字符串中小写转为大写后发送回客户端,客户端打印输出经转换后的字符串。
例如:发送abcde
,打印输出ABCDE
服务端源码如下:
/*server.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <errno.h>
#define BUFSIZE 666
#define SERV_PORT 8000
#define OPEN_MAX 1024
int main()
{
int i, j, maxi, listenfd, connfd, sockfd;
int nready, efd, res;
ssize_t n;
char buf[BUFSIZE], str[INET_ADDRSTRLEN];
socklen_t clilen;
int client[OPEN_MAX];
struct sockaddr_in cliaddr, servaddr;
struct epoll_event tep, ep[OPEN_MAX];//监听事件
/*分配一个网络通信套接字,监听文件描述符listenfd*/
listenfd = socket(AF_INET, SOCK_STREAM, 0);
/*初始化 IP类型 端口*/
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
/*将listenfd绑定服务端地址*/
bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
/*监听请求*/
listen(listenfd, 20);
/*将客户端标识初始化为-1*/
for(i = 0; i < OPEN_MAX; i++){
client[i] = -1;
}
maxi = -1;
/*告诉内核要监听的文件描述符个数 OPEN_MAX = 1024*/
efd = epoll_create(OPEN_MAX);
if(efd == -1){
perror("epoll_create");
}
tep.events = EPOLLIN;/*监听文件描述符的可读事件*/
tep.data.fd = listenfd;/*设置为监听的文件描述符*/
/*控制epoll监控的文件描述符上的事件*/
res = epoll_ctl(efd, EPOLL_CTL_ADD/*注册新的fd到efd*/, listenfd, &tep);
if(res == -1)
perror("epoll_ctl");
for(;;){
/*等待所监控文件描述符上有事件的产生,阻塞监听*/
nready = epoll_wait(efd, ep, OPEN_MAX, -1);
if(nready == -1)
perror("epoll_wait");
for(i = 0; i < nready; i++){
if(!(ep[i].events & EPOLLIN))/*若不是EPOLLIN事件,不做往下的处理*/
continue;
if(ep[i].data.fd == listenfd){ /*若是EPOLLIN,执行连接,接受请求*/
clilen = sizeof(cliaddr);
/*接受请求,分配新文件描述符connfd进行通信*/
connfd = accept(listenfd, (struct sockaddr*)&cliaddr, &clilen);
printf("received from %s at PORT %d\n", (char*)inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)), ntohs(cliaddr.sin_port));
/*若将此新客户端添加至客户端集中*/
for(j = 0; j < OPEN_MAX; j++)
if(client[j] < 0){
client[j] = connfd;
break;
}
if(j == OPEN_MAX)
perror("客户端超过限制");
if(j > maxi)
maxi = j;//保证maxi为最大文件描述符
tep.events = EPOLLIN;
tep.data.fd = connfd;
res = epoll_ctl(efd, EPOLL_CTL_ADD/*注册新的connfd到efd*/, connfd, &tep);
if(res == -1)
perror("epoll_ctl");
}else{/*处理efd中监听的客户端请求*/
sockfd = ep[i].data.fd;
n = read(sockfd, buf, BUFSIZE);
if(n == 0){ /*读取若为空*/
for(j = 0; j <= maxi; j++){
if(client[j] == sockfd){
client[j] = -1;
break;
}
}
/*清除对sockfd文件描述符事件的监听*/
res = epoll_ctl(efd, EPOLL_CTL_DEL, sockfd, NULL);
if(res == -1)
perror("epoll_ctl");
close(sockfd);
printf("client[%d] closed connection\n", j);
}else{/*非空则处理客户端信息*/
for(j = 0; j<n; j++)
buf[j] = toupper(buf[j]);
/*写入与客户端通信的文件描述符sockfd*/
write(sockfd, buf, n);
}
}
}
}
/*关闭监听*/
close(listenfd);
close(efd);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
客户端源码如下:
/*client.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <errno.h>
#define BUFSIZE 666
#define SERV_PORT 8000
#define OPEN_MAX 1024
int main(int argc, char *argv[])
{
struct sockaddr_in servaddr;
char buf[BUFSIZE];
int sockfd, n;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
servaddr.sin_port = htons(SERV_PORT);
/*连接服务端*/
connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
while(fgets(buf, BUFSIZE, stdin) != NULL){
/*通过sockfd给服务端发送数据*/
write(sockfd, buf, strlen(buf));
n = read(sockfd, buf, BUFSIZE);
if(n == 0)
printf("the other side has been closed.\n");
else/*打印输出服务端传过来的数据*/
write(STDOUT_FILENO, buf, n);
}
close(sockfd);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
编译及执行
在终端1先执行:
yu@ubuntu:~/Linux/217/epoll$ ls
client.c server.c
yu@ubuntu:~/Linux/217/epoll$ gcc -o client client.c
yu@ubuntu:~/Linux/217/epoll$ gcc -o server server.c
yu@ubuntu:~/Linux/217/epoll$ ./server
- 1
- 2
- 3
- 4
- 5
另开一终端2:
yu@ubuntu:~/Linux/217/epoll$ ./client
hey how are you
HEY HOW ARE YOU
bye
BYE
- 1
- 2
- 3
- 4
- 5
- 6
再另开一终端3:
yu@ubuntu:~/Linux/217/epoll$ ./client
i am here 007
I AM HERE 007
hehe
HEHE
- 1
- 2
- 3
- 4
- 5
- 6
最后开一终端4:
yu@ubuntu:~/Linux/217/epoll$ ./client
i am 008
I AM 008
zai jian
ZAI JIAN
- 1
- 2
- 3
- 4
- 5
- 6
关闭3个客户端(CTR+C
)后服务端窗口:
yu@ubuntu:~/Linux/217/epoll$ ./server
received from 127.0.0.1 at PORT 58385
received from 127.0.0.1 at PORT 58386
received from 127.0.0.1 at PORT 58387
client[2] closed connection
client[1] closed connection
client[0] closed connection
- 1
- 2
- 3
- 4
- 5
- 6
- 7