Answer the question
In order to leave comments, you need to log in
Why broadcast is one-way?
There are 2 classes, 1 for sending, the other for listening to UDP. For some reason, if you run them on 2 machines on the same network, the first one sends and the second one receives normally. But if the second sends, then the first receives nothing. I cannot understand how this is possible. In theory, I did not set interfaces anywhere and there is no reason for broadcast packets to go in one direction and not go in the other. Can anyone tell me where to dig?
Shipping:
public class Sender { UdpClient sendClient; IPEndPoint sendEndPoint; public Sender(int port_udp) { sendClient = new UdpClient(); sendEndPoint = new IPEndPoint(IPAddress.Broadcast, port_udp); //- See more at: http://onlinedotnettutorials.blogspot.ru/2012/07/chat-application-without-server-udp.html#sthash.3irnhrOB.dpuf } public void Send(string message) { Byte[] Text = Encoding.ASCII.GetBytes(message); sendClient.Send(Text, Text.Length, sendEndPoint); } ~Sender() { sendClient.Close(); } }
Receipt:
public class Receiver { public delegate void onNewMessageContainer(string Text); public event onNewMessageContainer onNewMessage; UdpClient receiveClient; IPEndPoint receiveEndPint; //- See more at: http://onlinedotnettutorials.blogspot.ru/2012/07/chat-application-without-server-udp.html#sthash.3irnhrOB.dpuf public Receiver(int port_udp) { receiveClient = new UdpClient(port_udp); receiveEndPint = new IPEndPoint(IPAddress.Any, 0); Thread rec = new Thread(ReceiveMessageFn); rec.Start(); } ~Receiver() { NeedShutdown = true; } public bool NeedShutdown = false; private void ReceiveMessageFn() { try { while (!NeedShutdown) { Byte[] receve = receiveClient.Receive(ref receiveEndPint); string message = Encoding.ASCII.GetString(receve); if (message != null || message.Length >0) { if (onNewMessage!=null ) { onNewMessage(message); } } } Thread.CurrentThread.Abort(); } catch (ThreadAbortException ex) { Console.WriteLine(ex.Message); } } }
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question