Socket + Thread - any working example??

Vincent thornik at gmail.com
Tue Jun 1 13:38:56 PDT 2010


Hello, guys!
Still fighting with my server example and cannot catch what's wrong. Just simple example:
==========================================
import std.stdio;
import std.socket;
import core.thread;

int main(){
    auto srv = new TcpSocket;
    srv.bind(new InternetAddress(1000));
    srv.listen(10);
    writefln("Listen on port 1000");

    for(Socket cli = srv.accept(); srv.isAlive(); cli = srv.accept()){
        auto t = new ServiceClientThread(cli);
        t.start();
    }

    return 0;
}

class ServiceClientThread : Thread
{
    this(Socket remote){
        cli = remote;
        super(&run);
    }

private:
    Socket cli;

    void run()
    {
        cli.send("hello!\n");
        cli.close();
    }
}
==========================================
After first client connects to the server, it receives (as expected) 'hello!' line, but after this server dies, his output:

Listen on port 1000
std.socket.SocketAcceptException: Unable to accept socket connection

I.e. second call to 'accept' kills server. WHY? Can somebody help me, please? Any working example (in my configuration)?
(dmd 2.046, Phobos)


More information about the Digitalmars-d mailing list