client:
#include <iostream>
#include "hv/UdpClient.h"
#pragma comment(lib,"hv.lib")
class client
{
public:
client()
{
cli = nullptr;
}
~client()
{
SAFE_DELETE(cli);
}
int send(const std::string& msg)
{
return cli->sendto(msg);
}
bool start(int port, const char* host)
{
cli = new hv::UdpClient;
int sockfd = cli->createsocket(port, host);
if (sockfd < 0) return false;
cli->start();
return true;
}
private:
hv::UdpClient* cli;
};
int main()
{
client* clie = new client();
clie->start(8080, "127.0.0.1");
std::string s;
std::cin >> s;
while(getchar())
{
clie->send(s);
std::cin >> s;
}
}
server:
#include <iostream>
#include "hv/UdpServer.h"
#pragma comment(lib,"hv.lib")
void rev(const hv::SocketChannelPtr& channel, hv::Buffer* buf)
{
std::cout << (char*)buf->data() << std::endl;
std::cout << "data size:" << buf->size() << std::endl;
}
class server
{
public:
server()
{
ser = nullptr;
}
~server()
{
SAFE_DELETE(ser);
}
bool start(int port, const char* host)
{
ser = new hv::UdpServer;
int sockfd = ser->createsocket(port, host);
if (sockfd < 0) return false;
ser->onMessage = rev;
ser->start();
return true;
}
private:
hv::UdpServer* ser;
};
int main()
{
server* se = new server();
se->start(8080, "127.0.0.1");
getchar();
}
标签:udp,return,cli,libhv,int,hv,测试程序,start,client
From: https://www.cnblogs.com/horizonhz/p/17614962.html