【发布时间】:2019-09-29 19:01:49
【问题描述】:
目前,我正在开发一个由 200 个传感器节点组成的网络模拟器。在我的代码中,我需要每个节点向其邻居发送 hello 数据包。在上一步中,我编写了一个代码来将特定节点的 hello 消息广播到它们的邻居节点,它们是传感器节点。现在,我需要这些传感器节点将其队列中的 hello 数据包重新转发给其邻居。我需要检查传感器节点的队列是否已经有一个 hello 数据包,以便将其重新转发到该传感器节点的邻居。 . .
例如,我创建了这样的 hello:
Packet hello = new Packet(CNs[i].cnID, i, i, 0, CNs[i].cnDepth, DateTime.Now, "Hello, I am a Courier node near of you");
数据包声明如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AUV_Topology
{
class Packet
{
public int senderID;
public int nextID;
public int recieverID;
public int packetSequenceNum;
public int depth;
public DateTime sendingTime;
public String data;
public Packet(int sID, int nID, int rID, int pSecNum, int depth,DateTime sTime, String data)
{
this.senderID = sID;
this.nextID = nID;
this.recieverID = rID;
this.packetSequenceNum = pSecNum;
this.depth = depth;
this.sendingTime = sTime;
this.data = data;
}
}
}
为了确保传感器节点队列有从另一个节点接收到的hello数据包,我使用“foreach”并检查列表中的每个数据包是否包含“你好,我是你附近的Courier节点”......
不幸的是,我尝试使用
SNs[i]queue.contains("Hello, I am a Courier node near of you");
其中SN[i]是一个传感器节点数组,队列是一个属性声明如下:
public List<Packet> queue = new List<Packet>();
但我得到一个语法错误:
参数 1:无法从 'string' 转换为 'AUV_Topology.Packet' AUVs_TOPOLOGY
我该怎么做?
这是一个可能的解决方案:
for(int j=0; j < NodeNum; j++)
{
if (SNsNighbors[i, j] == 1)
{
String temp = SNs[i].queue.ToString();
if (temp.Contains("Hello"))
{
}
}
}
【问题讨论】:
-
如果类是
public,则编译。 -
SNs[i]queue.Contains(...)-Contains中的大写字母C? -
请多放些代码,因为你说队列是一个属性,但什么是属性?我不理解数组上下文中的属性。
-
“我遇到了一个错误”——请说出确切的类型和错误消息文本是什么——“猜猜这段代码会抛出什么错误”是一款不错的游戏,但玩起来需要相当长的时间:)
-
@CaiusJard :我添加了错误......