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

Overview
Comment:add Concurrent Clock Server example from Go Programming Language
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 5b30d6891f7516d63ba5c87bdc78fac459d3486fd961564fef02c53aaf6e1f48
User & Date: admin 2021-10-30 19:55:14
Context
2021-10-30
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
19:24
add 'spinner' example from Go Programming Language ch8 golang works 2.18x as hard (perf stat cycle count) to take 2.31x times as long (hyperfine runtime) as spinner.gdc. check-in: 40a2744c9d user: admin tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added par/2-clockd/client.d.

































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import std.socket : TcpSocket, InternetAddress;
import std.stdio : stdout;
import std.exception : errnoEnforce;

void main() {
    auto client = new TcpSocket;
    scope (exit)
        client.close;
    client.connect(new InternetAddress("localhost", 8000));
    ubyte[4096] buffer;

    while (auto len = client.receive(buffer[])) {
        errnoEnforce(len > 0, "socket receive failed");
        stdout.write(cast(char[]) buffer[0 .. len]);
    }
}

Added par/2-clockd/golang/go.mod.







>
>
>
1
2
3
module how.annoying/clockd

go 1.17

Added par/2-clockd/golang/main.go.





































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
31
32
33
34
package main

import (
	"io"
	"log"
	"net"
	"time"
)

func main() {
	listener, err := net.Listen("tcp", "localhost:8000")
	if err != nil {
		log.Fatal(err)
	}
	for {
		conn, err := listener.Accept()
		if err != nil {
			log.Print(err) // e.g., connection aborted
			continue
		}
		go handleConn(conn) // handle connections concurrently
	}
}

func handleConn(c net.Conn) {
	defer c.Close()
	for {
		_, err := io.WriteString(c, time.Now().Format("15:04:05\n"))
		if err != nil {
			return // e.g., client disconnected
		}
		time.Sleep(1 * time.Second)
	}
}

Added par/2-clockd/server.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
31
32
import std.concurrency : spawn;
import std.datetime : Clock, seconds;
import std.socket;
import core.thread : Thread;

void main() {
    import std.stdio : writeln;

    auto listener = new TcpSocket;
    listener.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, 1);
    listener.bind(new InternetAddress(8000));
    listener.listen(10);
    writeln("Listening on port 8000");

    while (true) {
        auto client = listener.accept;
        spawn(&handleConn, cast(shared(Socket)) client);
    }
}

void handleConn(shared(Socket) socket) {
    import std.format : format;

    auto client = cast(Socket) socket;
    scope (exit)
        client.close;
    while (true) {
        if (0 >= client.send(format!"%s\n"(Clock.currTime)))
            break;
        Thread.sleep(1.seconds);
    }
}