func main() { fmt.Println("time = ", time.Now().Second()) runtime.GOMAXPROCS(runtime.NumCPU() - 1) s1 := NewMyServer() err := s1.Init(`{"ip":"127.0.0.1","port":12000}`) if err != nil { fmt.Println("net server init err,", err) return } s1.SetListenAddress("0.0.0.0") s1.SetListenPort(12000) s1.SetPackParser(netio.NewDefaultParser()) s1.SetPackDispatcher(NewServerDispatcher()) s1.GetPackDispatcher().AddPackEventListener("myserver", s1) s1.SetQos(netqos.NewServerQos()) s1.GetQos().SetEnable(true) s1.GetQos().Stat() go s1.Start() //启动服务器 console.CheckInput() s1.Close() }
func (n *NodeService) InitService() error { n.SetListenAddress(ProxyConf.ForwardIp) n.SetListenPort(uint16(ProxyConf.ForwardPort)) n.SetPackParser(netio.NewDefaultParser()) n.SetPackDispatcher(n) n.GetPackDispatcher().AddPackEventListener("nodeserver", n) n.Init(n.GetConfigJson()) go n.Start() return nil }
func main() { runtime.GOMAXPROCS(runtime.NumCPU() - 1) //创建N个客户端 clientList := make(map[int]*MyClient) for i := 0; i < 3000; i++ { go func() { c1 := NewMyClient() c1.SetPackDispatcher(netio.NewDefaultPackDispatcher()) c1.SetPackParser(netio.NewDefaultParser()) c1.SetServerAddress("192.168.15.26") c1.SetServerPort(12000) c1.GetPackDispatcher().AddPackEventListener(strconv.Itoa(i+1), c1) err := c1.Connect() //启动客户端,看乒乓能不能打起来,哈哈! if err != nil { fmt.Println("client error,", err) } clientList[i+1] = c1 login := protocol_login.Login_Req{} login.Packet = packet.NewEmptyPacket() login.CmdID = 0x01 login.UserName = "******" login.PWD = "1q2323" buf := login.EncodePacket(512) time.Sleep(20 * time.Millisecond) //fmt.Println("client send data :", buf.GetData()) for { time.Sleep(time.Millisecond * 500) c1.Write(buf.GetData()) } }() } console.CheckInput() }