Beispiel #1
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	addr, err := net.ResolveTCPAddr("tcp", os.Args[1])
	if err != nil {
		panic(err)
	}

	conn, err := net.DialTCP("tcp", nil, addr)
	if err != nil {
		panic(err)
	}

	sock := connection.NewSocket(conn)
	c := connection.NewConnection(sock, 10240, nil, nil)

	p := 3000
	synch := make(chan bool, p)
	for i := 0; i < p; i++ {
		synch <- true
	}

	for {

		select {
		case <-synch:
			go func(ch chan bool) {
				rsp, err := c.Query([]byte("time"), 1000) //timeout: 1s
				fmt.Println("Query():", string(rsp), err)

				err = c.Send([]byte("time"))
				fmt.Println("Send() error:", err)

				ch <- true
			}(synch)
		}
		time.Sleep(time.Second * 10)
	}

}
Beispiel #2
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	addr, err := net.ResolveTCPAddr("tcp", "0.0.0.0:5555")
	if err != nil {
		panic(err)
	}

	l, err := net.ListenTCP("tcp", addr)
	if err != nil {
		panic(err)
	}
	defer l.Close()

	for {
		conn, err := l.AcceptTCP()
		if err != nil {
			panic(err)
		}

		fmt.Println("Connected from", conn.RemoteAddr().String())

		sock := connection.NewSocket(conn)
		c := connection.NewConnection(sock, 10240, &time_handler{}, nil)

		go func(conn connection.Connection) {
			defer func() {
				fmt.Println("Disconnected from")
				conn.Close()
			}()

			cc := make(chan bool)
			<-cc
		}(c)

	}
}