dotnet / dotnet/dotnet-api-docs

Connect() doesn't seem to direct the following Send() to the new host

Open
#4,578 5 comments 0 reactions 0 assignees View on GitHub
area-System.Net Pri3 untriaged
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

https://github.com/dotnet/dotnet-api-docs/blob/ddd279dab83d7f447eb4675963c15a5a7455e19e/xml/System.Net.Sockets/UdpClient.xml#L1097

The statement in this line does not seem to be correct.

In my tests, I had to call ```.Dispose()``` then reinstantiate in order to connect to another host, i.e. if I have the following code
```
private UdpClient _UdpClient = new UdpClient(8000);

public void Send(string ip, int port, byte[] data)
{
_UdpClient.Dispose();
_UdpClient = new UdpClient(_Port);
_UdpClient.Connect(ip, port);
_UdpClient.Send(data, data.Length);
}
```
This works really well.

If I leave out ```_UdpClient.Dispose();``` and ```_UdpClient = new UdpClient(_Port);```, reusing the previous instance, it does not work. It continues to send to the host previously connected.

I've had to avoid using ```Connect()``` altogether and instead just use ```Send``` with a signature including the ```IPEndPoint``` of the recipient.
```
private UdpClient _UdpClient = new UdpClient(8000);

public void Send(string ip, int port, byte[] data)
{
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(ip), port);
_UdpClient.Send(data, data.Length, ipe);
}
```

It may be useful for someone to investigate whether the following statement from the documentation is true:
```
If you want to send datagrams to a different remote host, you must make another call to the Connect method or create another UdpClient without a default remote host.
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.