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.ConnectAsync result get the socket error "InvalidArgument" for UDP #39268

Closed
iozcelik opened this issue Jul 14, 2020 · 6 comments
Closed

Comments

@iozcelik
Copy link

I use .net core 3.1 and creating a worker service.

I try to implement Socket.ConnectAsync both TCP and UDP. However UDP is not working as expected.

From documentation, it will be possibble. However, I am not successed.

If you are using a connection-oriented protocol, the ConnectAsync method starts an asynchronous request for a connection to the remote host. If you are using a connectionless protocol, ConnectAsync establishes a default remote host.

My code is simple:

var socket = new Socket(Endpoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);

var _connectEventArg = new SocketAsyncEventArgs();
_connectEventArg.RemoteEndPoint = Endpoint;
_connectEventArg.Completed += OnAsyncCompleted;

socket.ConnectAsync(_connectEventArg);
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Net.Sockets untriaged New issue has not been triaged by the area owner labels Jul 14, 2020
@ghost
Copy link

ghost commented Jul 14, 2020

Tagging subscribers to this area: @dotnet/ncl
Notify danmosemsft if you want to be subscribed.

@antonfirsov
Copy link
Member

antonfirsov commented Jul 14, 2020

However UDP is not working as expected.

Can you elaborate on this? Do you get an error? If not, what is the behavior you find unexpected?
Your code looks fine. What is the value of Endpoint, where does it come from?

@iozcelik
Copy link
Author

iozcelik commented Jul 14, 2020

I add some screenshoot. And try to what I expect. First of all, I expect ConnectAsync must return true but it return false. Why?
From documentation:

true if the I/O operation is pending. The Completed event on the e parameter will be raised upon completion of the operation.

false if the I/O operation completed synchronously. In this case, The Completed event on the e parameter will not be raised and the e object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation.

Secondly, _connectEventArg.SocketError give InvalidArgument error. Which given argument is invalid? I have only one endpoint argument.

image

Example code is here:

static void Main(string[] args) { 
       string address = "172.21.48.1";
       int port = 5858;
       var endpoint = new IPEndPoint(IPAddress.Parse(address), port);

       var socket = new Socket(endpoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);

       var _connectEventArg = new SocketAsyncEventArgs();
       _connectEventArg.RemoteEndPoint = endpoint;
       _connectEventArg.Completed += OnAsyncCompleted;

       var status = socket.ConnectAsync(_connectEventArg);
}

private static void OnAsyncCompleted(object sender, SocketAsyncEventArgs e) {
       Console.WriteLine(e.SocketError);
}

@wfurt
Copy link
Member

wfurt commented Jul 14, 2020

This should be fixed by #33674.
Use 5.0 preview if you can @iozcelik
(You did not specify but I assume you are on Windows, right?)

@iozcelik
Copy link
Author

Yes, working on Windows. I cant use .net 5 but thanks.

@wfurt
Copy link
Member

wfurt commented Jul 14, 2020

You should be able to use socket.ConnectAsync(endpoint);

@wfurt wfurt closed this as completed Jul 14, 2020
@karelz karelz added this to the 5.0.0 milestone Aug 18, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 2020
@karelz karelz removed the untriaged New issue has not been triaged by the area owner label Oct 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants