Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:similar to gopl.io/ch8/netcat3 , using a Tid instead of a channel
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 0fc740f25afc9d313c9fe2f3082aa020cdee14a147352e3c7e8cf0b2cbafa81f
User & Date: admin 2021-10-30 20:43:47
Context
2021-10-30
21:12
similar to gopl.io/ch8/pipeline1 Leaf check-in: 680ec67a2d user: admin tags: trunk
20:43
similar to gopl.io/ch8/netcat3 , using a Tid instead of a channel check-in: 0fc740f25a user: admin tags: trunk
19:55
add Concurrent Clock Server example from Go Programming Language check-in: 5b30d6891f user: admin tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added par/3-ncdone/client.d.





























































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import std.socket : TcpSocket, InternetAddress, SocketShutdown;
import std.stdio : stdout, writeln, stdin;
import std.exception : errnoEnforce;
import std.concurrency : spawn, send, Tid, thisTid, receiveOnly;

struct Done { }

void main() {
    auto client = new TcpSocket;
    client.connect(new InternetAddress("localhost", 8000));

    spawn((shared(TcpSocket) socket, Tid done) {
        auto conn = cast(TcpSocket) socket;
        ubyte[4096] buffer;

        while (auto len = conn.receive(buffer[])) {
            errnoEnforce(len > 0, "socket receive failed");
            stdout.write(cast(char[]) buffer[0 .. len]);
        }
        writeln("done");
        done.send(Done());
    }, cast(shared(TcpSocket)) client, thisTid);

    foreach (line; stdin.byLine)
        client.send(line ~ "\n");

    client.shutdown(SocketShutdown.BOTH);
    client.close;
    receiveOnly!Done;
}