Skip to content

UDP socket cannot be disconnected with Connect(IPAddress.Any, 0) #77962

@wasabii

Description

@wasabii

Description

It is fairly common (at least among those apps that do a lot of low level UDP stuff) to disconnect a UDP socket (revert SendTo target) by sending socket.EndConnect(socket.BeginConnect(new IPEndPoint(IPAddress.IPv6Any, 0), null, null)); This code works perfectly fine in Framework. It mirrors the same type of idea in BSD sockets and Winsock where you can simply disconnect the socket by connecting it to a AF_UNSPEC. Which is a constant to 0. Which for whatever reason .NET has been happy with IPAddress.Any.

It's also the documented operation on Linux, BSD, etc, for disconnecting a UDP socket (minus the weird .NET-async requirement).

Connectionless sockets may dissolve the association by connecting to an address with the sa_family member of sockaddr set to AF_UNSPEC (supported on Linux since kernel 2.2).

However, this doesn't appear to work in Core 6, as far as I can tell, because Core 6 has a check for IsConnected at the top of ConnectAsync (called from BeginConnect). Framework has no similar check (if reference source is to be believed).

I can't find a work around for this. Disconnect won't work, since that has a similar problem with isDisconnected.

Is this a bug? Or an undocumented change to the way Socket.Connect is suppsoed to function in Core? If so, how are you to disconnect a UDP socket but preserve it otherwise for reconnection?

Reproduction Steps

Create a DGRAM Socket. Call Connect(). Call connect a second time, with IPAddress.IPAny/0.

Expected behavior

I'd expect it to work like Framework, and BSD sockets, and WinSock.

Actual behavior

A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied.

(SocketException with SocketError of NotConnected).

Regression?

Yes! Worked in Framework.

Known Workarounds

I wish.

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions