gpt4 book ai didi

c - 使用select()的套接字多路复用无法按预期方式工作

转载 作者:行者123 更新时间:2023-12-03 11:56:44 25 4
gpt4 key购买 nike

我正在尝试创建一个服务器,该服务器监听两个不同的端口,从相应的套接字读取一条消息,然后将其打印到stdout。当我第一次连接到任何可用端口时,该程序将按预期工作。然后,当我尝试连接到第二个端口时,connect()将不会完全解除阻塞。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main(int argc, char** argv)
{
struct sockaddr_in addr1;
struct sockaddr_in addr2;
int srv_fd1;
int srv_fd2;
int srv_fd;
int accept_fd;
fd_set srv_fds;
char mem[1024];

if ((srv_fd1 = socket(AF_INET, SOCK_STREAM, 0)) == -1 ||
(srv_fd2 = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
fprintf(stderr, "socket(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}

addr1.sin_family = AF_INET;
addr1.sin_port = htons(16000);
addr1.sin_addr.s_addr = htonl(INADDR_ANY);

addr2.sin_family = AF_INET;
addr2.sin_port = htons(16001);
addr2.sin_addr.s_addr = htonl(INADDR_ANY);

if (bind(srv_fd1, (struct sockaddr *)&addr1, (socklen_t)sizeof(addr1)) == -1 ||
bind(srv_fd2, (struct sockaddr *)&addr2, (socklen_t)sizeof(addr2)) == -1)
{
fprintf(stderr, "bind(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}

if (listen(srv_fd1, 128) == -1 || listen(srv_fd2, 128) == -1) {
fprintf(stderr, "listen(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}

FD_ZERO(&srv_fds);
FD_SET(srv_fd1, &srv_fds);
FD_SET(srv_fd2, &srv_fds);

while (select(srv_fd2 +1, &srv_fds, NULL, NULL, NULL) != -1) {

printf("Inside loop\n");
fflush(stdout);
memset(mem, 0, 1024);

if (FD_ISSET(srv_fd1, &srv_fds))
srv_fd = srv_fd1;
else if (FD_ISSET(srv_fd2, &srv_fds))
srv_fd = srv_fd2;

accept_fd = accept(srv_fd, NULL, NULL);
if (accept_fd == -1) {
fprintf(stderr, "accept(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}

read(accept_fd, mem, 1023);
printf("%s", mem);

close(accept_fd);
}

close(srv_fd1);
close(srv_fd2);
return 0;
}

telnet问题的演示:
$ telnet 127.0.0.1 16000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
FIRST ATTEMPT
Connection closed by foreign host.

$ telnet 127.0.0.1 16001
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
SECOND ATTEMPT
^]

telnet> quit
Connection closed.

服务器响应:
$ ./test
Inside loop
FIRST ATTEMPT
^C

最佳答案

你需要做

FD_ZERO(&srv_fds);
FD_SET(srv_fd1, &srv_fds);
FD_SET(srv_fd2, &srv_fds);

每次调用select之前,因为那会重置 srv_fds来指示哪个fd有东西。在您的情况下,第一次调用 select会取消 FD_SET(srv_fd2, &srv_fds);,因此第二次调用 select只会管理 srv_fd1
请注意,在执行 srv_fd2 +1时,您还假设 srv_fd2 > srv_fd1可以为false。

您还假设您只有一个准备工作
   if (FD_ISSET(srv_fd1, &srv_fds))
srv_fd = srv_fd1;
else if (FD_ISSET(srv_fd2, &srv_fds))
srv_fd = srv_fd2;
...

但您可以将它们都准备好。

一种方法可以是:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define NSOCK 2
#define FIRST_PORT 16000

/* returns the max descriptor */
int do_fd_set(int * srv_fd, fd_set * srv_fds)
{
int r = 0;
int i;

FD_ZERO(srv_fds);

for (i = 0; i != NSOCK; ++i) {
FD_SET(srv_fd[i], srv_fds);
if (srv_fd[i] > r)
r = srv_fd[i];
}

return r;
}

int main(int argc, char** argv)
{
int srv_fd[NSOCK];
fd_set srv_fds;
int i;

for (i = 0; i != NSOCK; ++i) {
struct sockaddr_in addr;

if ((srv_fd[i] = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
fprintf(stderr, "socket(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}

addr.sin_family = AF_INET;
addr.sin_port = htons(FIRST_PORT + i);
addr.sin_addr.s_addr = htonl(INADDR_ANY);

if (bind(srv_fd[i], (struct sockaddr *)&addr, (socklen_t)sizeof(addr)) == -1)
{
fprintf(stderr, "bind(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}

if (listen(srv_fd[i], 128) == -1) {
fprintf(stderr, "listen(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}

while (select(do_fd_set(srv_fd, &srv_fds) + 1, &srv_fds, NULL, NULL, NULL) != -1) {
printf("Inside loop\n");

for (int i = 0; i != NSOCK; ++i) {
if (FD_ISSET(srv_fd[i], &srv_fds)) {
char mem[1024];
int accept_fd;

memset(mem, 0, 1024);
accept_fd = accept(srv_fd[i], NULL, NULL);
if (accept_fd == -1) {
fprintf(stderr, "accept(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
read(accept_fd, mem, sizeof(mem) - 1);
printf("from port %d : %s\n", FIRST_PORT + i, mem);
close(accept_fd);
}
}
}

/* probably never reach that code */
for (i = 0; i != NSOCK; ++i)
close(srv_fd[i]);

return 0;
}

编译与执行:
pi@raspberrypi:/tmp $ gcc -Wall so.c
pi@raspberrypi:/tmp $ ./a.out
Inside loop
from port 16000 : aze

Inside loop
from port 16000 : qsd

Inside loop
from port 16001 : wxc

^C
pi@raspberrypi:/tmp $

在另一个终端上做:
pi@raspberrypi:/tmp $ telnet localhost 16000
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
aze
Connection closed by foreign host.
pi@raspberrypi:/tmp $ telnet localhost 16000
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
qsd
Connection closed by foreign host.
pi@raspberrypi:/tmp $ telnet localhost 16001
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
wxc
Connection closed by foreign host.

关于c - 使用select()的套接字多路复用无法按预期方式工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62372844/

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