D语言Socket.receive()

时间:2013-03-30 11:25:13

标签: sockets d

我在Windows上使用D编写了一个套接字服务器,现在我想在Linux上移植它。这是代码摘要:

/*
 * 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上中断循环。没有任何例外,当第一个客户端断开连接时,下一个客户端的test2将被触发。

Linux中是否存在.receive()方法的特殊行为,或者我的实现中存在一些问题?

1 个答案:

答案 0 :(得分:1)

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

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");
}