Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);


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.
Posted
Comments
dan!sh 28-Jan-16 8:09am    
Joke begins: Use MVC, responsive UI, web API and json. Everyone is doing it this way. Oh, and don't forget to host it on cloud and access from your refrigerator or watch. It will be as modern as it gets but may not do what you need it to.
Joke ends
OriginalGriff 28-Jan-16 8:19am    
If only some people didn't really think like that... :sigh:
pkfox 28-Jan-16 8:36am    
Poor man needs help :-)
Mohibur Rashid 28-Jan-16 19:56pm    
Well, those who like to think that way are considered 'not good enough'
Jochen Arndt 28-Jan-16 8:30am    
You may use a reinterpret_cast instead of the C cast. Nothing else from my point of view.

The C++ Standard newer than C++98 states that vector memory is contiguous.
E.g. see c++ - Is it safe to assume that STL vector storage is always contiguous? - Stack Overflow[^] and vector - C++ Reference[^].
You could do something like this:
C++
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);
        ...
    }
};
Cheers
Andi
 
Share this answer
 
Comments
CPallini 28-Jan-16 15:58pm    
5.
Andreas Gieriet 28-Jan-16 17:11pm    
Thanks for your 5!
Cheers
Andi
pkfox 28-Jan-16 18:57pm    
Didn't realise I'd given you a five ;-) I'm new to this game - thanks a lot
Andreas Gieriet 28-Jan-16 19:03pm    
Thanks for your 5.
Cheers
Andi
Albert Holguin 28-Jan-16 19:39pm    
+4.... this really doesn't buy you anything... for that matter, why a std::vector and not a std::array? the socket recvfrom() call isn't going to change size, no point in using a vector.
Why would you change something that works? UDP socket calls are relatively simple, keep it that way...

If anything, I'd make sure that call was asynchronous and give it a timeout period in case you never get a response (otherwise, you just have a call that can hang your program).
 
Share this answer
 
Comments
pkfox 28-Jan-16 18:59pm    
Hi there , I'm not going to change it I was just being curious - thanks for your time
Albert Holguin 28-Jan-16 20:12pm    
curiosity is a great way to learn... keep it up
Albert Holguin 28-Jan-16 20:17pm    
FYI... sizeof() gives you the size of whatever you're passing to it, if you pass an char[] to it, it'll give you the size of the address, not the size of the buffer... looks like you're using it wrong... also, you shouldn't have to specify "this" all the time if you're already within the object, makes the code unnecessarily verbose... if you're making calls within a class, "this" is implied.
pkfox 29-Jan-16 10:48am    
Hi Albert what should I use instead of sizeof ?
Albert Holguin 29-Jan-16 14:59pm    
The actual size of the array that you're using. If you're using a raw char array, you should know the size since you allocated it... if you're using a container (say vector, even though I don't think it's applicable to use a vector for this) then use whatever size() function the container has.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900