Sockets between D and C(++) app

bearophile bearophileHUGS at
Tue Apr 1 17:34:07 PDT 2014

Alexandre L.:

Some comments on your code:

> Here's my 'server' code:
> int main(string[] args)
> {

If you don't need args, then I suggest to not put it as main 
argument. So probably this is better (note no int nor return, in 
D they are not needed):

void main() {

> 		int count = s.receiveFrom(recv_buf);

It's better to use immutable here, and infer the type (use 
const/immutable everywhere you don't really need to mutate a 

		immutable count = s.receiveFrom(recv_buf);

> 		char[] test = cast(char[])recv_buf[0..count-1]; // -1 for C 
> string comp.
> 		writefln("Received: %s\n", test);
> 		char[] rep = "regan\0".dup;
> 		s.send(cast(ubyte[])rep);

casts are dangerous, because they silently assume you know what 
you are doing. As first try I suggest you to remove every cast() 
from your D program, and replace them with to!() or other 
functions like toStringz. Sometimes this is not the most 
efficient thing to do, but it's safer, so it's better when you 
start to learn D.


More information about the Digitalmars-d-learn mailing list