转载:http://blog.csdn.net/y396397735/article/details/50680359
本例实现如下功能:
支持多客户端与一个服务端进行通信,客户端给服务端发送字符串数据,服务端将字符串中小写转为大写后发送回客户端,客户端打印输出经转换后的字符串。
例如:发送abcde
,打印输出ABCDE
服务端源码如下:
#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 = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
listen(listenfd, 20);
for(i = 0; i < OPEN_MAX; i++){
client[i] = -1;
}
maxi = -1;
efd = epoll_create(OPEN_MAX);
if(efd == -1){
perror("epoll_create");
}
tep.events = EPOLLIN;
tep.data.fd = listenfd;
res = epoll_ctl(efd, EPOLL_CTL_ADD, 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))
continue;
if(ep[i].data.fd == listenfd){
clilen = sizeof(cliaddr);
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;
tep.events = EPOLLIN;
tep.data.fd = connfd;
res = epoll_ctl(efd, EPOLL_CTL_ADD, connfd, &tep);
if(res == -1)
perror("epoll_ctl");
}else{
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;
}
}
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]);
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
- 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
客户端源码如下:
#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){
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
- 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
另开一终端2:
yu@ubuntu:~/Linux/217/epoll$ ./client
hey how are you
HEY HOW ARE YOU
bye
BYE
再另开一终端3:
yu@ubuntu:~/Linux/217/epoll$ ./client
i am here 007
I AM HERE 007
hehe
HEHE
最后开一终端4:
yu@ubuntu:~/Linux/217/epoll$ ./client
i am 008
I AM 008
zai jian
ZAI JIAN
关闭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