コード例 #1
0
void LocalSocketTest::doSelectOrPoll2(SelectPtr pAction)
{
	Timespan timeout(100000);

	SocketAddress sas1("/tmp/poco.server1.tcp.sock");
	EchoServer echoServer1(sas1);
	SocketAddress sac1("/tmp/poco.client1.tcp.sock");
	StreamSocket ss1(sas1, &sac1);

	SocketAddress sas2("/tmp/poco.server2.tcp.sock");
	EchoServer echoServer2(sas2);
	SocketAddress sac2("/tmp/poco.client2.tcp.sock");
	StreamSocket ss2(sas2, &sac2);
	
	Socket::SocketList readList;
	Socket::SocketList writeList;
	Socket::SocketList exceptList;

	readList.push_back(ss1);
	readList.push_back(ss2);
	assert (pAction(readList, writeList, exceptList, timeout) == 0);
	assert (readList.empty());
	assert (writeList.empty());
	assert (exceptList.empty());
	
	ss1.sendBytes("hello", 5);

	ss1.poll(timeout, Socket::SELECT_READ);

	readList.push_back(ss1);
	readList.push_back(ss2);
	assert (pAction(readList, writeList, exceptList, timeout) == 1);

	assert (readList.size() == 1);
	assert (readList[0] == ss1);
	assert (writeList.empty());
	assert (exceptList.empty());

	char buffer[256];
	int n = ss1.receiveBytes(buffer, sizeof(buffer));
	assert (n == 5);

	readList.clear();
	writeList.clear();
	exceptList.clear();
	writeList.push_back(ss1);
	writeList.push_back(ss2);
	assert (pAction(readList, writeList, exceptList, timeout) == 2);
	assert (readList.empty());
	assert (writeList.size() == 2);
	assert (writeList[0] == ss1);
	assert (writeList[1] == ss2);
	assert (exceptList.empty());

	ss1.close();
	ss2.close();
}
コード例 #2
0
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();
}
コード例 #3
0
void SocketTest::testSelect2()
{
	Timespan timeout(100000);

	EchoServer echoServer1;
	EchoServer echoServer2;
	StreamSocket ss1(SocketAddress("localhost", echoServer1.port()));
	StreamSocket ss2(SocketAddress("localhost", echoServer2.port()));
	
	Socket::SocketList readList;
	Socket::SocketList writeList;
	Socket::SocketList exceptList;

	readList.push_back(ss1);
	readList.push_back(ss2);
	assert (Socket::select(readList, writeList, exceptList, timeout) == 0);
	assert (readList.empty());
	assert (writeList.empty());
	assert (exceptList.empty());
	
	ss1.sendBytes("hello", 5);

	ss1.poll(timeout, Socket::SELECT_READ);

	readList.push_back(ss1);
	readList.push_back(ss2);
	assert (Socket::select(readList, writeList, exceptList, timeout) == 1);

	assert (readList.size() == 1);
	assert (readList[0] == ss1);
	assert (writeList.empty());
	assert (exceptList.empty());

	char buffer[256];
	int n = ss1.receiveBytes(buffer, sizeof(buffer));
	assert (n == 5);

	readList.clear();
	writeList.clear();
	exceptList.clear();
	writeList.push_back(ss1);
	writeList.push_back(ss2);
	assert (Socket::select(readList, writeList, exceptList, timeout) == 2);
	assert (readList.empty());
	assert (writeList.size() == 2);
	assert (writeList[0] == ss1);
	assert (writeList[1] == ss2);
	assert (exceptList.empty());

	ss1.close();
	ss2.close();
}
コード例 #4
0
void LocalSocketTest::doSelectOrPoll1(SelectPtr pAction)
{
	Timespan timeout(250000);

	SocketAddress sas("/tmp/poco.server.tcp.sock");
	EchoServer echoServer(sas);
	SocketAddress sac("/tmp/poco.client.tcp.sock");
	StreamSocket ss(sas, &sac);

	Socket::SocketList readList;
	Socket::SocketList writeList;
	Socket::SocketList exceptList;

	readList.push_back(ss);
	assert (pAction(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 (pAction(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();
}