Exemplo n.º 1
0
func main() {

	if len(os.Args) < 3 {
		fmt.Printf("usage ./transferclient <filename> <savefilename\n")
		return
	}
	service := "127.0.0.1:8010"
	tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
	if err != nil {
		fmt.Printf("ResolveTCPAddr")
	}
	conn, err := net.DialTCP("tcp", nil, tcpAddr)
	if err != nil {
		fmt.Printf("DialTcp error,%s\n", err)
	} else {
		session := socket.NewTcpSession(conn)
		fmt.Printf("connect sucessful\n")
		//发出文件请求
		wpk := packet.NewWPacket(packet.NewByteBuffer(64))
		wpk.PutUint16(request_file)
		wpk.PutString(os.Args[1])
		session.Send(wpk)
		tsession := &transfer_session{filename: os.Args[2]}
		session.SetUd(tsession)
		socket.ProcessSession(session, packet.NewRPacketDecoder(65535), process_client)
	}
}
Exemplo n.º 2
0
func main() {

	clientcount := int32(0)
	packetcount := int32(0)

	clients := util.NewConnCurrMap()

	service := ":8010"
	tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
	if err != nil {
		fmt.Printf("ResolveTCPAddr")
	}
	listener, err := net.ListenTCP("tcp", tcpAddr)
	if err != nil {
		fmt.Printf("ListenTCP")
	}

	ticker := util.DurationTicker()
	ticker.Start(1000, func(_ time.Time) {
		tmp := atomic.LoadInt32(&packetcount)
		atomic.StoreInt32(&packetcount, 0)
		fmt.Printf("clientcount:%d,packetcount:%d/s\n", clientcount, tmp)
	})

	for {
		conn, err := listener.Accept()
		if err != nil {
			continue
		}
		session := socket.NewTcpSession(conn)
		clients.Set(session, session)
		atomic.AddInt32(&clientcount, 1)
		go socket.ProcessSession(session, packet.NewRPacketDecoder(1024),
			func(session *socket.Tcpsession, rpk packet.Packet, errno error) {
				if rpk == nil {
					session.Close()
					atomic.AddInt32(&clientcount, -1)
					fmt.Printf("error:%s\n", errno)
					clients.Del(session)
					return
				}
				for _, v := range clients.Vals() {
					atomic.AddInt32(&packetcount, int32(1))
					wpk := rpk.MakeWrite()
					v.(*socket.Tcpsession).Send(wpk)
				}
			})
	}
}
Exemplo n.º 3
0
func main() {
	service := "127.0.0.1:8010"
	tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
	if err != nil {
		fmt.Printf("ResolveTCPAddr")
	}
	listener, err := net.ListenTCP("tcp", tcpAddr)
	if err != nil {
		fmt.Printf("ListenTCP")
	}
	loadfile()
	for {
		conn, err := listener.Accept()
		if err != nil {
			continue
		}
		session := socket.NewTcpSession(conn)
		fmt.Printf("a client comming\n")
		go socket.ProcessSession(session, packet.NewRPacketDecoder(4096), process_client)
	}
}
Exemplo n.º 4
0
func main() {
	var i uint32
	for i = 1; i <= 100; i++ {
		service := "127.0.0.1:8010"
		tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
		if err != nil {
			fmt.Printf("ResolveTCPAddr")
		}
		conn, err := net.DialTCP("tcp", nil, tcpAddr)
		if err != nil {
			fmt.Printf("DialTcp error,%s\n", err)
		} else {
			session := socket.NewTcpSession(conn)
			wpk := packet.NewWPacket(packet.NewByteBuffer(uint32(64)))
			wpk.PutUint32(uint32(i))
			session.Send(wpk)
			idx := i
			go socket.ProcessSession(session, packet.NewRPacketDecoder(1024),
				func(session *socket.Tcpsession, rpk packet.Packet, errno error) {
					if rpk == nil {
						fmt.Printf("error:%s\n", errno)
						session.Close()
						return
					}
					r := rpk.(*packet.RPacket)
					id, _ := r.Uint32()
					if id == idx {
						session.Send(r.MakeWrite())
					}
				})
		}
	}

	for {
		time.Sleep(10000000)
	}
}