Sockets between D and C(++) app

Alexandre L. alex.cs00 at mail.com
Tue Apr 1 17:12:56 PDT 2014


Hello,
I lately did a minimal udp socket server-client application with 
C++, and I've been trying to recreate it with D, after.

I'm able to get the client's request (C++ client) without too 
much trouble (after I figured I needed to get it in ubyte[]).

Then I've tried to send the client an answer through send() (then 
tried sendTo() ) but none of them work.

Here's my 'server' code:

int main(string[] args)
{
	auto s = new UdpSocket();

	auto addr = new InternetAddress("127.0.0.1", 6666);
	s.setOption(SocketOptionLevel.IP, SocketOption.REUSEADDR, true);
	s.bind(addr);

	while (true)
	{
		ubyte[2048] recv_buf;
		int 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);
	}

	writeln("End!");

	return 0;
}

Then the client tries to get the string in a std::string, hence 
why I did input the \0 literal. I'm also fairly new to D, so any 
guidance is sure welcomed!

Alexandre


More information about the Digitalmars-d-learn mailing list