ubuntu C/C++UDP客户端的一个简单实现

昨天项目上要增加一个将算法结果通过UDP协议发送报文给用户服务端的小功能,自己百度了相关资料,发现网上的代码都是将启动UDP套接字和发送数据写在一个函数里, 这样就造成使用的时候,会重复启动套接字。于是我结合自己的项目,写了一个UDP_CLIENT类,可以实现动态ip, port设置,将启动套接字和发送数据函数分开实现。

1.头文件

#ifndef UDP_CLIENT_H
#define UDP_CLIENT_H
#include <iostream>
#include <fsteam>
#include <cstring>
#include <ifstream>
#include <vector>
#include <string>

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;


struct Udp{
   
	int client_sockfd; //套接字
	struct sockaddr_in remote_addr;  //服务器端网络地址结构体
};


class Udp_client{
       

public:
    Udp_client(string config_path);
    ~Udp_client();
    //启动udp连接获得套接字
    struct Udp get_scokfd(); 
    //发送数据
    bool send_udp(int x1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值