4

I was wondering if there is a good implementation (library) of a C++ iterator facade around sockets. I've gone through the Boost Iterator library and ASIO, and can't seem to find anything. An open source solution would be great!

I'm looking for a solution to the following use-case:

int socket_handler = 0;

socket_iterator it(socket_handler);
socket_iterator end;

//read mode 1:
while (it != end)
{
  char c = *it;
   .
   .
  ++it;
}

//read mode 2:
while (it != end)
{
  std::string s = *it;
   .
   .
  ++it;
}

//write mode 1:
unsigned char c = 0;
while (c < 100)
{
  *it = c++;
  .
  .
  ++it;
}

//write mode 2:
std::sttring s = "abc";
for (unsigned int i = 0; i < 10; ++i)
{
  *it = s;
   .
   .
  ++it;
}

Note: it == end, when the connection is disconnected.

4

1 回答 1

2

@Gerdiner,Boost.Asio 是赢家。关于您的 istream_iterator,请查看以下内容:

boost::asio::streambuf myBuffer;
std::string myString;

// Convert streambuf to std::string
std::istream(&myBuffer) >> myString;

但是,使用 ASIO,您将不需要迭代器。请参阅以下异步客户端作为起点。

异步 HTTP 客户端

于 2011-05-24T20:15:10.790 回答