Receiving data into a type using sockets

David Nadlinger see at klickverbot.at
Sun Jun 5 11:52:33 PDT 2011


On 6/5/11 8:51 PM, Jonathan Sternberg wrote:
> I'm trying to learn how to do some networking code in D. In C, if I wanted to
> read 4 bytes into an integer, I could just do (ignoring any error detection):
>
> int foo;
> recv(sockfd,&foo, sizeof(int), 0);
>
> How do I do this with D? The receive function takes in a void [] parameter and
> doesn't take in a number of bytes to read. Is there anyway to accomplish this
> in D?

auto socket = new Socket(…);
…
int foo;
socket.receive((&foo)[0..1]);

---

void[] is like an array of bytes, with the difference to ubyte[] being 
that it can contain pointers which must be taken into account by the GC, 
and implicit convertibility. However, all the socket functions should 
operate on ubyte[] instead of void[] by current guidelines – the socket 
module is really old, this will definitely be changed when it will be 
overhauled/replaced.

David


More information about the Digitalmars-d-learn mailing list