我有几种不同的方法可以连接到远程主机、发送消息、获得回复和使用信息。这一直很好,直到我在我的连接类的同一个实例中使用两种方法。在我收到错误的示例中,我运行以下方法;
public string sendRequestAccountID(string siteID)
{
//build message
String response = String.Empty;
TcpClient client = new TcpClient();
client.Connect(detailsHere);
NetworkStream stream = client.GetStream();
StreamWriter writer = new StreamWriter(stream);
writer.AutoFlush = false;
writer.WriteLine(sb.ToString());
writer.Flush();
StreamReader reader = new StreamReader(stream);
List<XmlNode> nodeList = new List<XmlNode>();
byte[] responseBytes = new byte[4096];
int bytesRead = 0;
while (true)
{
bytesRead = stream.Read(responseBytes, 0, responseBytes.Length);
if (bytesRead > 0)
{
//handle message
}
if (bytesRead == 0)
{
stream.Flush();
stream.Close();
client.Close();
string finalResponse = stuffHereToSend;
return finalResponse;
}
}
}
这发送正常,并按预期返回一条消息。但是,如果我然后使用我的连接类的相同实例并使用以下方法;
public bool sendNewDevice(IDeviceInterface device)
{
NetworkStream stream;
sb = new StringBuilder();
//build message
String response = String.Empty;
TcpClient client = new TcpClient();
client.Connect(detailsHere);
stream = client.GetStream();
StreamWriter writer = new StreamWriter(stream);
writer.AutoFlush = false;
writer.WriteLine(sb.ToString());
writer.Flush();
StreamReader reader = new StreamReader(stream);
List<XmlNode> nodeList = new List<XmlNode>();
byte[] responseBytes = new byte[4096];
int bytesRead = 0;
while (true)
{
bytesRead = stream.Read(responseBytes, 0, responseBytes.Length);
if (bytesRead > 0)
{
//handle message
}
}
}
我收到一个错误,上面写着“无法访问已处置的对象”;
bytesRead = stream.Read(responseBytes, 0, responseBytes.Length);
虽然我认为我刚刚以最新的方法分配了流。它是否试图使用以前关闭的?有没有办法解决这个问题或我错过了什么愚蠢的事情?
编辑:这与客户没有正确处理有关吗?这两种方法在一秒钟内运行,也许第二种方法是在第一种方法关闭之前尝试打开?