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 user32;

char pwd32;

};

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:

{

mapClientUserchannel-\>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 user32;

char pwd32;

};

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完成

相关推荐
2501_915106323 小时前
TraceEagle 代理抓包教程 本机和手机的 HTTPS 抓包方法
网络协议·计算机网络·网络安全·ios·adb·https·udp
DLYSB_4 小时前
基于 HTTP API 与 Modbus 协议的多源监控声光语音联动告警系统设计与实现
网络·网络协议·http·报警灯
Bobolink_5 小时前
手机端和电脑端的网络环境配置有什么不同?
网络·智能手机·电脑·网络环境
星恒讯工业路由器6 小时前
三大工业以太网协议选型对比
网络·物联网·ethercat·profinet·ethernet/ip·工业以太网协议
春卷同学6 小时前
HarmonyOS掌上记账APP开发实践第25篇:Image 组件高级用法 — 资源引用、网络图片与首字符占位方案
网络·华为·harmonyos
mounter6256 小时前
从内存隔离到运行时防线:透视 Linux 内核安全强化的博弈与演进
linux·网络·安全·linux kernel·kernel
Bug退散师8 小时前
多路IO复用[select版TCP服务器与poll版TCP服务器]与常用网络编程函数详解
linux·服务器·c语言·网络·驱动开发·tcp/ip
Acrellea9 小时前
固态变压器(SST)热管理破局:安科瑞交直流专属测温方案护航无人值守运维
运维·网络
振浩微433射频芯片10 小时前
5个IO控制20个LED?查理复用实战拆解
网络·单片机·物联网·学习·智能家居
rcms1527026921810 小时前
IS200TREAH2A 终端板
网络