110,536
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 连接telnet
/// </summary>
/// <param name="host"></param>
/// <param name="login"></param>
/// <param name="password"></param>
/// <param name="port"></param>
/// <returns></returns>
public string Connect(string host, int port = 23)
{
//连接telnet
IPAddress ip = System.Net.Dns.GetHostAddresses(host)[0];
_remote = new IPEndPoint(ip, port);
_client = new TcpClient();
try
{
_client.Connect(_remote);
_ns = _client.GetStream();
Thread.Sleep(500);//预防连接、GetStream比较慢
}
catch (Exception ex)
{
_logger.Error(ex.Message + "_" + host, ex);
}
return "";
}
/// <summary>
/// 向服务器发送命令
/// </summary>
/// <param name="command"></param>
private void SendData(string command,string ipaddress)
{
if (!command.EndsWith(ENTER))
command += ENTER;
byte[] data = System.Text.Encoding.Default.GetBytes(command);
try
{
_ns.Write(data, 0, data.Length);
}
catch (Exception ex)
{
_logger.Error(ex.Message + "_" + ipaddress, ex);
return;
}
}
/// <summary>
/// 读数据
/// </summary>
/// <returns></returns>
private string RecvData()
{
byte[] tempData = new byte[buffsize];
List<byte> data = new List<byte>();
int count = 0;
do
{
if (!_ns.DataAvailable)
{
return "";
}
try
{
count = _ns.Read(tempData, 0, tempData.Length);
}
catch (Exception ex)
{
_logger.ErrorException(ex.Message, ex);
return null;
}
data.AddRange(tempData.Take(count));
} while (count == buffsize);
return Negotiate(data.ToArray());
}
希望对楼主有帮助吧