我知道这可能是某些人的基本问题所以请善待。
以下解释了我的问题。在我的计算机上,我有visual studio 2010,其中运行了一个c#程序,我有一个udp监听器在udp端口上等待udp数据(比如port:85)。
UdpClient listener = null;
try
{
listener = new UdpClient((int)nudPort.Value);
if (listener.Available > 0)
{ ......
}
}
任何人都可以告诉我一个方法(任何程序)我可以在这个端口发送udp数据,以便我的c#程序可以使用同一台计算机检测它。
答案 0 :(得分:0)
您是否尝试过Netcat
nc -u your_server_name_here 85< - 其中85是您的侦听端口
检查Wikipedia.org如何使用它。有一个关于如何在客户端和服务器之间发送UDP包的例子
答案 1 :(得分:0)
以下代码将为您提供一个想法
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;
}
}
}