Example #1
0
func receiveMsg(conn net.Conn) {
	buf := make([]byte, 128)
	n, err := conn.Read(buf)
	if checkerr(err) {
		os.Exit(-1)
		return
	}
	// fmt.Printf("\nread (%d) byte from %v :\n%v\n", n, conn.RemoteAddr(), buf[:n])

	protobuf := proto.NewBuffer(buf[:n])
	var _msg msg.OSMsg
	pumerr := protobuf.Unmarshal(&_msg)
	if checkerr(pumerr) {
		return
	}
	fmt.Printf("[received <----- message]%v\n", _msg.String())
}
Example #2
0
func sendMsg(conn net.Conn) chan bool {
	// _msg := msg.OSMsg{Fromu: proto.String("jack"), Tou: proto.String("tom"), Content: proto.String("hello,tom")}
	_msg := msg.OSMsg{Fromu: proto.String("tom"), Tou: proto.String("jack"), Content: proto.String("hello,jack")}
	end := make(chan bool)
	for {
		// send
		data, err := proto.Marshal(&_msg)
		if checkerr(err) {
			end <- true
			break
		}
		conn.Write(data)

		proto.Unmarshal(data, &_msg)
		fmt.Printf("[send -----> message]%s\n", _msg.String())
		time.Sleep(4e9)
	}
	return end
}