1、c++ acl udp服务器客户端简单实例-客户器端(2)

// UDPServerDemo.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include "acl_cpp/lib_acl.hpp"

using namespace acl;

int _tmain(int argc, _TCHAR* argv[])

{

acl_cpp_init();

const char* server_ip = "127.0.0.1:12345";

const int BUF_SIZE = 4096;

char recv_buf[BUF_SIZE];

acl::socket_stream udp_client;

const char* ppp = NULL;

if (!udp_client.bind_udp("127.0.0.1:0", -1)) {

return 1;

}

udp_client.set_peer(server_ip);

std::string send_msg;

while (true) {

std::cout << "\n请输入要发送的内容: ";

std::getline(std::cin, send_msg);

if (send_msg == "exit") {

break;

}

if (send_msg.empty()) {

continue;

}

int send_len = udp_client.write(send_msg.c_str(), send_msg.size());

if (send_len <= 0) {

continue;

}

std::cout << "发送成功,字节数: " << send_len << ",内容: " << send_msg << std::endl;

int recv_len = udp_client.read(recv_buf, BUF_SIZE, false);

if (recv_len <= 0) {

continue;

}

recv_buf[recv_len] = '\0';

std::cout << "收到服务端回显: " << recv_buf << std::endl;

}

udp_client.close();

return 0;

}

相关推荐
落羽的落羽7 小时前
【网络】计算机网络世界的基础概念
linux·服务器·网络·c++·人工智能·计算机网络·机器学习
stars-he7 小时前
基于 Design Compiler 的 UDP Payload 追加控制模块综合与门级后仿真
笔记·fpga开发·udp
时空自由民.7 小时前
蓝牙GAP/GATT协议和计算机网络TCP/UDP通信对比
tcp/ip·计算机网络·udp
TBrL7UtdTELTTdut4BAL7 小时前
F7015TV3 光猫 Telnet 命令配置 DHCP 服务器
运维·服务器·网络
海兰7 小时前
将 Cursor 连接到生产日志:通过 Elastic MCP 服务器
运维·服务器·elasticsearch
计算机安禾7 小时前
【Linux从入门到精通】第41篇:Linux内核编译初体验——裁剪属于你自己的内核
linux·运维·服务器
handler017 小时前
算法:图的基本概念
c语言·开发语言·c++·笔记·算法·图论
科技苑7 小时前
C++ 与 QML 交互入门“陷阱”全解析
c++
谷哥的小弟8 小时前
(最新版)腾讯云服务器项目部署教程(4)— 部署项目
linux·运维·服务器·云计算·腾讯云·云服务器·项目部署