udp 监听器等待数据 [英] udp listener waiting for data

查看:25
本文介绍了udp 监听器等待数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这对某些人来说可能是一个基本问题,所以请善待.

I know it might be a basic question for some people so please be kind.

以下说明了我的问题.在我的计算机上,我有 Visual Studio 2010,其中运行了一个 c# 程序,我有一个 udp 侦听器,正在等待 udp 端口​​(例如端口:85)上的 udp 数据.

Following explains my problem. On my computer, i have visual studio 2010 in which a c# program is running where I have a udp listener waiting for udp data on a udp port (lets say port:85).

UdpClient listener = null;
try
{
    listener = new UdpClient((int)nudPort.Value);
    if (listener.Available > 0)
    { ......
    }
}

谁能告诉我一种方法(任何程序),我可以通过它在此端口上发送 udp 数据,以便我的 c# 程序可以使用同一台计算机检测它.

Can anybody tell me a way (any program ) by which i can send udp data on this port so that my c# program can detect it using the same computer.

推荐答案

下面的代码给你一个思路

The following code will give you an idea

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

namespace WindowsFormsApplication1
{
    public partial class Form4 : Form
    {
        private Thread _listenThread;
        private UdpClient _listener;

        public Form4()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this._listenThread = new Thread(new ThreadStart(this.StartListening));
            this._listenThread.Start();
        }

        private void StartListening()
        { 

            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 35555);
            IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

            this._listener = new UdpClient(localEndPoint);

            try
            {
                do
                {
                    byte[] received = this._listener.Receive(ref remoteIpEndPoint);

                    MessageBox.Show(Encoding.ASCII.GetString(received));

                }
                while (this._listener.Available == 0);
            }
            catch (Exception ex)
            {
                //handle Exception
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 35556);
            IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 35555);

            UdpClient caller = new UdpClient(localEndPoint);

            caller.Send(Encoding.ASCII.GetBytes("Hello World!"), 12, remoteIpEndPoint);

            caller.Close();
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);

            this._listener.Close();

            this._listenThread.Abort();
            this._listenThread.Join();

            this._listenThread = null;
        }    
    }
}

这篇关于udp 监听器等待数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆