G
greenxiar
My Computer is Win2K3 R2, Code is:
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();
}
}
}
When my computer is single network connection(Local Network),
the code works fine. My problem is, when i connect the Internet with PPPOE,
the UDPClient can not receive the broadcasting data!
Why?
greenxiar
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();
}
}
}
When my computer is single network connection(Local Network),
the code works fine. My problem is, when i connect the Internet with PPPOE,
the UDPClient can not receive the broadcasting data!
Why?
greenxiar