从文本框获取的多个端口上的C#UDP侦听器
我试着到处寻找这个问题,但没有结果。 我试图实现的是在文本框中输入端口列表。 然后,我创建一个udpClient阵列,并使用这些端口开始侦听它们从文本框获取的多个端口上的C#UDP侦听器,c#,arrays,udp,listener,port,C#,Arrays,Udp,Listener,Port,我试着到处寻找这个问题,但没有结果。 我试图实现的是在文本框中输入端口列表。 然后,我创建一个udpClient阵列,并使用这些端口开始侦听它们 static class communicator { // Setting Variables static UdpClient[] UDPreceiver; static TcpListener[] TCPreceiver; static IPAddress ipAddress
static class communicator
{
// Setting Variables
static UdpClient[] UDPreceiver;
static TcpListener[] TCPreceiver;
static IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
static bool listening = false;
// Listener function
public static void UDPstartlistening(int port)
{
// Startlistening
listening = true;
while (listening)
{
try
{
UDPreceiver[port] = new UdpClient(port); // udp server
if (UDPreceiver[port].Available > 0) // Only read if we have some data queued in buffer
{
//IPEndPoint object will allow us to read datagrams sent from any tracker.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
// Blocks untill data is received
Byte[] receiveBytes = UDPreceiver[port].Receive(ref RemoteIpEndPoint);
string returnData = ByteArrayToString(receiveBytes);
// Uses the IPEndPoint object to determine who sent us anything
Program.form1.addlog("Received: " + returnData.ToString() + " - from " + RemoteIpEndPoint.Address.ToString() + " on port: " + RemoteIpEndPoint.Port.ToString());
// Forward this message to the website
Task.Run(() => forwardToWebsite(returnData.ToString(), RemoteIpEndPoint.Address.ToString(), RemoteIpEndPoint.Port, "udp", port));
}
Thread.Sleep(10);
}
catch (Exception e)
{
MessageBox.Show("Source : " + e.Source + "\r\n" + "Message : " + e.Message, "Error");
}
}
}
它在“udpreciver[port].Available”一行给出了“未设置为对象实例的对象引用”
我这样做对吗?我想你需要仔细看看互联网上的例子,你刚刚创建的对象
udpreciver[port]
没有处于接收数据的状态。根据对象应调用BeginReceive
。没有C#但这可能会有所帮助。试试这个,它包含一些错误修复:
static class communicator
{
// Setting Variables
static List<UdpClient> UDPreceivers = new List<UdpClient>();
//static List<TcpListener> TCPreceivers = new List<TcpListener>();
static IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
static bool listening = false;
// Listener function
public static void UDPstartlistening(int port)
{
UdpClient UDPreceiver = new UdpClient(port); // udp server
UDPreceivers.Add(UDPreceiver);
// Startlistening
listening = true;
while (listening)
{
try
{
if (UDPreceiver.Available > 0) // Only read if we have some data queued in buffer
{
//IPEndPoint object will allow us to read datagrams sent from any tracker.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
// Blocks untill data is received
Byte[] receiveBytes = UDPreceiver.Receive(ref RemoteIpEndPoint);
string returnData = ByteArrayToString(receiveBytes);
// Uses the IPEndPoint object to determine who sent us anything
Program.form1.addlog("Received: " + returnData.ToString() + " - from " + RemoteIpEndPoint.Address.ToString() + " on port: " + RemoteIpEndPoint.Port.ToString());
// Forward this message to the website
Task.Run(() => forwardToWebsite(returnData.ToString(), RemoteIpEndPoint.Address.ToString(), RemoteIpEndPoint.Port, "udp", port));
}
Thread.Sleep(10);
}
catch (Exception e)
{
MessageBox.Show("Source : " + e.Source + "\r\n" + "Message : " + e.Message, "Error");
}
}
}
}
静态类通信器
{
//设置变量
静态列表UDPrecivers=新列表();
//静态列表tcprecivers=新列表();
静态IPAddress IPAddress=Dns.GetHostEntry(“本地主机”).AddressList[0];
静态布尔监听=假;
//侦听器功能
公共静态无效UDPstartlistening(int端口)
{
UdpClient udpreciver=新的UdpClient(端口);//udp服务器
udpreciver.Add(udpreciver);
//惊人的倾听
倾听=真实;
边听
{
尝试
{
if(udpreciver.Available>0)//仅当缓冲区中有一些数据排队时才读取
{
//IPEndPoint对象将允许我们读取从任何跟踪器发送的数据报。
IPEndPoint RemoteIpEndPoint=新IPEndPoint(IPAddress.Any,0);
//块,直到接收到数据
字节[]receiveBytes=udpreciver.Receive(参考RemoteIpEndPoint);
string returnData=ByteArrayToString(接收字节);
//使用IPEndPoint对象确定谁向我们发送了任何内容
Program.form1.addlog(“接收:“+returnData.ToString()+”-来自端口:“+RemoteIpEndPoint.Address.ToString()+”上的“+RemoteIpEndPoint.port.ToString()”);
//将此消息转发到网站
运行(()=>forwardToWebsite(returnData.ToString(),RemoteIpEndPoint.Address.ToString(),RemoteIpEndPoint.Port,“udp”,Port));
}
睡眠(10);
}
捕获(例外e)
{
MessageBox.Show(“源:+e.Source+”\r\n“+”消息:+e.Message,“错误”);
}
}
}
}
您在哪里为UDPreciver数组赋值?您是否最好使用字典或其他集合来完成此操作?我在第4行声明了数组,并开始为try括号中的元素赋值。数组中没有其他赋值方法。为什么?我是否以错误的方式分配它?在写有:Byte[]receiveBytes=udpreciver[port].Receive(ref RemoteIpEndPoint)的行上;这就是你所说的吗?你愿意让一个线程在异步模式下等待传入数据,我的意思是你需要命令对象开始等待,Available
只有在这之后才可用。尝试udpreciver[port].BeginReceive()代码>在udpreciver[port]=新的UdpClient(port)之后
在您使用完EndReceive
后,请不要忘记停止此操作。我做了一些与您所做的相近的事情,并且成功了!!