go-play / nc.go

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)))
	}
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.