Open
Description
This code is copied from UDPClient, The decision made here is really ODD.
Aparently,
tempRemoteEP has nothing to do with remoteEP, which is a bug here, client will receive message from all EP instead of specified EP.
Then, simply because you didn't return a length, you decide to make a copy of the buffer, then your buffer is a new buffer, which would cause performance issue, and the worse, sometimes it's the same buffer you always use. If I assume it's always a new buffer, it will create bugs!
public byte[] Receive([NotNull] ref IPEndPoint? remoteEP)
{
ThrowIfDisposed();
// this is a fix due to the nature of the ReceiveFrom() call and the
// ref parameter convention, we need to cast an IPEndPoint to it's base
// class EndPoint and cast it back down to IPEndPoint. ugly but it works.
EndPoint tempRemoteEP = _family == AddressFamily.InterNetwork ?
IPEndPointStatics.Any :
IPEndPointStatics.IPv6Any;
int received = Client.ReceiveFrom(_buffer, MaxUDPSize, 0, ref tempRemoteEP);
remoteEP = (IPEndPoint)tempRemoteEP;
// because we don't return the actual length, we need to ensure the returned buffer
// has the appropriate length.
if (received < MaxUDPSize)
{
byte[] newBuffer = new byte[received];
Buffer.BlockCopy(_buffer, 0, newBuffer, 0, received);
return newBuffer;
}
return _buffer;
}
Activity