首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >D语言Socket.receive()

D语言Socket.receive()
EN

Stack Overflow用户
提问于 2013-03-30 19:25:13
回答 1查看 596关注 0票数 3

我在Windows上用D写了一个socket服务器,现在我想把它移植到Linux上。以下是代码摘要:

代码语言:javascript
复制
/*
 * this.rawsocks - SocketSet
 * this.server - Socket
 * this.clients - array of custom client worker class
 */

char[1] buff;
string input;
int nbuff = 0;

while (this.isrun) {
    this.rawsocks.add(this.server);

    if (Socket.select(this.rawsocks, null, null)) {
        if (this.rawsocks.isSet(this.server)) {
            // accepting a new connection
        }

        foreach (ref key, item; this.clients) {
            // works for all connections
            writeln("test1");

            // mystically interrupts foreach loop
            nbuff = item.connection.receive(buff);

            // works only for the first connection.
            // when the first connection is closed, it works for the next
            writeln("test2");
        }
    }

    this.rawsocks.reset();

    foreach (key, value; this.clients)
        this.rawsocks.add(value.connection);
}

item.connection.receive(buff)在Windows上运行良好,但会中断Linux上的foreach循环。没有任何异常,当第一个客户端断开连接时,将触发下一个客户端的test2

.receive()方法在Linux中有没有什么特殊的行为,或者在我的实现中有什么问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-01 03:59:25

这个问题的解决方案和问题本身一样奇怪:)

代码语言:javascript
复制
foreach (ref key, item; this.clients) {
     /*
      * The solution
      * Check of client's socket activity in SocketSet queue may not be necessary in Windows, but it is necessary in Linux
      * Thanks to my friend's research of this problem
      */
     if (!this.rawsocks.isSet(item.connection)) continue;

     // works for all connections
     writeln("test1");

     // after modifying not interrupts foreach loop
     nbuff = item.connection.receive(buff);

     // after modifying also works for all connections
     writeln("test2");
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15717989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档