• Advertisement
Sign in to follow this  

receive buffer

This topic is 3628 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi a common sockets principle is the receive buffer. Does a socket wrapper have one of these I cannot find it or does anyone know of a library which has one and the functions that allows you to pop information off and do run length? Example: POP3 C Read data on socket S +OK POP3 server ready C LIST S +OK 2 messages (320 octets) S 1 120 S 2 342 S . C RETR 1 S +OK 120 octets S datadatadata... S . because tcp can arrive fragmented, you would have to receive this into a buffer, and when it reaches the \r\n.\r\n you would pop off the buffer. when you send the RETR 1, you would pop off the first \r\n which would be +OK 120 octects. Then you would set the receive buffer to run length for 120 bytes and then continue. I wrote a receive buffer a long time ago that did this but I lost it and do not want to write it again because it took me like a week and is a lot of pointer arithmetic stuff. How can you do this with a library I cannot find one so far.

Share this post


Link to post
Share on other sites
Advertisement
hmm doesnt look like what im looking for doesnt have a feature that lets you pop off an item based on a delimeter.

Share this post


Link to post
Share on other sites
It's just not that hard to write that kind of class. Something like the following:


class buffer : public std::vector<char> {
public:
int receive_from_socket(int socket, size_t max_size) {
size_t s = size();
resize(s + max_size);
int r = ::recv(socket, &(*this)[s], max_size, 0);
resize(s + (r < 0) ? 0 : r);
return r;
}
bool get_str_delimited(std::string &out, std::string const &delim) {
if (size() == 0) return false;
if (delim.length() == 0) return false;
push_back(0);
char *str = &(*this)[0];
char *pos = strstr(str, delim.c_str());
pop_back();
if (pos == 0) return false;
out = std::string(str, pos);
erase(begin(), begin() + (pos - str) + delim.length());
return true;
}
};


With the proper use of existing classes, a simple buffer is a matter of minutes, not days, to implement, and only slightly more than that to test and verify.

Note: I haven't tested it, but it should be within inches of working.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement