gpt4 book ai didi

c - 带有选择但客户端 block 的linux服务器

转载 作者:太空宇宙 更新时间:2023-11-04 10:12:49 24 4
gpt4 key购买 nike

我写了一个程序服务器,我想同时连接上千个客户端,代码如下:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/select.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<unistd.h>

int fds[sizeof(fd_set)*8];

static usage(const char* proc)
{
printf("usage :%s [local_ip] [local_port]\n",proc);
}

int startup(const char* ip,int port)
{
int sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0)
{
perror("socket");
exit(2);
}
int opt = 1;

setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
//set non-blocking
int flags = fcntl(sock, F_GETFL, 0);
fcntl(sock, F_SETFL, flags|O_NONBLOCK);

struct sockaddr_in local;
local.sin_family = AF_INET;
local.sin_port = htons(port);
local.sin_addr.s_addr = inet_addr(ip);

if(bind(sock,(struct sockaddr*)&local,sizeof(local)) < 0)
{
perror("bind");
exit(3);
}

if(listen(sock,10) < 0)
{
perror("listen");
exit(4);
}

return sock;
}

int main(int argc,char* argv[])
{
if(argc != 3)
{
usage(argv[0]);
return 1;
}
int listen_sock = startup(argv[1],atoi(argv[2]));
printf("fd_set: %d\n",sizeof(fd_set)*8);
int fds[sizeof(fd_set)];
int nums = sizeof(fds)/sizeof(fds[0]);
int i = 0;
for(; i < nums; i++)
{
fds[i] = -1;
}
fds[0] = listen_sock;
int maxfd = -1;
fd_set rfds;//读事件
fd_set wfds;//写事件

while(1)
{
int maxfd = -1;
struct timeval timeout = {2,0};
FD_ZERO(&rfds);
FD_ZERO(&wfds);
i = 0;
for(; i < nums;i++)
{
if(fds[i] == -1)
{
continue;
}
FD_SET(fds[i],&rfds);
if(maxfd < fds[i])
{
maxfd = fds[i];
}
}
switch(select(maxfd+1,&rfds,&wfds,NULL,&timeout))
{
case -1: //select失败
perror("select");
break;
case 0: //超过时间没有任何描述符就绪
printf("time out!\n");
break;
default:
{
//at least one fd ready!
i = 0;
for(; i < nums;i++)
{
if(i == 0 && FD_ISSET(fds[i],&rfds))//listen_sock is ready, get connect
{
struct sockaddr_in client;
socklen_t len = sizeof(client);
int new_sock = accept(listen_sock,(struct sockaddr*)&client,&len);
if(new_sock < 0)
{
perror("accept");
continue;
}

printf("get a new client: [%s:%d]\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port));
int flags = fcntl(new_sock, F_GETFL, 0);
fcntl(new_sock, F_SETFL, flags|O_NONBLOCK);
int j = 1;
for(; j < nums ; j++)
{
if(fds[j] == -1)
{
break;
}
}

if(j == nums)
{
close(new_sock);
}
else
{
fds[j] = new_sock;
}
}


else if(i != 0 && FD_ISSET(fds[i],&rfds))//normal fd is ready
{
char buf[1024];
ssize_t s = read(fds[i],buf,sizeof(buf)-1);
if( s > 0)
{
buf[s] = 0;
printf("client# %s\n",buf);
FD_SET(fds[i],&wfds);

}
else if(s == 0)
{
printf("client is quit!\n");
close(fds[i]);
fds[i] = -1;
}
else
{
perror("read");
close(fds[i]);
fds[i] = -1;
}
}
if(i !=0 && FD_ISSET(fds[i],&wfds))//普通的写操作
{
const char* msg = "hello client!\n";
ssize_t s = write(fds[i],msg,strlen(msg));
if(s < 0)
{
perror("write");
}
else
{
FD_CLR(fds[i],&wfds);
}
}
}
break;
}
}

}
close(listen_sock);
return 0;
}

在代码中,当我接受一个新套接字时,我设置了非阻塞,但是当我用 ab 测试它时,它显示如下结果:

[zoushengfu@test ~]$ ab -n 10 -c 10 http://127.0.0.1:1234/ 
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)...
apr_pollset_poll: The timeout specified has expired (70007)

服务器显示:

enter image description here

结果好像是client block,怎么解决或者给我举个例子?

最佳答案

  1. 您应该将您的服务器绑定(bind)到 INADDR_ANY 而不是特定的 IP 地址,并且您可能还必须注意防火墙规则和端口转发。

  2. 这是错误的:

    maxfd = fds[i];

    select() 的第一个参数不是maxfd 而是nfds,事件>fds,它应该在上面设置为最大的有效 i+1,以便包含 fds[0] 处的条目监听套接字。

关于c - 带有选择但客户端 block 的linux服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47957797/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com