Пример #1
0
func listener(name string, port int, fn workers.WorkerFunc) {
	//fmt.Printf("name = %s; port = %d\n",name,port)
	laddr := fmt.Sprintf("0.0.0.0:%d", port)
	addr, e := net.ResolveTCPAddr("tcp", laddr)
	if e != nil {
		fmt.Printf("Cannot resolve address %s\n", laddr)
		os.Exit(1)
	}
	socket, e := net.ListenTCP("tcp", addr)
	if e != nil {
		fmt.Printf("Failed to listen on address %s\n", laddr)
		os.Exit(2)
	}
	fmt.Printf("%s server listening on port %d\n", name, port)

	log := utils.NewLog(name)
	for {
		conn, e := socket.AcceptTCP()
		if e != nil {
			fmt.Printf("Accept failed on address %s\n", laddr)
			//break
			time.Sleep(60000000000) // 30 min
		}
		log.Printf("Connected to remote address %s", conn.RemoteAddr())
		go fn(name, conn)
	}

}
Пример #2
0
func Start() {
	var p = flag.Int("p", 8000, "base port")
	var help = flag.Bool("h", false, "help")
	flag.Parse()
	if *help || flag.NArg() != 0 {
		fmt.Printf("Usage: server\n")
		flag.PrintDefaults()
		return
	}

	log := utils.NewLog("server")
	log.Printf("Server started.")
	utils.Version()
	workers.Apply(listener, *p)
	for {
		fmt.Printf("++++ %s\n", time.Now().Local().Format(time.UnixDate))
		time.Sleep(1800000000000) // 30 min
	}
}