本文介绍了这样做的现代方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我有一个非常古老的c ++程序,它将UDP广播发送到我的本地局域网并将结果返回到一个char数组(this-> buf)



Hi all I have a really old c++ program that sends a UDP broadcast to my local LAN and returns the result in a char array (this->buf)

// Initial discovery broadcast - should return a request string in a char array.
this->Result = recvfrom(this->udpsocket, this->buf, sizeof(this->buf), 0, (sockaddr *)&this->si_other, &this->slen);





完美无缺,但我想知道在c ++中这样做的现代方法是什么?现代我的意思是使用一个容器而不是一个原始数组的字符作为返回值。



It works perfectly but I'm wondering what would be the *modern* way of doing this in c++ ? by modern I mean using a container other than a raw array of chars for the return value.

推荐答案

class MyClass {
private:
    typedef unisnged char Byte;
    enum { MY_MESSAGE_SIZE = 1024 };
    ...
    std::vector<Byte> buffer;
    ...
public:
    // constructor
    MyClass()
    : ...
    , buffer(MY_MESSAGE_SIZE) // IMPORTANT: This allocates a vector of the given size
    , ...
    { ... }

    void doSomethingUseful(...) {
        ...
        result = recvfrom(udpsocket, &(buffer[0]), MY_MESSAGE_SIZE, 0, (sockaddr *)&si_other, &slen);
        ...
    }
};

干杯

Andi

Cheers
Andi



这篇关于这样做的现代方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 05:09