www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - [Code Example for Beginners (like me)] Sockets with Fibers

reply ddos <oggs gmx.at> writes:
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
Sep 18 2015
parent Alex Parrill <initrd.gz gmail.com> writes:
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).
Sep 18 2015