[Code Example for Beginners (like me)] Sockets with Fibers

Alex Parrill via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Sep 18 06:34:20 PDT 2015


On Friday, 18 September 2015 at 07:58:10 UTC, ddos wrote:
> hello!
>
> yesterday i got curious about how fibers work, and if the can 
> be used as a replacement in network programming. so i started 
> hacking a small example together, which is hopefully useful to 
> other D beginners too :)
>
> http://pastebin.com/Xg4GJbKE

Two things:

In the line `string s = (cast(immutable(char)*)buf)[0..len];`, 
the cast to immutable is unsafe, as `buf` is modified on the next 
iteration and so the slice into it is not actually immutable. 
`writeln` can take a regular `char[]` fine, so you can just drop 
the immutable; otherwise you can do 
`cast(string)((buf[0..len]).idup)`.

Also, using select/poll/epoll would be better than looping and 
sleeping. std.socket includes a SocketSet class for doing this 
(though the API for it is kinda bad).


More information about the Digitalmars-d-learn mailing list