Source

go-play / nc.go

Full commit
package main

import (
	"flag"
	"fmt"
	"net"
	"old/netchan"
	"os"
	"time"
)

func server() {
	out := make(chan time.Time)

	exporter := netchan.NewExporter()
	exporter.Export("seconds", out, netchan.Send)
	exporter.ListenAndServe("tcp", ":5959")

	for {
		out <- time.Now()
		time.Sleep(1e9)
	}

}

func client() {
	conn, err := net.Dial("tcp", "localhost:5959")
	if err != nil {
		panic(fmt.Sprintf("can't connect"))
	}

	importer := netchan.NewImporter(conn)
	in := make(chan time.Time)
	if err := importer.Import("seconds", in, netchan.Recv, -1); err != nil {
		panic("can't import")
	}

	for {
		value := <-in
		fmt.Println(value)
	}
}

func main() {
	defer func() {
		if err := recover(); err != nil {
			fmt.Fprintf(os.Stderr, "error: %s\n", err)
			os.Exit(1)
		}
	}()

	flag.Parse()
	switch flag.Arg(0) {
	case "server":
		server()
	case "client":
		client()
	default:
		panic(fmt.Sprintf(
			"unknown command %s (user either 'server' or 'client'",
			flag.Arg(0)))
	}
}