Exchange of data through socket connection.

BPS jaroslaw.janikowski at gmail.com
Sun Dec 27 17:49:10 UTC 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?





More information about the Digitalmars-d-learn mailing list