1 Star 0 Fork 262

1647606255 / libhv

forked from libhv / libhv 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
UdpClient_test.cpp 1.14 KB
一键复制 编辑 原始数据 按行查看 历史
hewei.it 提交于 2021-05-05 11:25 . sleep => hv_sleep
/*
* UdpClient_test.cpp
*
* @build
* make libhv && sudo make install
* g++ -std=c++11 UdpClient_test.cpp -o UdpClient_test -I/usr/local/include/hv -lhv -lpthread
*
*/
#include "UdpClient.h"
#include "htime.h"
using namespace hv;
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("Usage: %s port\n", argv[0]);
return -10;
}
int port = atoi(argv[1]);
UdpClient cli;
int sockfd = cli.createsocket(port);
if (sockfd < 0) {
return -20;
}
printf("client sendto port %d, sockfd=%d ...\n", port, sockfd);
cli.onMessage = [](const SocketChannelPtr& channel, Buffer* buf) {
printf("< %.*s\n", (int)buf->size(), (char*)buf->data());
};
cli.onWriteComplete = [](const SocketChannelPtr& channel, Buffer* buf) {
printf("> %.*s\n", (int)buf->size(), (char*)buf->data());
};
cli.start();
// sendto(time) every 3s
cli.loop()->setInterval(3000, [&cli](TimerID timerID) {
char str[DATETIME_FMT_BUFLEN] = {0};
datetime_t dt = datetime_now();
datetime_fmt(&dt, str);
cli.sendto(str);
});
while (1) hv_sleep(1);
return 0;
}
C/C++
1
https://gitee.com/AnsonWei/libhv.git
git@gitee.com:AnsonWei/libhv.git
AnsonWei
libhv
libhv
master

搜索帮助