Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
andrewauclair opened this issue Apr 22, 2024 · 3 comments
Labels

Comments

@andrewauclair
Copy link
Contributor

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
Copy link
Contributor Author

andrewauclair commented Apr 23, 2024

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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

@andrewauclair andrewauclair changed the title Socket::poll returns true after sending to local UDP socket that is not connected Socket::available Causes Connection Reset by Peer on Windows UDP Sockets Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant