December 27, 2020
Hello, i want to create basic example of 2 apps which will be exchanging data through socket connection. I have:

server
======
import std.stdio;
import std.string;
import std.socket;


void main()
{
	auto sock = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.IP);
	sock.bind(new InternetAddress("0.0.0.0", 7777));
	sock.listen(1);
	Socket incommingConn = sock.accept();

	void[] buff;
	incommingConn.receive(buff);
	writeln((cast(ubyte[])buff).assumeUTF());

	buff = cast(void[])representation("OK.");
	incommingConn.send(buff);
	incommingConn.close();
	sock.close();
}

Client
======
import std.stdio;
import std.string;
import std.socket;


void main()
{
	auto sock = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.IP);
	sock.connect(new InternetAddress("0.0.0.0", 7777));
	void[] buff = ['a', 's', 'd', 'f'];
	sock.send(buff);
	sock.receive(buff);
	writeln((cast(ubyte[])buff).assumeUTF());
	sock.close();
}


I was expecting to see string "asdf" in server terminal, but there is only blank line. Also client prints "OK.f" instead of "Ok.". I'm doing something wrong?



December 27, 2020
On Sunday, 27 December 2020 at 17:49:10 UTC, BPS wrote:
> 	void[] buff;
> 	incommingConn.receive(buff);

this has no actual space to receive anything

> 	void[] buff = ['a', 's', 'd', 'f'];
> 	sock.send(buff);
> 	sock.receive(buff);

and the return value needs to be checked to tell you how much actually got received.


The Phobos socket api is a very thin wrapper over the C function and work basically the same way: you must pre-allocate buffers and check return values.

I wrote about it at length in my blog last year:

http://dpldocs.info/this-week-in-d/Blog.Posted_2019_11_11.html

so I'll just point you there instead of repeating much more, there's full examples on the link with comments describing the details of what is going on.

You probably want the "Communication by stream" part of the table of contents and can jump there, but the whole page might be helpful.