Socket.BeginReceiveFrom gives multiple callbacks



I'm trying to write a UDP client that receives broadcast messages. I have a
server broadcasting at UDP port 12345 and the client is listening for
broadcasts at that port.

Initially, it seems as if everything works, but a closer look reveals that I
get 2 strange problems:

1. For each individual server broadcast, I get the MessageReceiveCallback
function called 3 times (network trafic investigated with a sniffer to
certify that is is a single UDP packet). Each time I get the same complete
UDP packet in the callback function. The deserialized SOAP information is
correctly formatted and everything seems to be OK.
2. After 2-3 broadcasts (i.e. 6-9 calls to the callback function), I get an
"InvalidOperationException" in the deserialize function. As far as I can
tell, all involved objects are still holding valid content, so I really don't
understand what the problem is.

I presume thatt the root cause of my problem is the multiple activation of
the callback function. Can anyone tell me why this happens?

Code:


public static partial class ClientService
{

public static Socket workSocket;
public static EndPoint bindEndPoint;
public static byte[] buffer = new byte[2000];


public static void InitializeUDPReceiver()
{

workSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
bindEndPoint = new IPEndPoint(IPAddress.Any, 12345);
workSocket.Bind(bindEndPoint);
workSocket.BeginReceiveFrom(buffer, 0, buffer.Length,
SocketFlags.None, ref bindEndPoint, new
AsyncCallback(MessageReceivedCallback), workSocket);
}



public static void MessageReceivedCallback(IAsyncResult result)
{
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint remoteEndPoint = (EndPoint)sender;

int bytesRead = workSocket.EndReceiveFrom(result, ref
remoteEndPoint);

XmlTypeMapping myTypeMapping = (new
SoapReflectionImporter().ImportTypeMapping(typeof(SOAPData)));
XmlSerializer mySerializer = new XmlSerializer(myTypeMapping);
MemoryStream s = new MemoryStream(buffer);

SOAPData PublishedData = (SOAPData)mySerializer.Deserialize(s);


workSocket.BeginReceiveFrom(buffer, 0, buffer.Length,
SocketFlags.None, ref bindEndPoint, new
AsyncCallback(MessageReceivedCallback), workSocket);

}



public static void StopUDPReceiver()
{
workSocket.Close();
}

}

.



Relevant Pages

  • Re: Problem with certificates/L2TP VPN
    ... Do you have Kerberos (port 88 on UDP & TCP) open? ... of RRAS server. ... The client IS behind NAT. ... UDP 500 - for IKE ...
    (microsoft.public.windows.server.networking)
  • Re: What is the minimum-size UDP packet?
    ... > with the server but for scalability reasons there is not a one to one map ... A client may disconnect ... If the server hasn't received a valid session ID via UDP from the ... Connectionless - there is no connection. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: What is the minimum-size UDP packet?
    ... > with the server but for scalability reasons there is not a one to one map ... A client may disconnect ... If the server hasn't received a valid session ID via UDP from the ... Connectionless - there is no connection. ...
    (microsoft.public.win2000.networking)
  • Re: UDP client/server problem: please help!
    ... the client shall not communicate anything to the server which shall ... When the client receives data, ... create a ListenSocket socket ... Listen only applies to TCP/IP, not UDP, and is therefore inappropriate. ...
    (microsoft.public.vc.mfc)
  • Re: tunneling through hotspot firewall
    ... By default a firewall is ... As soon as the client logs out again his IP will be removed ... UDP and everything else from being both recieved or send. ... Or if not on which layer would it fail. ...
    (Pen-Test)

Loading