0

伙计们,我在这里需要一些帮助。我正在用 c# 做一个项目,其中数据需要作为数据报发送并接收广播的数据。

以下是代码:

  public void StartUdpListener(Object state)
    {

        receivedNotification = udpServer.Receive(ref remoteEndPoint);
        notificationReceived = Encoding.ASCII.GetString(receivedNotification);

        listBox = new StringBuilder(this.listBox1.Text);
        listBox.AppendLine(notificationReceived);


        if (listBox1.InvokeRequired)
        {
            this.Invoke((MethodInvoker)delegate { this.listBox1.Items.Add(listBox.ToString()); });
        }



    }

    public void StartNotification()
    {

        ThreadPool.QueueUserWorkItem(new WaitCallback(StartUdpListener));

        hostName = Dns.GetHostName();
        hostBuffer = Encoding.ASCII.GetBytes(hostName);

        UdpClient newUdpClient = new UdpClient();
        newUdpClient.Send(hostBuffer, hostBuffer.Length, notifyIP);



    }

你们能否告诉我代码是否正常,因为周围没有人可以在局域网上测试代码,谢谢大家。

4

1 回答 1

0

为什么不在单独的实例中设置自己的广播器和侦听器。

这是一篇关于 c# 中套接字编程的好文章

于 2009-05-15T14:11:45.583 回答