poco icon indicating copy to clipboard operation
poco copied to clipboard

Socket::available Causes Connection Reset by Peer on Windows UDP Sockets

Open andrewauclair opened this issue 1 year ago • 5 comments

Describe the bug Created a DatagramSocket with local address 127.0.0.1:0 and remote address 127.0.0.1:2345. After calling sendBytes on this UDP socket, poll returns true, then available returns -1 and the last error is "connection reset by peer". I suspect this is from the recvfrom call within available. If I change this to use the IP of a remote machine as the remote address, poll properly times out and returns false after sendBytes.

To Reproduce

#include <iostream>
void DatagramSocketTest::testLocalIPIssue()
{
	DatagramSocket socket(SocketAddress("127.0.0.1", 0), true, true);

	socket.connect(SocketAddress("127.0.0.1", 2345));

	unsigned char values[5]{};
	socket.sendBytes(values, 5);

	Poco::Timespan timeout(5000);

	if (socket.poll(timeout, Socket::SELECT_READ))
	{
		std::cout << socket.available(); // -1 and last error is "connection reset by peer"
	}
}

Expected behavior Poll returns false, just like it would when local and remote IPs don't match.

Please add relevant environment information:

  • Windows 10
  • POCO 1.12.5

andrewauclair avatar Apr 22 '24 13:04 andrewauclair

Should we be using Poco::Net::UDPServer and Poco::Net::UDPClient instead of a Poco::Net::DatagramSocket directly to avoid issues like this?

Maybe not. Looks like that only supports connecting locally.

andrewauclair avatar Apr 23 '24 23:04 andrewauclair

I ended up closing and reopening the socket if there was an error after calling available(). It still doesn't seem that great to me that available() could cause an error and return -1.

andrewauclair avatar Apr 24 '24 17:04 andrewauclair

Some additional testing resulted in receiveBytes throwing a std::bad_array_new_length because available returned -1.

andrewauclair avatar Apr 24 '24 17:04 andrewauclair