Avoid socket leaks in examples.
This commit is contained in:
parent
958856f6f1
commit
a44d3f2655
|
@ -19,7 +19,7 @@ void main()
|
||||||
conn.readLine(r);
|
conn.readLine(r);
|
||||||
if (!r.count) break;
|
if (!r.count) break;
|
||||||
}
|
}
|
||||||
conn.write(cast(const(ubyte)[])"HTTP/1.1 200 OK\r\nContent-Length: 13\r\nContent-Type: text/plain\r\nConnection: keep-alive\r\n\r\nHello, World!");
|
conn.write(cast(const(ubyte)[])"HTTP/1.1 200 OK\r\nContent-Length: 13\r\nContent-Type: text/plain\r\nConnection: keep-alive\r\nKeep-Alive: timeout=10\r\n\r\nHello, World!");
|
||||||
conn.flush();
|
conn.flush();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -30,6 +30,7 @@ void main()
|
||||||
|
|
||||||
auto listener = listenTCP(8080, &staticAnswer, "127.0.0.1");
|
auto listener = listenTCP(8080, &staticAnswer, "127.0.0.1");
|
||||||
logInfo("Listening to HTTP requests on http://127.0.0.1:8080/");
|
logInfo("Listening to HTTP requests on http://127.0.0.1:8080/");
|
||||||
|
scope (exit) listener.stopListening();
|
||||||
|
|
||||||
runApplication();
|
runApplication();
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,10 +5,16 @@ import vibe.core.stream : pipe;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
listenTCP(7000, (conn) @safe nothrow {
|
auto listeners = listenTCP(7000, (conn) @safe nothrow {
|
||||||
try pipe(conn, conn);
|
try pipe(conn, conn);
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
logError("Error: %s", e.msg);
|
logError("Error: %s", e.msg);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// closes the listening sockets
|
||||||
|
scope (exit)
|
||||||
|
foreach (l; listeners)
|
||||||
|
l.stopListening();
|
||||||
|
|
||||||
runApplication();
|
runApplication();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue