Reputation: 5959
I am trying to auto find server app within a local network using usbclient in the following code. the sever and broadcaster is run on separate pc. The server always receives the broadcaster message, but the broadcaster doesn't or sometimes receives message only once.
//////////////////////
// UDP BROADCASTER //
//////////////////////
static void Main(string[] args)
{
const string addr = "224.0.0.1";
const int prt = 5555;
UdpClient udpc = new UdpClient();
udpc.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
var LocalIP = GetLocalIPAddress();
Console.WriteLine($"LocalIP: {LocalIP}");
udpc.Client.Bind(new IPEndPoint(IPAddress.Parse(LocalIP), prt));
// Enable broadcast.
udpc.EnableBroadcast = false;
// Disable multicast loopback.
udpc.MulticastLoopback = false;
udpc.JoinMulticastGroup(IPAddress.Parse(addr));
System.Threading.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);
string data = Encoding.ASCII.GetString(recv);
Console.WriteLine($"recv {data} from {rp.ToString()}");
}), null);
evs[1] = asy.AsyncWaitHandle;
if (WaitHandle.WaitAny(evs) == 0) return;
}
}));
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
{
for (int i = 0; i < 100; i++)
{
var msg = $"Hello {i}";
var byts = ASCIIEncoding.ASCII.GetBytes(msg);//new byte[] { 0x1, 0x2 };
udpc.Send(byts, byts.Length, new IPEndPoint(IPAddress.Parse(addr), prt));
Console.WriteLine($"Sent: {msg}");
Thread.Sleep(2000);
if (abort.WaitOne(0, false)) return;
}
}));
Console.Read();
abort.Set();
}
/////////////////
// UDP SERVER //
////////////////
static void Main(string[] args)
{
const string addr = "224.0.0.1";
const int prt = 5555;
UdpClient udpc = new UdpClient();
udpc.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
//udpc.Client.Bind(new IPEndPoint(IPAddress.Any, prt));
var LocalIP = GetLocalIPAddress();
Console.WriteLine($"LocalIP: {LocalIP}");
udpc.Client.Bind(new IPEndPoint(IPAddress.Parse(LocalIP), prt));
udpc.JoinMulticastGroup(IPAddress.Parse(addr));
System.Threading.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);
string data = Encoding.ASCII.GetString(recv);
Console.WriteLine($"{DateTime.Now.TimeOfDay} recv {data} from {rp.ToString()}");
var byts = ASCIIEncoding.ASCII.GetBytes($"Ip:: {LocalIP} MachineName: {Environment.MachineName}");
udpc.Send(byts, byts.Length, rp);
}), null);
evs[1] = asy.AsyncWaitHandle;
if (WaitHandle.WaitAny(evs) == 0) return;
}
}));
Console.Read();
abort.Set();
}
any ideas or recommendation is welcome
Upvotes: 0
Views: 29