Пример #1
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.NewTcpSocket(conn)
		c := connection.NewConnection(sock, 10240, &time_handler{}, nil, 5*time.Second)

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

			cc := make(chan bool)
			<-cc

			//for {
			//    _, err := conn.Write([]byte(time.Now().String()))
			//    //rsp, err := conn.Query([]byte(time.Now().String()))
			//    if err != nil {
			//        return
			//    }

			//    time.Sleep(time.Second)
			//}

		}(c)

	}
}
Пример #2
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.NewTcpSocket(conn)
	c := connection.NewConnection(sock, 10240, nil, nil, 5*time.Second)

	//defer func() {
	//	fmt.Println("closed from server")
	//	c.Close()
	//}()
	p := 3
	synch := make(chan bool, p)
	for i := 0; i < p; i++ {
		synch <- true
	}

	count := 1
	for {
		fmt.Println("\ntimes:", count)
		select {
		case <-synch:
			go func(ch chan bool) {

				rsp, err := c.Query([]byte("time"))
				fmt.Println(string(rsp), err)
				ch <- true
			}(synch)
		}

		//time.Sleep(time.Second)

		count += 1

	}

}