您的位置:首页 > 理论基础 > 计算机网络

C# 超级简单的Telnet (TcpClient)客户端

2015-05-20 11:49 309 查看
基于Sockets

没什么好说的,代码说明了所有

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
public static NetworkStream stream;
public static TcpClient tcpclient;
public static string ip;
public static  int port;
static void Main(string[] args)
{
Console.WriteLine("目标IP:");
ip = Console.ReadLine();
Console.WriteLine("目标Port:");
port =int.Parse(Console.ReadLine());

Run();
}

static public void Run()
{
tcpclient = new TcpClient(ip, port);  // 连接服务器
stream = tcpclient.GetStream();   // 获取网络数据流对象
StreamWriter sw = new StreamWriter(stream);
StreamReader sr = new StreamReader(stream);
while (true)
{
//Read Echo
//Set ReadEcho Timeout
stream.ReadTimeout = 10;
try
{
while (true)
{
char c = (char)sr.Read();
if (c < 256)
{
if (c == 27)
{
while (sr.Read() != 109) { }
}
else
{
Console.Write(c);
}
}
}
}
catch
{

}
//Send CMD
sw.Write("{0}\r\n", Console.ReadLine());
sw.Flush();
}
}

}
}


TcpClient
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: