对不起!如果问题是无关紧要的。我有一台机器通过我试图通过c#中的套接字编程使用IP地址和端口号连接。在连接后我试图传递一些机器特定的凭证,如用户名/密码,然后尝试执行一些命令将代码保存到机器中。每件事情都很顺利,我没有得到任何类型的异常或错误,但在验证机器中的代码时我没有得到那个。 所以我的问题是为什么呢?
这是我在c#中的代码。
TcpClient client = new TcpClient();
textfileSaveLocation = "D:/Response.txt";
try
{
using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true))
{
writer.WriteLine("Before Connection !");
}
client.Connect(new IPEndPoint(IPAddress.Parse(ip), intport));
Thread.Sleep(5000);
using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true))
{
writer.WriteLine("Connected To Machine !");
}
catch (Exception ex)
{
// Log the error here.
client.Close();
using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true))
{
writer.WriteLine("Connection Error !");
}
continue;
}
try
{
using (NetworkStream stream = client.GetStream())
{
byte[] username = Encoding.ASCII.GetBytes(user + "\r\n");
stream.Write(username, 0, username.Length);
Thread.Sleep(5000);
using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true))
{
writer.WriteLine("UserName Enetered !");
}
byte[] password = Encoding.ASCII.GetBytes(pass + "\r\n");
stream.Write(password, 0, password.Length);
Thread.Sleep(5000);
using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true))
{
writer.WriteLine("Password Passed !");
}
byte[] terminal = Encoding.ASCII.GetBytes(port2 + "\r\n");
stream.Write(terminal, 0, terminal.Length);
Thread.Sleep(5000);
using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true))
{
writer.WriteLine("Terminal Value Entered !");
}
string ch = "change autho-code ";
if (acc != "0")
{
ch = "change authorization-code " + acc + "\r\n";
byte[] chAuth = Encoding.ASCII.GetBytes(ch);
stream.Write(chAuth, 0, chAuth.Length);
Thread.Sleep(5000);
using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true))
{
writer.WriteLine("Change Authcode Request not Zero !");
}
byte[] aCodeServ = Encoding.ASCII.GetBytes(acode + "\t");
stream.Write(aCodeServ, 0, aCodeServ.Length);
Thread.Sleep(5000);
using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true))
{
writer.WriteLine("Authcode Configured not Zero !");
}
}
else
{
byte[] aChServ = Encoding.ASCII.GetBytes(ch + "\r\n");
stream.Write(aChServ, 0, aChServ.Length);
Thread.Sleep(5000);
using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true))
{
writer.WriteLine("New Authcode Request !");
}
byte[] aSpace = Encoding.ASCII.GetBytes(" " + "\t" + " ");
stream.Write(aSpace, 0, aSpace.Length);
Thread.Sleep(5000);
using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true)) {
writer.WriteLine("Authcode Configured !");
}
}
}
client.Close();
using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true))
{
writer.WriteLine("Connection Out !");
}
}
catch (Exception ex)
{
// Log the error here.
client.Close();
using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true))
{
writer.WriteLine("Executing Command Error !");
}
}
}
在上面的程序中,我注意到我没有添加NetworkStream.Close()
。这是为什么我的代码没有在机器或其他东西中配置的问题。
请帮帮我。 提前谢谢。