libhv vs2019 udp简单的实例

项目依赖头文件与库文件之后

(1)服务端

// HttpServerDemo.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。

//

using namespace hv;

#include <string>

#include <vector>

#include "hv/hv.h"

using namespace std;

#include "hv/UdpServer.h"

#include <iostream>

using namespace hv;

#pragma pack(push, 1)

enum PacketType {

LOGIN = 1, // 登录

LOGOUT = 2, // 退出

USER_DATA = 3 // 自定义结构体数据

};

struct LoginMsgData

{

uint32_t type;

char user[32];

char pwd[32];

};

struct LogoutMsgData

{

uint32_t type;

int nId;

};

// 主机字节序 → 网络字节序

static inline uint32_t hton_u32(uint32_t value) {

return htonl(value);

}

static inline uint32_t get_u32(const void* data) {

return ntohl(*(const uint32_t*)data);

}

map<int, string> mapClientUser;

int main(int argc, char* argv[]) {

int port = 8080;

UdpServer srv;

int bindfd = srv.createsocket(port);

if (bindfd < 0) {

return -20;

}

printf("server bind on port %d, bindfd=%d ...\n", port, bindfd);

srv.onMessage = [](const SocketChannelPtr& channel, Buffer* buf) {

uint32_t type = get_u32(buf->data());

switch (type)

{

case LOGIN:

{

mapClientUser[channel->fd()] = channel->peeraddr();

printf("< 唯一标识:%d - ip地址:%s\n", channel->fd(),

channel->peeraddr().c_str());

if (buf->size() < sizeof(LoginMsgData)) return;

LoginMsgData* pkt = (LoginMsgData*)buf->data();

channel->write("login success");

break;

}

case LOGOUT:

{

printf("< 唯一标识:%d - ip地址:%s\n", channel->fd(),

channel->peeraddr().c_str());

if (buf->size() < sizeof(LogoutMsgData)) return;

channel->write("logout success");

auto itrFind = mapClientUser.find(channel->fd());

if (itrFind != mapClientUser.end())

{

mapClientUser.erase(itrFind);

}

break;

}

default:

{

printf("❌ 未知包类型\n");

break;

}

}

};

srv.start();

std::string str;

while (std::getline(std::cin, str)) {

if (str == "close") {

srv.closesocket();

}

else if (str == "start") {

srv.start();

}

else if (str == "stop") {

srv.stop();

break;

}

else {

srv.sendto(str);

}

}

return 0;

}

#pragma pack(pop)

(2)客户端

#include "hv/UdpClient.h"

#include <iostream>

#include <string>

#pragma pack(push, 1)

using namespace std;

using namespace hv;

enum PacketType {

LOGIN = 1, // 登录

LOGOUT = 2, // 退出

USER_DATA = 3 // 自定义结构体数据

};

struct LoginMsgData

{

uint32_t type;

char user[32];

char pwd[32];

};

struct LogoutMsgData

{

uint32_t type;

int nId;

};

static inline uint32_t hton_u32(uint32_t value) {

return htonl(value);

}

static inline uint32_t get_u32(const void* data) {

return ntohl(*(const uint32_t*)data);

}

int main(int argc, char* argv[]) {

int remote_port = 8080;

const char* remote_host = "127.0.0.1";

UdpClient cli;

int sockfd = cli.createsocket(remote_port, remote_host);

if (sockfd < 0) {

return -20;

}

printf("client sendto port %d, sockfd=%d ...\n", remote_port, sockfd);

cli.onMessage = [](const SocketChannelPtr& channel, Buffer* buf) {

printf("< %s\n", (char*)buf->data());

};

cli.start();

{

LoginMsgData login{};

login.type = hton_u32(PacketType::LOGIN);

strcpy(login.user, "admin");

strcpy(login.pwd, "123456");

cli.sendto(&login, sizeof(login));

}

{

LogoutMsgData _logout{};

_logout.type = hton_u32(PacketType::LOGOUT);

_logout.nId = 123;

cli.sendto(&_logout, sizeof(_logout));

}

std::string str;

while (std::getline(std::cin, str)) {

if (str == "close") {

}

else if (str == "start") {

}

else if (str == "stop") {

}

else {

}

}

return 0;

}

#pragma pack(pop)

至此,一个简单的udp服务器端与客户端的demo完成

相关推荐
初願致夕霞40 分钟前
基于系统调用的Linux网络编程——UDP与TCP
linux·网络·c++·tcp/ip·udp
数智化精益手记局2 小时前
什么是设备维护管理?设备维护管理包含哪些内容?
大数据·网络·人工智能·安全·信息可视化
salipopl5 小时前
FPGA中AXI-FIFO主机接口的自定义实现与versal读写工程分析
网络·fpga开发
会周易的程序员6 小时前
aiDgeScanner 工业设备网络扫描与管理工具
网络·c++·物联网·架构·electron·node.js·iot
CableTech_SQH6 小时前
F5G 全光网,赋能智慧校园数字化建设
大数据·网络·5g·运维开发·信息与通信
hellojackjiang20117 小时前
socket长连接在手游场景下的技术实践
网络·网络协议·tcp/ip·架构·网络编程
精益数智小屋7 小时前
设备维护方案核心功能拆解:一套好的设备维护方案如何解决设备突发故障
大数据·运维·网络·数据库·人工智能·面试·自动化
其实防守也摸鱼7 小时前
VS code怎么使用 Conda 安装预编译包
开发语言·网络·c++·vscode·安全·web安全·conda
zhangfeng11337 小时前
IB = InfiniBand:一种超高速、低延迟的专用网络 和和一般我们在用的光纤网络的区别
网络