Skip to content

Socket.SendFile() should fail with a consistent exception for connectionless sockets #47472

Closed
@antonfirsov

Description

@antonfirsov

Socket.SendFile() is not supported on Windows, but the exceptions being thrown are not consistent. The first call throws SocketException, while the second attempt leads to NotSupportedException.

Repro

The twice=true case fails:

[Theory]
[InlineData(false)]
[InlineData(true)]
public void UDP_SendFile(bool twice)
{
    string tempFile = Path.GetTempFileName();
    File.WriteAllBytes(tempFile, new byte[128]);

    using var client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    using var listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    listener.BindToAnonymousPort(IPAddress.Loopback);

    client.Connect(listener.LocalEndPoint);

    Assert.Throws<SocketException>(() => client.SendFile(tempFile));
    if (twice)
    {
        Assert.Throws<SocketException>(() => client.SendFile(tempFile)); // Throws NotSupportedException
    }
}

Edit (after discussion below)

On Linux the call does not fail. We should fail NotSupportedException for connectionless sockets on all OS-es in all cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Net.Socketsbuggood first issueIssue should be easy to implement, good for first-time contributorshelp wanted[up-for-grabs] Good issue for external contributors

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions