Les gars ai du mal à comprendre ceci: Je suis en train de vérifier si le code (en C#) pour diffuser un message et recevoir le message fonctionne:Contrôle des messages de radiodiffusion et de réception
Le code pour envoyer le datagramme (dans ce cas, son nom d'hôte) est:
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);
}
}
Et le code pour recevoir le datagramme est:
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());
}
}
Pour la réception du code j'ai un formulaire qui a seulement une listbox (listBox1). Le problème ici est que lorsque j'exécute le code à recevoir, le programme s'exécute mais le formulaire n'est pas visible. Cependant quand je commente l'appel de fonction (startUdpListener1()), le but n'est pas servi mais le formulaire est visible. Qu'est-ce qui ne va pas?
mais wont la forme soit atleast visible? – Avik
@Avik, ryansstack a raison. Vous devrez démarrer un nouveau thread pour éviter l'appel de blocage, ou simplement utiliser udpServer.BeginReceive –