/** * Copyright (C) 2015-2018 * All rights reserved. * * AUTHOR(S) * Zheng Shuxin * E-mail: zhengshuxin@qiyi.com * * VERSION * Tue 30 May 2017 10:32:58 PM CST */ #include "stdafx.h" #include "udp.h" static void mio(udp_sock& sock, int inter, bool echo) { #define PKT_CNT 20 #define BUF_LEN 1024 udp_pkts upkts(PKT_CNT); std::vector& pkts = upkts.get_pkts(); int nread = 0, total_read = 0, n = 0, nwrite = 0; while (true) { int ret = sock.recv(pkts); if (ret <= 0) { printf("read error %s, ret: %d\r\n", acl::last_serror(), ret); break; } nread++; total_read += ret; if (echo) { ret = sock.send(pkts, ret); if (ret <= 0) { printf("send error %s, ret: %d\r\n", acl::last_serror(), ret); break; } nwrite++; } if (++n % inter == 0) { char buf[256], ip[64]; snprintf(buf, sizeof(buf), "curr: %d, total: %d, ret: %d, dlen: %lu, " "nread: %d, nwrite: %d, ip: %s, port: %d", n, total_read, ret, pkts[0]->get_dlen(), nread, nwrite, pkts[0]->get_ip(ip, sizeof(ip)), pkts[0]->get_port()); ACL_METER_TIME(buf); } } } int main(void) { const char* addr = "127.0.0.1:8888"; udp_sock sock; if (sock.server_open(addr) == false) { printf("server_open %s error %s\r\n", addr, acl::last_serror()); return 1; } mio(sock, 50000, true); return 0; }