This one works ok for me, but I am on linux:On Wed, Nov 15, 2017 at 12:46 PM, Daniel Kozak <kozzi11@gmail.com> wrote:Do not use your own taskPool, just use global taskPool proerty (import std.parallelism: taskPool).You should not set blocking to false. And dont use Thread here. There is no reason to do that. Just move that code into the mainDne 15. 11. 2017 12:15 odp. napsal uživatel "ade90036 via Digitalmars-d-learn" <digitalmars-d-learn@puremagic.com >:So thanks for the suggestions, i have fixed HTTP response not postman cal also parse the headers correctly!! happy days.
I have removed the duration from the Socket.select but the application seems to process a bunch or requests and then it stalls for several seconds (3/5) and then it resumes.
The httpclinet which i'm using to test the application is reporting: "connection timeout".
Could this be caused by the GC?
```updated code
import std.algorithm : remove;
import std.conv : to;
import core.thread: Thread;
import std.socket : InternetAddress, Socket, SocketException, SocketSet, TcpSocket, SocketShutdown;
import core.time : Duration, dur;
import std.stdio : writeln, writefln;
import std.parallelism : task, TaskPool;
string to_retlf (string s)
{
import std.algorithm;
import std.string;
return s
.lineSplitter
.map!(a => chomp (a))
.join ("\r\n");
}
void main(string[] args)port = 4444;
{
ushort port;
if (args.length >= 2)
port = to!ushort(args[1]);
elsestring response = "HTTP/1.1 200 OK
auto listener = new TcpSocket();
assert(listener.isAlive);
listener.blocking = false;
listener.bind(new InternetAddress(port));
listener.listen(100);
writefln("Listening on port %d.", port);
auto taskPool = new TaskPool(8);
Content-Type: text/html; charset=UTF-8
Server: dland:v2.076.1
Date: Tue, 11 Nov 2017 15:56:02 GMT
Content-Length: 51
<html><head></head><body>Hello World!</body></html>".to_retlf; while(true) {
new Thread({
auto listeningSet = new SocketSet();
listeningSet.add(listener);
if (Socket.select(listeningSet, null, null)) {Socket socket = listener.accept();
if (listeningSet.isSet(listener))// connection request
{
assert(socket.isAlive);auto task = task!handle_socket(socket, response);
//writefln("Connection from %s established.", socket.remoteAddress().toString());
taskPool.put(task);
}
}
listeningSet.reset();
}
}).start();
}
void handle_socket(Socket socket, string response) {if (Socket.select(socketSet, null, null)) {
auto socketSet = new SocketSet();
while(true) {
socketSet.add(socket);socket.send(response);
char[1024] buf;
auto datLength = socket.receive(buf[]);
if (datLength == Socket.ERROR)
writeln("Connection error.");
else if (datLength != 0)
{
//writefln("Received %d bytes from %s: \"%s\"", datLength, socket.remoteAddress().toString(), buf[0..datLength]);
//writefln("Writing response");socket.close();
}
// release socket resources now
break;
}
socketSet.reset();
}
```
Regards