草庐IT

关于 c:Server 无法接受来自多个客户端的消息?

codeneng 2023-03-28 原文

Server is not able to accept() messages from multiple clients?

两个客户端能够连接到服务器,但它只接受和显示第一个客户端的输入流消息,而不是第二个客户端,尽管另一个客户端也已连接。

以下是我接受流的代码,我尝试关闭每个领带的套接字,但没有成功。

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
int main(int argc,char *argv[])
{
fd_set ready;
struct sockaddr_in msgfrom;
int msgsize;
union {
    uint32_t addr;
    char bytes[4];
} fromaddr;

if ((progname = rindex(argv[0], '/')) == NULL)
    progname = argv[0];
else
    progname++;
while ((ch = getopt(argc, argv,"adsp:h:")) != -1)
    switch(ch) {
        case 'a':
            aflg++;     /* print address in output */
            break;
        case 'd':
            soctype = SOCK_DGRAM;
            break;
        case 's':
            server = 1;
            break;
        case 'p':
            port = optarg;
            break;
        case 'h':
            host = optarg;
            break;
        case '?':
        default:
            usage();
    }
argc -= optind;
if (argc != 0)
    usage();
if (!server && (host == NULL || port == NULL))
    usage();
if (server && host != NULL)
    usage();
/*
 * Create socket on local host.
 */

if ((s = socket(AF_INET, soctype, 0)) < 0) {
    perror("socket");
    exit(1);
}
sock = setup_server();  
while (!done) {
    FD_ZERO(&ready);
    FD_SET(sock, &ready);
    FD_SET(fileno(stdin), &ready);
    if (select((sock + 1), &ready, 0, 0, 0) < 0) {
        perror("select");
        exit(1);
    }
    if (FD_ISSET(fileno(stdin), &ready)) {
        if ((bytes = read(fileno(stdin), buf, BUF_LEN)) <= 0)
            done++;
        send(sock, buf, bytes, 0);
    }
    msgsize = sizeof(msgfrom);
    if (FD_ISSET(sock, &ready)) {
        if ((bytes = recvfrom(sock, buf, BUF_LEN, 0, (struct sockaddr *)&msgfrom, &msgsize)) <= 0) {
            done++;
        } else if (aflg) {
            fromaddr.addr = ntohl(msgfrom.sin_addr.s_addr);
            fprintf(stderr,"%d.%d.%d.%d:", 0xff & (unsigned int)fromaddr.bytes[0],
                0xff & (unsigned int)fromaddr.bytes[1],
                0xff & (unsigned int)fromaddr.bytes[2],
                0xff & (unsigned int)fromaddr.bytes[3]);
        }
        write(fileno(stdout), buf, bytes);
    }
}

这是我设置服务器的代码,供参考:

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
int setup_server() {
struct sockaddr_in serv, remote;
struct servent *se;
int newsock, len;

len = sizeof(remote);
memset((void *)&serv, 0, sizeof(serv));
serv.sin_family = AF_INET;
if (port == NULL)
    serv.sin_port = htons(9990);
else if (isdigit(*port))
    serv.sin_port = htons(atoi(port));
if (bind(s, (struct sockaddr *)&serv, sizeof(serv)) < 0) {
    perror("bind");
    exit(1);
}
if (getsockname(s, (struct sockaddr *) &remote, &len) < 0) {
    perror("getsockname");
    exit(1);
}
fprintf(stderr,"Port number is %d\
"
, ntohs(remote.sin_port));
listen(s, 1);
newsock = s;
if (soctype == SOCK_STREAM) {
    fprintf(stderr,"Entering accept() waiting for connection.\
"
);
    newsock = accept(s, (struct sockaddr *) &remote, &len);
}
return(newsock);
}

客户端代码:

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
sock = setup_client();
/*
 * Set up select(2) on both socket and terminal, anything that comes
 * in on socket goes to terminal, anything that gets typed on terminal
 * goes out socket...
 */

while (!done) {
    FD_ZERO(&ready);
    FD_SET(sock, &ready);
    FD_SET(fileno(stdin), &ready);
    if (select((sock + 1), &ready, 0, 0, 0) < 0) {
        perror("select");
        exit(1);
    }
    if (FD_ISSET(fileno(stdin), &ready)) {
        if ((bytes = read(fileno(stdin), buf, BUF_LEN)) <= 0)
            done++;
        send(sock, buf, bytes, 0);
    }
    msgsize = sizeof(msgfrom);
    if (FD_ISSET(sock, &ready)) {
        if ((bytes = recvfrom(sock, buf, BUF_LEN, 0, (struct sockaddr *)&msgfrom, &msgsize)) <= 0) {
            done++;
        } else if (aflg) {
            fromaddr.addr = ntohl(msgfrom.sin_addr.s_addr);
            fprintf(stderr,"%d.%d.%d.%d:", 0xff & (unsigned int)fromaddr.bytes[0],    0xff & (unsigned int)fromaddr.bytes[1],
                0xff & (unsigned int)fromaddr.bytes[2],
                0xff & (unsigned int)fromaddr.bytes[3]);
        }
        write(fileno(stdout), buf, bytes);
    }
    //close(sock);
}
return(0);
}

/*
 * setup_client() - set up socket for the mode of soc running as a
 *      client connecting to a port on a remote machine.
 */


int setup_client() {

struct hostent *hp, *gethostbyname();
struct sockaddr_in serv;
struct servent *se;

/*
 * Look up name of remote machine, getting its address.
 */

if ((hp = gethostbyname(host)) == NULL) {
    fprintf(stderr,"%s: %s unknown host\
"
, progname, host);
    exit(1);
}
/*
 * Set up the information needed for the socket to be bound to a socket on
 * a remote host.  Needs address family to use, the address of the remote
 * host (obtained above), and the port on the remote host to connect to.
 */

serv.sin_family = AF_INET;
memcpy(&serv.sin_addr, hp->h_addr, hp->h_length);
if (isdigit(*port))
    serv.sin_port = htons(atoi(port));
else {
    if ((se = getservbyname(port, (char *)NULL)) < (struct servent *) 0) {
        perror(port);
        exit(1);
    }
    serv.sin_port = se->s_port;
}
/*
 * Try to connect the sockets...
 */

if (connect(s, (struct sockaddr *) &serv, sizeof(serv)) < 0) {
    perror("connect");
    exit(1);
} else
    fprintf(stderr,"Connected...\
"
);
return(s);
}

  • 从您发布的代码中,您的 accept 调用在您的服务器设置中,所以虽然您没有显示 setup_server 被调用的位置,但我只能假设您调用 accept 一次?如果是这样,那是你的问题。 accept() 是获取客户端连接以进行处理的内容。如果您只调用一次,您将只处理一个客户端连接。
  • 你的 accept() 电话在哪里?
  • @spdaley 我现在在代码段中包含了 main 方法。


UDP 没问题,你可以像你已经在做的那样 recvfrom()。

TCP 是不同的,但你快到了:你需要为你想要处理的每个连接调用 accept(),即你在循环中持有 select() 服务器套接字,调用 accept() 为有必要获得一个新的套接字,处理它并在最后关闭它。

在客户端,您似乎已连接,因为您在服务器的待处理连接队列中 - 请参阅 listen(2) 手册页。

  • 我已经尝试过,但这次即使在我关闭它之后它也无法绑定套接字。供您参考,我还包括了客户端代码,请告知我到底哪里出错了。
  • 您不应该关闭服务器套接字或重新绑定它;只需在同一个服务器套接字上再次调用 accept() 即可。
  • 您应该关闭 accept() 返回的套接字句柄。在您准备好停止接受客户端连接之前,不要关闭服务器套接字句柄。

有关关于 c:Server 无法接受来自多个客户端的消息?的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  3. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  4. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  5. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  6. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  7. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  8. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  9. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  10. ruby-on-rails - 在 ruby​​ .gemspec 文件中,如何指定依赖项的多个版本? - 2

    我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这

随机推荐