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

基于Sockets

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

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Net.Sockets;
 6 using System.IO;
 7 
 8 namespace ConsoleApplication1
 9 {
10     class Program
11     {
12         public static NetworkStream stream;
13         public static TcpClient tcpclient;
14         public static string ip;
15         public static  int port;
16         static void Main(string[] args)
17         {
18             Console.WriteLine("目标IP:");
19             ip = Console.ReadLine();
20             Console.WriteLine("目标Port:");
21             port =int.Parse(Console.ReadLine());
22 
23             Run();
24         }
25 
26         static public void Run()
27         {
28             tcpclient = new TcpClient(ip, port);  // 连接服务器
29             stream = tcpclient.GetStream();   // 获取网络数据流对象
30             StreamWriter sw = new StreamWriter(stream);
31             StreamReader sr = new StreamReader(stream);
32             while (true)
33             {
34                 //Read Echo
35                 //Set ReadEcho Timeout
36                 stream.ReadTimeout = 10;
37                 try
38                 {
39                     while (true)
40                     {                        
41                             char c = (char)sr.Read();
42                             if (c < 256)
43                             {
44                                 if (c == 27)
45                                 {
46                                     while (sr.Read() != 109) { }
47                                 }
48                                 else
49                                 {
50                                     Console.Write(c);
51                                 }
52                             }
53                     }
54                 }
55                 catch
56                 {
57                     
58                 }
59                 //Send CMD
60                 sw.Write("{0}\r\n", Console.ReadLine());
61                 sw.Flush();
62             }
63         }
64 
65     }
66 }
TcpClient

 

转载于:https://www.cnblogs.com/xiaoboz/p/4516803.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值