Send and receive in socket

Downs default_357-line at yahoo.de
Wed Aug 15 14:02:29 PDT 2007


Charma wrote:
> Hello,
> I try to use the std.socket to send data from a client to a "server". Up
> to now i managed to get a connection and to send strings and chars of
> text but i am not able to send and receive floats or other types of
> numbers...
> Maybe anyone knows how to do that?
> the socket-command only accepts void[] and i have no idea how to make an
> array from the 4 bytes that a float has...
> 
> Thanks for any help,
> sorry for my engrish...
> 
> Charma
well there's always the cheap way to do it
void socksend(T)(Socket s, T value) { s.send((cast(void
*)&T)[0..T.sizeof]); }

Note that this will break utterly for reference types and arrays, but
you get the basic idea. It would probably be possible to make an
extended version via static if that handles arrays correctly (deep
copying), but I'm too lazy. :p
 --downs


More information about the Digitalmars-d-learn mailing list