Skip to content

Socket LocalEndPoint not updated in SendToAsync(SocketAsyncEventArgs e) #915

@funkynicco

Description

@funkynicco

The SocketAsyncEventArgs version of SendToAsync does not properly set _rightEndPoint, while it creates the endPointSnapshot variable that is used to set _rightEndPoint in SendTo, BeginSendTo and SendToAsync (task) methods.

According to remarks section of the documentation on recvfrom method, all of sendto, WSASendTo and WSAJoinLeaf implicitly binds to a local address which is reflected by calling getsockname.

Here is a reference link for Socket.cs with line number.
https://github.com/dotnet/corefx/blob/master/src/System.Net.Sockets/src/System/Net/Sockets/Socket.cs#L4213
Otherwise it is in public bool SendToAsync(SocketAsyncEventArgs e) method.

Example code for reproducing issue

static void Main(string[] args)
{
    using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
    {
        var destinationEndPoint = new IPEndPoint(IPAddress.Parse("10.20.30.40"), 1234);

        var socketAsyncEventArgs = new SocketAsyncEventArgs();
        socketAsyncEventArgs.RemoteEndPoint = destinationEndPoint;
        socketAsyncEventArgs.SetBuffer(new byte[32], 0, 32); // 32 bytes of zeros
        socketAsyncEventArgs.Completed += (sender, e) => SendCompleted(e);

        if (!socket.SendToAsync(socketAsyncEventArgs))
            SendCompleted(socketAsyncEventArgs);

        Debug.Assert(socket.LocalEndPoint != null); // LocalEndPoint should not be null
    }

    Console.ReadLine();
}

static void SendCompleted(SocketAsyncEventArgs args)
{
    args.Dispose();
}

Metadata

Metadata

Assignees

Labels

area-System.Net.Socketsbreaking-changeIssue or PR that represents a breaking API or functional change over a previous release.bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions