C# C接收UDP数据

C# C接收UDP数据,c#,winforms,udp,C#,Winforms,Udp,我正在尝试学习C语言中的UDP套接字编程。我制作了一个WinForm来发送数据,下面列出了代码。这是一个小表单,有三个文本框和一个按钮 using System; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; namespace UDP_example { public partial class Form1 : Form1 { pu

我正在尝试学习C语言中的UDP套接字编程。我制作了一个WinForm来发送数据,下面列出了代码。这是一个小表单,有三个文本框和一个按钮

using System;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace UDP_example

{
    public partial class Form1 : Form1
    {
    public Form1()
    {
        InitializeComponent();
    }

    private void btnSend_Click(object sender, EventArgs e)
    {
        // make sure all text boxes contain data, or do nothing.
        if (string.IsNullOrEmpty(txtIP.Text)) return;
        if (string.IsNullOrEmpty(txtPort.Text)) return;
        if (string.IsNullOrEmpty(txtMessage.Text)) return;

        // convert the text into byte array
        byte[] message = Encoding.ASCII.GetBytes(txtMessage.Text);
        string ipAddress = txtIP.Text;
        int sendPort = Convert.ToInt32(txtPort.Text);

        // send the data
        try
        {
            using (var client = new UdpClient())
            {
                IPEndPoint server = new IPEndPoint(IPAddress.Parse(ipAddress), sendPort);
                client.Connect(server);
                client.Send(message, message.Length);
            }
        }
        // display error if one occurs
        catch (Exception exc)
        {
            MessageBox.Show(exc.Message);
        }

        // clean up
        txtMessage.Clear();
        txtMessage.Focus();
    }
}
这部分似乎工作正常。我需要的是在连接另一侧的文本框或消息框中显示接收到的数据

有人能告诉我怎么做吗?我不知道如何基本上让套接字异步监听并在接收端显示传入数据。它将只是一个带有文本框的表单,或者一个显示输入数据的消息框

编辑: 这是接收代码,但我希望它从任何IP地址接收,而不仅仅是本地地址。找到并修改了此代码段

using System;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace UDP_Server
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ConnectUDP));
        }

        private void ConnectUDP(object state)
        {
            UdpClient subscriber = new UdpClient(4345);
            IPAddress addr = IPAddress.Parse("127.0.0.1");
            subscriber.JoinMulticastGroup(addr);
            IPEndPoint ep = null;

            byte[] pdata = subscriber.Receive(ref ep);
            string rdata = Encoding.ASCII.GetString(pdata);
            txtReceived.Text += rdata;
        }
    }
}

今天我读了很多样本,在YouTube上看了一些视频,但我觉得这很简单。我唯一的要求是它将从UDP端口接收的数据放入文本框中。谢谢您的帮助。

如@JuanR所述,您的服务器端需要类似的东西

// This constructor arbitrarily assigns the local port number.
UdpClient udpClient = new UdpClient(yourUDPPort);

//IPEndPoint object will allow us to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

// Blocks until a message returns on this socket from a remote host.
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); 
string returnData = Encoding.ASCII.GetString(receiveBytes);

你好。UdpClient类的文档中有一个例子可以做到这一点:JuanR-谢谢。我不明白在收到数据时如何调用此代码。我想这是我的问题。使用TCP,当接收到数据时,触发并处理事件。我不知道如何在使用WinForm.Steve接收数据时引发事件-谢谢。如上所述,当代码用于Windows窗体应用程序时,我看不到在何处侦听和响应传入的数据。再次感谢。