Repository navigation
Expand file tree
/
Copy pathserver.c
More file actions
137 lines (111 loc) · 2.59 KB
/
Copy pathserver.c
File metadata and controls
137 lines (111 loc) · 2.59 KB
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
131
132
133
134
135
136
137
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#define PORT 8888
#define MAX_LINE 2048
#define LISTENQ 20
int main(int argc , char **argv)
{
int i, maxi, maxfd, listenfd, connfd, sockfd;
int nready , client[FD_SETSIZE];
ssize_t n, ret;
fd_set rset , allset;
char buf[MAX_LINE];
socklen_t clilen;
struct sockaddr_in servaddr , cliaddr;
/*(1) 得到监听描述符*/
listenfd = socket(AF_INET , SOCK_STREAM , 0);
/*(2) 绑定套接字*/
bzero(&servaddr , sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
bind(listenfd , (struct sockaddr *)&servaddr , sizeof(servaddr));
/*(3) 监听*/
listen(listenfd , LISTENQ);
/*(4) 设置select*/
maxfd = listenfd;
maxi = -1;
for(i=0 ; i<FD_SETSIZE ; ++i)
{
client[i] = -1;
}//for
FD_ZERO(&allset);
FD_SET(listenfd , &allset);
/*(5) 进入服务器接收请求死循环*/
while(1)
{
rset = allset;
nready = select(maxfd+1 , &rset , NULL , NULL , NULL);
if(FD_ISSET(listenfd , &rset))
{
/*接收客户端的请求*/
clilen = sizeof(cliaddr);
printf("\naccpet connection~\n");
if((connfd = accept(listenfd , (struct sockaddr *)&cliaddr , &clilen)) < 0)
{
perror("accept error.\n");
exit(1);
}//if
printf("accpet a new client: %s:%d\n", inet_ntoa(cliaddr.sin_addr) , cliaddr.sin_port);
/*将客户链接套接字描述符添加到数组*/
for(i=0 ; i<FD_SETSIZE ; ++i)
{
if(client[i] < 0)
{
client[i] = connfd;
break;
}//if
}//for
if(FD_SETSIZE == i)
{
perror("too many connection.\n");
exit(1);
}//if
FD_SET(connfd , &allset);
if(connfd > maxfd)
maxfd = connfd;
if(i > maxi)
maxi = i;
if(--nready < 0)
continue;
}//if
for(i=0; i<=maxi ; ++i)
{
if((sockfd = client[i]) < 0)
continue;
if(FD_ISSET(sockfd , &rset))
{
/*处理客户请求*/
printf("\nreading the socket~~~ \n");
bzero(buf , MAX_LINE);
if((n = read(sockfd , buf , MAX_LINE)) <= 0)
{
close(sockfd);
FD_CLR(sockfd , &allset);
client[i] = -1;
}//if
else{
printf("clint[%d] send message: %s\n", i , buf);
if((ret = write(sockfd , buf , n)) != n)
{
printf("error writing to the sockfd!\n");
break;
}//if
}//else
if(--nready <= 0)
break;
}//if
}//for
}//while
}