digitalmars.D.learn - Tango Server Example
reference: Tango Server: http://www.dsource.org/projects/tango/wiki/SocketServerExample Tango Client: http://www.dsource.org/projects/tango/wiki/SocketHelloExample I'm referring to these two examples. In the server Example, I strip off the client part and it becomes as follow. void main() { const int port = 8080; // thread body for socket-listener void run() { auto server = new ServerSocket (new IPv4Address(port)); // wait for requests auto request = server.accept; // write a response request.output.write ("server replies 'hello'"); } Cout("server started"); while (true ) { // start server in a separate thread, and wait for it to start (new Thread (&run)).start; tango.core.Thread.thread_sleep (0.250); } } Situation: I have my server running and I open a client in another cmd console window and it replied alright. But if I try to connect it again second time, it freezes. Why is that? I thought the server is constantly receiving messages and replying, why would it freeze up and stop responding? what went wrong? please advise. Thanks
Jun 19 2014
I modify the server program as follow and it works ok. But how do I use thread to expedite the execution? void main() { const int port = 8080; auto server = new ServerSocket (new IPv4Address(port)); Cout("server started"); while (true ) { // wait for requests auto request = server.accept; // write a response request.output.write ("server replies 'hello'"); request.close; } }
Jun 19 2014
I solved it. void main() { const int port = 8080; auto server = new ServerSocket (new IPv4Address(port)); Cout("server started").flush; while (true ) { // wait for requests auto request = server.accept; // write a response // // start server in a separate thread, and wait for it to start (new Thread ( { // do lots of things here or add some functions for processing request.output.write ("server replies 'hello'"); request.close; })).start; } }
Jun 19 2014