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 138 139 140 141 142 143 144 145 146 147 148
| #include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERV_PORT 8031
#define MAXDATASIZE 1024
#define FD_SET_SIZE 128
int main(void)
{
int fd_ready_num; // select返回的准备好的描述符个数
int listenfd, connectfd, maxfd, scokfd;
struct sockaddr_in serv_addr;
fd_set read_set, allset;
int client[FD_SETSIZE];
int i;
int maxi = -1;
int sin_size; //地址信息结构体大小
char recvbuf[MAXDATASIZE];
int len;
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("套接字描述符创建失败");
exit(1);
}
printf("listenfd :%d\n", listenfd);
int opt = 1;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(SERV_PORT);
if(bind(listenfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1)
{
perror("绑定失败");
exit(1);
}
if(listen(listenfd, FD_SET_SIZE) == -1)
{
perror("监听失败");
exit(1);
}
maxfd = listenfd + 1;
for (i = 0; i {
client[i] = -1;
}
FD_ZERO(&allset);
FD_SET(listenfd,&allset);
while(1){
struct sockaddr_in addr;
read_set = allset;
fd_ready_num = select(maxfd, &read_set, NULL, NULL, NULL);
// printf("有 %d 个文件描述符准备好了\n", fd_ready_num);
if (FD_ISSET(listenfd,&read_set))
{
sin_size = sizeof(addr);
if ((cOnnectfd= accept(listenfd, (struct sockaddr *)&addr, &sin_size)) == -1)
{
perror("接收错误\n");
continue;
}
for (i = 0; i {
if (client[i] <0)
{
client[i] = connectfd;
printf("接收client[%d]一个请求来自于: %s:%d\n", i, inet_ntoa(addr.sin_addr),ntohs(addr.sin_port));
break;
}
}
if (i == FD_SETSIZE)
{
printf("连接数过多\n");
}
FD_SET(connectfd,&allset);
maxfd = (connectfd > maxfd) ? (connectfd + 1) : maxfd;
maxi = (i > maxi) ? i : maxi;
if (--fd_ready_num <= 0)
{
continue;
}
}
for (i = 0; i {
if ((scokfd = client[i]) <0)
{
continue;
}
if (FD_ISSET(scokfd,&read_set))
{
if((len = recv(scokfd,recvbuf,MAXDATASIZE,0)) == 0)
{
close(scokfd);
printf("clinet[%d] 连接关闭\n", i);
FD_CLR(scokfd, &read_set);
client[i] = -1;
}
else
{
char web_result[] = "HTTP/1.1 200 OK\r\nContent-Type:text/html\r\nContent-Length: 11\r\nServer: mengkang\r\n\r\nhello world";
write(scokfd,web_result,sizeof(web_result));
}
if (--fd_ready_num <= 0)
{
break;
}
}
}
}
close(listenfd);
return 0;
} |