[Mono-list] UDP Broadcast Socket.ReceiveFrom Anomalies

Matthew Franz mdfranz@io.com
Wed, 24 Mar 2004 23:37:14 -0600


I'm writing (actually trying to simulate) a simple UDP client and I've noticed some anomalies between Mono 0.31 and .NET in terms of
the handling the messages. I'm assuming because the replies from the server are broadcasts.

1) C: 1.1.1.1 -> 1.1.1.2 (or 1.1.1.255) S:
2) S: 1.1.1.2 -> 1.1.1.255 C:

With mono, the client only gets the data from the server if the message 1 is sent to the server's broadcast address. The client
ignores the data if it was sent to the server's unicast address. With .NET it receives the data regardless of the destination address 
of message 1. Whether or not the socket option is set to broadcast doesn't make any difference.

In both cases the server is responding the same (verified with tcpdump) and there isn't anything weird (like non-Ethernet II frames)  
at the datalink layer. There is also a discrepancy between the value returned by s.Available. Mono returns just the length of the
next message but .NET returns the length of all the responses.

And I've seen http://bugzilla.ximian.com/show_bug.cgi?id=45959

Any ideas? I'm assuming I made a simple mistake, and .NET is more forgiving?

= mdf

================================================================

IPEndPoint ep = new IPEndPoint(ip,dport);  //remote
IPEndPoint lep = new IPEndPoint(IPAddress.Any,sport); //local
		
Socket s = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);

if (dstbcast) {
	s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast,1);
}

s.Bind(lep);
		
try {
	s.SendTo(Message1, ep);
}
catch (Exception e) {
	Console.WriteLine(e.ToString());
}

byte[] rBuffer = new byte[1500];

IPEndPoint sender = new IPEndPoint(IPAddress.Any,0);
EndPoint target = (EndPoint)sender;

System.Console.WriteLine("{0} bytes in the buffer",s.Available);

while (s.Available > 0) {

	int recv_bytes = s.ReceiveFrom(rBuffer, ref target);
			
	// For some reason we are getting our own message with ReceiveFrom
	// and this doesn't work for multihomed
			
	if (target == sender) {
		continue;
	}

	System.Console.Write ("{0} bytes ",recv_bytes);
	System.Console.WriteLine("Received from:"+((IPEndPoint)target).Address.ToString()+"\n" );

	for (int i=0; i<recv_bytes; i++) {
		Console.Write("{0:X} ",rBuffer[i]);
	}
	Console.WriteLine("\n");
}

s.Close();



-- 
| Matthew Franz                              mdfranz@io.com |
|              http://www.io.com/~mdfranz                   |