本文整理汇总了C++中StreamSocket::poll方法的典型用法代码示例。如果您正苦于以下问题:C++ StreamSocket::poll方法的具体用法?C++ StreamSocket::poll怎么用?C++ StreamSocket::poll使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StreamSocket
的用法示例。
在下文中一共展示了StreamSocket::poll方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testSelect
void SocketTest::testSelect()
{
Timespan timeout(250000);
EchoServer echoServer;
StreamSocket ss;
ss.connect(SocketAddress("localhost", echoServer.port()));
Socket::SocketList readList;
Socket::SocketList writeList;
Socket::SocketList exceptList;
readList.push_back(ss);
assert (Socket::select(readList, writeList, exceptList, timeout) == 0);
assert (readList.empty());
assert (writeList.empty());
assert (exceptList.empty());
ss.sendBytes("hello", 5);
ss.poll(timeout, Socket::SELECT_READ);
readList.push_back(ss);
writeList.push_back(ss);
assert (Socket::select(readList, writeList, exceptList, timeout) == 2);
assert (!readList.empty());
assert (!writeList.empty());
assert (exceptList.empty());
char buffer[256];
int n = ss.receiveBytes(buffer, sizeof(buffer));
assert (n == 5);
assert (std::string(buffer, n) == "hello");
ss.close();
}
示例2: testNonBlocking
void SocketTest::testNonBlocking()
{
EchoServer echoServer;
StreamSocket ss;
ss.connect(SocketAddress("localhost", echoServer.port()));
ss.setBlocking(false);
Timespan timeout(1000000);
assert (ss.poll(timeout, Socket::SELECT_WRITE));
int n = ss.sendBytes("hello", 5);
assert (n == 5);
char buffer[256];
assert (ss.poll(timeout, Socket::SELECT_READ));
n = ss.receiveBytes(buffer, sizeof(buffer));
assert (n == 5);
assert (std::string(buffer, n) == "hello");
ss.close();
}
示例3: testPoll
void SocketTest::testPoll()
{
EchoServer echoServer;
StreamSocket ss;
ss.connect(SocketAddress("localhost", echoServer.port()));
Stopwatch sw;
sw.start();
Timespan timeout(1000000);
assert (!ss.poll(timeout, Socket::SELECT_READ));
assert (sw.elapsed() >= 900000);
sw.restart();
assert (ss.poll(timeout, Socket::SELECT_WRITE));
assert (sw.elapsed() < 100000);
ss.sendBytes("hello", 5);
char buffer[256];
sw.restart();
assert (ss.poll(timeout, Socket::SELECT_READ));
assert (sw.elapsed() < 100000);
int n = ss.receiveBytes(buffer, sizeof(buffer));
assert (n == 5);
assert (std::string(buffer, n) == "hello");
ss.close();
}