Testing broadcasting and receiving messages

Guys, with some difficulty figuring this out: I'm trying to check if the code works (in C #) to broadcast a message and receive a message:

Code for sending a datagram (in this case, the host name):

public partial class Form1 : Form { String hostName; byte[] hostBuffer = new byte[1024]; public Form1() { InitializeComponent(); StartNotification(); } public void StartNotification() { IPEndPoint notifyIP = new IPEndPoint(IPAddress.Broadcast, 6000); hostName = Dns.GetHostName(); hostBuffer = Encoding.ASCII.GetBytes(hostName); UdpClient newUdpClient = new UdpClient(); newUdpClient.Send(hostBuffer, hostBuffer.Length, notifyIP); } } 

And the code for receiving the datagram:

  public partial class Form1 : Form { byte[] receivedNotification = new byte[1024]; String notificationReceived; StringBuilder listBox; UdpClient udpServer; IPEndPoint remoteEndPoint; public Form1() { InitializeComponent(); udpServer = new UdpClient(new IPEndPoint(IPAddress.Any, 1234)); remoteEndPoint=null; startUdpListener1(); } public void startUdpListener1() { receivedNotification = udpServer.Receive(ref remoteEndPoint); notificationReceived = Encoding.ASCII.GetString(receivedNotification); listBox = new StringBuilder(this.listBox1.Text); listBox.AppendLine(notificationReceived); this.listBox1.Items.Add(listBox.ToString()); } 

}

To get the code, I have a form that has only a list (listBox1). The problem here is that when I execute the resulting code, the program starts, but the form is not visible. However, when I comment on the function call (startUdpListener1 ()), the target is not passed, but the form is visible. What is going wrong?

+1
broadcasting
source share
1 answer

udpServer.Receive () is probably a blocking call waiting for data (which it does not receive)

+1
source share

All Articles