UDP socket problem

T

Thomas Lerchner

Hi!

I have a problem with an UDP socket.
I create socket and call BeginReceiveFrom to use the socket async.
But the callback methode is being called several times with the same packet
from the same sender.
It has been confirmed that the data was sent only once.
I used a sniffer to debug that.
And it does not matter if the data is broadcasted or sent to specific
address.
What am I doing wrong?
Is it legal to call the BeginReceiveFrom methode in the callback methode?
Is this a common issue in Winsock?

This codes generates the socket and starts the reading:

this._recvBuffer = new byte[ 64 ];
this._remoteEndPoint = new IPEndPoint ( IPAddress.None, 0 );

// Create the broadcast socket
this._udpSocket = new Socket ( AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp );
this._udpSocket.Bind ( new IPEndPoint ( IPAddress.Any, this._cfg.Port ) );
this._udpSocket.SetSocketOption ( SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1 );
this._udpSocket.BeginReceiveFrom ( this._recvBuffer, 0,
this._recvBuffer.Length, SocketFlags.None, ref this._remoteEndPoint, new
AsyncCallback ( this.OnReceiveFrom ), null );


The OnReceiveFrom methode:

private void OnReceiveFrom ( IAsyncResult ar )
{
try
{
// Check if the socket still exists
if ( this._udpSocket != null )
{
// Lock the socket for only one thread
lock ( this._udpSocket )
{
// End the async reading of the UDP socket
int recvBufferSize = this._udpSocket.EndReceiveFrom ( ar,
ref this._remoteEndPoint );

// Check the received bytes ....

} // lock ( this._udpSocket )

} // if ( this._udpSocket != null )
} // try
// Catch the exception when we access the server socket after it has
been disposed
catch ( ObjectDisposedException )
{ this._udpSocket = null; }
finally
{
// Check if the socket still exists
if ( this._udpSocket != null )
{
// Lock the socket for only one thread
lock ( this._udpSocket )
{
// Begin again receiving the data from a client
this._udpSocket.BeginReceiveFrom ( this._recvBuffer, 0,
this._recvBuffer.Length, SocketFlags.None, ref this._remoteEndPoint, new
AsyncCallback ( this.OnReceiveFrom ), null );

} // lock ( this._udpSocket )
} // if ( this._udpSocket != null )
} // finally
} // OnReceiveFrom

Thanks alot for any comments and ideas.

Thomas
 
R

Robert Jordan

Hi Thomas,
I have a problem with an UDP socket.
I create socket and call BeginReceiveFrom to use the socket async.
But the callback methode is being called several times with the same packet
from the same sender.
It has been confirmed that the data was sent only once.
I used a sniffer to debug that.
And it does not matter if the data is broadcasted or sent to specific
address.
What am I doing wrong?
Is it legal to call the BeginReceiveFrom methode in the callback methode?
Is this a common issue in Winsock?

I don't know where the problem is, but your design has a capital
flaw: it reenters the ThreadPool while holding the lock.

bye
Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top