func main() {
	var addr, conf string
	flag.StringVar(&addr, "a", "127.0.0.1:9876", "`address` of server")
	flag.StringVar(&conf, "f", "client.json", "config `file` to use")
	flag.Parse()

	var id = new(core.Identity)
	data, err := ioutil.ReadFile(conf)
	if err != nil {
		exlib.Err(1, err, "reading config file")
	}

	err = json.Unmarshal(data, id)
	if err != nil {
		exlib.Err(1, err, "parsing config file")
	}

	tr, err := transport.New(exlib.Before, id)
	if err != nil {
		exlib.Err(1, err, "creating transport")
	}

	conn, err := transport.Dial(addr, tr)
	if err != nil {
		exlib.Err(1, err, "dialing %s", addr)
	}
	defer conn.Close()

	for _, msg := range messages {
		if err = exlib.Pack(conn, []byte(msg)); err != nil {
			exlib.Err(1, err, "sending message")
		}

		var resp []byte
		resp, err = exlib.Unpack(conn)
		if err != nil {
			exlib.Err(1, err, "receiving message")
		}

		if !bytes.Equal(resp, []byte("OK")) {
			exlib.Errx(1, "server didn't send an OK message; received '%s'", resp)
		}
	}

	err = exlib.Pack(conn, []byte{})
	if err != nil {
		exlib.Err(1, err, "sending shutdown message failed")
	}

	fmt.Println("OK")
}
Exemple #2
0
func connHandler(conn net.Conn) {
	defer conn.Close()

	for {
		buf, err := exlib.Unpack(conn)
		if err != nil {
			exlib.Warn(err, "unpack message")
			return
		}

		if len(buf) == 0 {
			log.Info(conn.RemoteAddr(), " sent empty record, closing connection")
			return
		}

		log.Infof("received %d-byte message: %s", len(buf), buf)

		err = exlib.Pack(conn, []byte("OK"))
		if err != nil {
			exlib.Warn(err, "pack message")
			return
		}
	}
}