diff --git a/Net/src/SocketImpl.cpp b/Net/src/SocketImpl.cpp index 34cad7d074..7c352060d7 100644 --- a/Net/src/SocketImpl.cpp +++ b/Net/src/SocketImpl.cpp @@ -653,6 +653,8 @@ int SocketImpl::available() { std::vector buf(result); result = recvfrom(sockfd(), &buf[0], result, MSG_PEEK, nullptr, nullptr); + + if (result < 0) error(); } #endif return result; diff --git a/Net/testsuite/src/DatagramSocketTest.cpp b/Net/testsuite/src/DatagramSocketTest.cpp index e765de29cc..1241599903 100644 --- a/Net/testsuite/src/DatagramSocketTest.cpp +++ b/Net/testsuite/src/DatagramSocketTest.cpp @@ -804,6 +804,32 @@ void DatagramSocketTest::testGatherScatterVariableUNIX() } +void DatagramSocketTest::testLocalUDPConnectionResetWin() +{ +#if defined(POCO_OS_FAMILY_WINDOWS) + // create a local UDP socket and connect to another local port which doesn't have a UDP socket + DatagramSocket socket(SocketAddress("127.0.0.1", 0), true, true); + + socket.connect(SocketAddress("127.0.0.1", 2345)); + + // send some data to the socket + unsigned char values[5]{}; + socket.sendBytes(values, 5); + + try + { + // available calls recvfrom which can cause a "Connection Reset by Peer" error for UDP sockets in Windows due to ICMP packets for remote destination not existing + socket.available(); + + fail(); + } + catch (const Poco::Net::ConnectionResetException&) + { + } +#endif +} + + void DatagramSocketTest::setUp() { } @@ -832,5 +858,7 @@ CppUnit::Test* DatagramSocketTest::suite() CppUnit_addTest(pSuite, DatagramSocketTest, testGatherScatterFixed); CppUnit_addTest(pSuite, DatagramSocketTest, testGatherScatterVariable); + CppUnit_addTest(pSuite, DatagramSocketTest, testLocalUDPConnectionResetWin); + return pSuite; } diff --git a/Net/testsuite/src/DatagramSocketTest.h b/Net/testsuite/src/DatagramSocketTest.h index 10bb5e2d83..ea2c13cde2 100644 --- a/Net/testsuite/src/DatagramSocketTest.h +++ b/Net/testsuite/src/DatagramSocketTest.h @@ -55,6 +55,8 @@ class DatagramSocketTest: public CppUnit::TestCase void testGatherScatterSTRFFixedUNIX(); void testGatherScatterVariableUNIX(); void testGatherScatterSTRFVariableUNIX(); + + void testLocalUDPConnectionResetWin(); };