1.poll函数说明
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
struct pollfd {
int fd;
short events;
short revents;
};
- 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
2.poll服务器端流程
int main()
{
struct pollfd client[1024];
client[0].fd = lfd;
client[0].events = POLLIN;
int maxi = 0;
for(i=1; i<1024; i++)
{
client[i].fd = -1;
}
while(1)
{
nready = poll(client, maxi+1, -1);
if(nready<0)
{
if(errno==EINTR)
{
continue;
}
break;
}
if(client[0].revents==POLLIN)
{
cfd = accept(lfd, NULL, NULL);
for(i=0; i<1024; i++)
{
if(client[i].fd==-1)
{
client[i].fd = cfd;
client[i].events = POLLIN;
break;
}
}
if(i==1024)
{
close(cfd);
continue;
}
if(maxi<i)
{
maxi = i;
}
if(--nready==0)
{
continue;
}
}
for(i=1; i<=maxi; i++)
{
sockfd = client[i].fd;
if(client[i].fd==-1)
{
continue;
}
if(client[i].revents==POLLIN)
{
n = read(sockfd, buf, sizeof(buf));
if(n<=0)
{
close(sockfd);
client[i].fd = -1;
}
else
{
write(sockfd, buf, n);
}
if(--nready==0)
{
break;
}
}
}
}
close(lfd);
}
- 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
3.poll服务器代码实现
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "wrap.h"
int main()
{
int i;
int n;
int lfd;
int cfd;
int ret;
int nready;
int maxfd;
char buf[1024];
socklen_t len;
int sockfd;
fd_set tmpfds, rdfds;
struct sockaddr_in svraddr, cliaddr;
lfd = Socket(AF_INET, SOCK_STREAM, 0);
int opt = 1;
setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
svraddr.sin_family = AF_INET;
svraddr.sin_addr.s_addr = htonl(INADDR_ANY);
svraddr.sin_port = htons(8888);
ret = Bind(lfd, (struct sockaddr *)&svraddr, sizeof(struct sockaddr_in));
ret = Listen(lfd, 128);
struct pollfd client[1024];
for(i=0; i<1024; i++)
{
client[i].fd = -1;
}
client[0].fd = lfd;
client[0].events = POLLIN;
maxfd = 0;
while(1)
{
nready = poll(client, maxfd+1, -1);
if(nready<0)
{
perror("poll error");
exit(1);
}
if(client[0].fd==lfd && (client[0].revents & POLLIN))
{
cfd = Accept(lfd, NULL, NULL);
for(i=1; i<1024; i++)
{
if(client[i].fd==-1)
{
client[i].fd = cfd;
client[i].events = POLLIN;
break;
}
}
if(i==1024)
{
Close(cfd);
continue;
}
if(maxfd<i)
{
maxfd = i;
}
if(--nready==0)
{
continue;
}
}
for(i=1; i<=maxfd; i++)
{
if(client[i].fd==-1)
{
continue;
}
sockfd = client[i].fd;
memset(buf, 0x00, sizeof(buf));
n = Read(sockfd, buf, sizeof(buf));
if(n<=0)
{
printf("read error or client closed,n==[%d]\n", n);
Close(sockfd);
client[i].fd = -1;
}
else
{
printf("read over,n==[%d],buf==[%s]\n", n, buf);
write(sockfd, buf, n);
}
if(--nready==0)
{
break;
}
}
}
Close(lfd);
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
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130