On Thursday, 4 October 2018 at 13:07:30 UTC, bauss wrote:
Not exactly a tutorial or a walkthrough, but I'll try to
explain basic usage of std.socket using an asynchronous tcp
server as example.
So I tried to code the example as described. After the program is
at
```d
auto clientResult = Socket.select(clientSet, null, null);
```
The program gets stuck and doesn't do anything. I use `telnet` in
Bash as client to connect to the server. What am I doing wrong?
For completeness, this is the full code made from the example:
```d
import std.stdio, std.socket;
void startServer() {
auto server = new TcpSocket;
server.blocking = false;
server.bind(new InternetAddress("localhost", 1234));
server.listen(100);
Socket[] clients;
auto serverSet = new SocketSet;
auto clientSet = new SocketSet;
while (true) {
serverSet.reset();
clientSet.reset();
serverSet.add(server);
if (clients) {
foreach (client; clients) {
clientSet.add(client);
}
}
auto serverResult = Socket.select(serverSet, null,
null);
if (serverResult > 0) {
auto client = server.accept();
if (client) {
clients ~= client;
}
writeln("Gets stuck");
auto clientResult = Socket.select(clientSet,
null, null);
writeln("Nevermind...");
// Keep it commented out as it gives compilation errors
/*if (clientSet < 1)
{
continue;
}*/
foreach (_client; clients) {
if (!clientSet.isSet(_client)) {
continue;
}
auto buffer = new ubyte[1024];
auto received = client.receive(buffer);
if (received == 0 || received ==
Socket.ERROR) {
continue; // Most likely disconnected ...
}
buffer = buffer[0 .. received]; // Slice the
buffer to the actual size of the received data.
}
}
}
}
```