mirror of
https://github.com/pocoproject/poco.git
synced 2025-11-03 00:23:24 +01:00
fix(DatagramSocket): Socket::available does not always return correct value for UDP #3589
This commit is contained in:
@@ -610,6 +610,13 @@ int SocketImpl::available()
|
||||
{
|
||||
int result = 0;
|
||||
ioctl(FIONREAD, result);
|
||||
#if (POCO_OS != POCO_OS_LINUX)
|
||||
if (type() == SOCKET_TYPE_DATAGRAM)
|
||||
{
|
||||
char buf[result];
|
||||
result = recvfrom(sockfd(), buf, result, MSG_PEEK, NULL, NULL);
|
||||
}
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "Poco/Timespan.h"
|
||||
#include "Poco/Buffer.h"
|
||||
#include "Poco/Stopwatch.h"
|
||||
#include "Poco/Thread.h"
|
||||
#include <cstring>
|
||||
|
||||
|
||||
@@ -34,6 +35,7 @@ using Poco::Stopwatch;
|
||||
using Poco::TimeoutException;
|
||||
using Poco::InvalidArgumentException;
|
||||
using Poco::IOException;
|
||||
using Poco::Thread;
|
||||
|
||||
|
||||
DatagramSocketTest::DatagramSocketTest(const std::string& name): CppUnit::TestCase(name)
|
||||
@@ -120,6 +122,25 @@ void DatagramSocketTest::testEchoBuffer()
|
||||
}
|
||||
|
||||
|
||||
void DatagramSocketTest::testReceiveFromAvailable()
|
||||
{
|
||||
UDPEchoServer echoServer(SocketAddress("127.0.0.1", 0));
|
||||
DatagramSocket ss(SocketAddress::IPv4);
|
||||
int n = ss.sendTo("hello", 5, SocketAddress("127.0.0.1", echoServer.port()));
|
||||
assertTrue (n == 5);
|
||||
Thread::sleep(100);
|
||||
char buffer[256];
|
||||
SocketAddress sa;
|
||||
assertTrue (ss.available() == 5);
|
||||
n = ss.receiveFrom(buffer, sizeof(buffer), sa);
|
||||
assertTrue (sa.host() == echoServer.address().host());
|
||||
assertTrue (sa.port() == echoServer.port());
|
||||
assertTrue (n == 5);
|
||||
assertTrue (std::string(buffer, n) == "hello");
|
||||
ss.close();
|
||||
}
|
||||
|
||||
|
||||
void DatagramSocketTest::testSendToReceiveFrom()
|
||||
{
|
||||
UDPEchoServer echoServer(SocketAddress("127.0.0.1", 0));
|
||||
@@ -800,6 +821,7 @@ CppUnit::Test* DatagramSocketTest::suite()
|
||||
CppUnit_addTest(pSuite, DatagramSocketTest, testEcho);
|
||||
CppUnit_addTest(pSuite, DatagramSocketTest, testMoveDatagramSocket);
|
||||
CppUnit_addTest(pSuite, DatagramSocketTest, testEchoBuffer);
|
||||
CppUnit_addTest(pSuite, DatagramSocketTest, testReceiveFromAvailable);
|
||||
CppUnit_addTest(pSuite, DatagramSocketTest, testSendToReceiveFrom);
|
||||
CppUnit_addTest(pSuite, DatagramSocketTest, testUnbound);
|
||||
CppUnit_addTest(pSuite, DatagramSocketTest, testReuseAddressPortWildcard);
|
||||
|
||||
@@ -28,6 +28,7 @@ public:
|
||||
void testEcho();
|
||||
void testMoveDatagramSocket();
|
||||
void testEchoBuffer();
|
||||
void testReceiveFromAvailable();
|
||||
void testSendToReceiveFrom();
|
||||
void testUnbound();
|
||||
void testReuseAddressPortWildcard();
|
||||
|
||||
Reference in New Issue
Block a user