alex025
alex025

Reputation: 173

D language Socket.receive()

I wrote a socket server using D on Windows and now I want to port it on Linux. Here is code summary:

/*
 * 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) works fine on Windows, but interrupts foreach loop on Linux. There are no any exceptions, and test2 for next client is firing when first client is disconnected.

Is there some specially behaviour of .receive() method in Linux, or there are some problems in my implementation?

Upvotes: 3

Views: 630

Answers (1)

alex025
alex025

Reputation: 173

The solution of this problem is as strange as the problem itself :)

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

Upvotes: 1

Related Questions