tiny web server in D

Jos van Uden user at domain.invalid
Wed Jul 13 21:43:50 PDT 2011


On 14-7-2011 5:48, Dr.Smith wrote:
> import std.socket, std.string;
>
> void main() {
>      Socket listener = new TcpSocket;
>      assert(listener.isAlive);
>      listener.bind(new InternetAddress(8080));
>      listener.listen(10);
>      string webpage = "index.html";
>
>      Socket currSock;
>      uint bytesRead;
>      ubyte buff[1];
>
>      while(1) {
>          currSock = listener.accept();
>          while ((bytesRead = currSock.receive(buff))>  0) {
>             currSock.sendTo(webpage);
>          }
>          currSock.close();
>          buff.clear();
>      }
> }

I recieve

index.htmlindex.htmlindex.html etc etc

if I use this, it works

import std.socket, std.string;

void main() {
     Socket listener = new TcpSocket;
     assert(listener.isAlive);
     listener.bind(new InternetAddress(8080));
     listener.listen(10);
     string webpage = "<html><body>hi</body></html>";

     Socket currSock;
     uint bytesRead;
     ubyte buff[1];

     while(1) {
         currSock = listener.accept();
         if ((bytesRead = currSock.receive(buff)) > 0) {
            currSock.sendTo(webpage);
         }
         currSock.close();
         buff.clear();
     }
}


More information about the Digitalmars-d-learn mailing list