Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / April 2007

Tip: Looking for answers? Try searching our database.

UdpClient Stop Receiving Broadcast Datagram On Lan When Connect to Internet

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
greenxiar - 25 Mar 2007 05:35 GMT
My code is below and the platform is Win2K3 R2:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace TechUDPBroadcast {
 class Program {
     static void Main(string[] args) {
         const string addr = "224.1.3.99";
         const int prt = 12799;

         UdpClient udpc = new UdpClient();
         udpc.Client.SetSocketOption(SocketOptionLevel.Socket,
                           SocketOptionName.ReuseAddress, true);
         udpc.Client.Bind(new IPEndPoint(IPAddress.Any, prt));
         udpc.EnableBroadcast = true;

         udpc.JoinMulticastGroup(IPAddress.Parse(addr));

         ManualResetEvent abort = new ManualResetEvent(false);
         WaitHandle[] evs = new WaitHandle[2];
         evs[0] = abort;
         ThreadPool.QueueUserWorkItem(new WaitCallback(delegate {
             while (true) {
                 IAsyncResult asy = udpc.BeginReceive(
                       new AsyncCallback(delegate(IAsyncResult iasy) {
                     IPEndPoint rp = null;
                     byte[] recv = udpc.EndReceive(iasy, ref rp);
                     Console.Write(".");
                 }), null);
                 evs[1] = asy.AsyncWaitHandle;
                 if (WaitHandle.WaitAny(evs) == 0) return;
             }
         }));
         ThreadPool.QueueUserWorkItem(new WaitCallback(delegate {
             for (int i = 0; i < 1000; i++) {
                 udpc.Send(new byte[] { 0x1, 0x2 }, 2,
                             new IPEndPoint(IPAddress.Parse(addr), prt));
                 Console.Write("+");
                 Thread.Sleep(1000);
                 if (abort.WaitOne(0, false)) return;
             }
         }));
         Console.ReadKey();
         Abort.Set();
     }
 }
}

The code is simple and has no tricky. UdpClient broadcast
datagram every second, another thread receive it asynchorously.
When it is running at LAN, sending & receiving are OK.
Then i connect the PC to Internet, the UdpClient stop receiving!
This happened with or without Windows FireWall.

Is this a bug of DotNetFramework?

greenxiar
greenxiar - 01 Apr 2007 06:09 GMT
Sorry. two line cause the problem

udpc.Client.Bind(new IPEndPoint(IPAddress.Any, prt));
udpc.JoinMulticastGroup(IPAddress.Parse(addr));

The UdpClient can only receive and send multicast datagrams
to only "ONE" binded IPAddress. If there are more than
one network connection as PPPOE or second NIC,
you will not sure which IPAddress is sending or listening.

So the answer is

udpc.Client.Bind(new IPEndPoint([localAddress], [localPort]));
udpc.JoinMulticastGroup(IPAddress.Parse([multicastAddress]),
[localAddress]);

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.