⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainform.cs

📁 Telnet客户端案例
💻 CS
📖 第 1 页 / 共 2 页
字号:
			{
				return;
			}

			if (e.KeyChar == 13)
			{
				Send(e.KeyChar.ToString() + LF);
			}
			else
			{
				//只接受有效的字符
				if (e.KeyChar >= ' ' && e.KeyChar <= 'z')
				{
					Send(e.KeyChar.ToString());
				}				
			}
			e.Handled = true;
		}


		private void Connect() 
		{
			sw_igoahead = false;
			sw_ugoahead = true;
			sw_igoahead = false;
			sw_echo = true;
			sw_termsent = false;

			Console.WriteLine("连接服务器" + sf.txtRemoteAddress.Text + "...");

			socket = new Socket(AddressFamily.InterNetwork,
				SocketType.Stream,ProtocolType.Tcp);
			socket.SetSocketOption (SocketOptionLevel.Socket, 
				SocketOptionName.SendTimeout, 5000);	
			IPAddress ipAdd=IPAddress.Parse(sf.txtRemoteAddress.Text);
			int port = System.Convert.ToInt32(sf.txtPort.Text);
			IPEndPoint hostEndPoint = new IPEndPoint(ipAdd, port);
			
			try
			{
				socket.Connect(hostEndPoint);
			}
			catch (Exception e)
			{
				Console.WriteLine(e.ToString());
				this.statusBarPanel4.Text = "服务器状态:服务器未准备好";
				return;
			}

			if (socket.Connected)
			{
				//更新状态
				this.statusBarPanel1.Text = "状态:已连接";
				this.statusBarPanel3.Text = "服务器地址:" + 
					sf.txtRemoteAddress.Text;
				this.statusBarPanel4.Text = "服务器状态:服务器已经接受连接"; 

				Thread thread=new Thread(new ThreadStart(this.TelnetThread));
				thread.Start();
			}
				
		}

		private void CloseConnection()
		{
			if (socket != null && socket.Connected)
			{
				socket.Shutdown(SocketShutdown.Both);
				socket.Close();
			}
			this.statusBarPanel1.Text = "状态:断开连接...";

		}				

		private string GetLocalAPAddress()
		{
			
			IPAddress[] AddressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
			if (AddressList.Length>0)
			{
				//首先获得拨号动态分配IP地址
				//如果没有,则获得局域网地址
				return AddressList[AddressList.Length-1].ToString();
			}
			return "";
		}

		private void TelnetThread()
		{	
			while(socket.Connected)
			{
				try
				{
					string str = Receive();
					str = str.Replace("\0", "");
					string delim = "\b";
					str = str.Trim(delim.ToCharArray());
					if (str.Length > 0)
					{	
						Console.WriteLine(str);
						
						if (str == OUTPUTMARK + BACK)
						{
							//BackupSpace键处理
							this.rtbConsole.ReadOnly = false;
							int curpos = rtbConsole.SelectionStart;
							this.rtbConsole.Select(curpos-1, 1);
							this.rtbConsole.SelectedText = "";
							this.rtbConsole.ReadOnly = true;
						}
						else 
						{
							int len;
							for (int i=0;i<str.Length;i+=80)
							{
								len = 80;
								if (i+80 > str.Length)
								{
									len = str.Length -i;
								}
								this.rtbConsole.AppendText(str.Substring(i, len));
							}
						}
					}
					Thread.Sleep(100);
				}
				catch (Exception e)
				{
					Console.WriteLine(e.ToString());
				}
			}
			this.statusBarPanel1.Text = "状态:已断开";
		}

		private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			CloseConnection();
		}

		//向Telnet服务器发送命令
		private void Send(string msg)
		{
			System.Byte[] message=System.Text.Encoding.ASCII.GetBytes(
				msg.ToCharArray());
			socket.Send(message,message.Length,0);
		}

		//向Telnet服务器发送命令
		private void Send(char[] chr)
		{
			System.Byte[] message=System.Text.Encoding.ASCII.GetBytes(chr);
			socket.Send(message,message.Length,0);
		}

		//接收数据
		private string Receive()
		{
			//用于接收数据的缓冲
			byte[] buf;
			string result="";

			int count = socket.Available;
			if (count > 0)
			{
				buf = new byte[count];
				socket.Receive(buf);
				result = ProcessOptions(buf);
			}
			return result;
		}

		//处理命令字符,buf是包含数据的缓冲
		private string ProcessOptions(byte[] buf)
		{
			string strNormal="";
			int i=0;
			while(i<buf.Length)
			{
				if (buf[i]==IAC)
				{
					switch((char)buf[++i])
					{
						case DO:
							Console.Write("--------------接收到 DO ");
							ProcessDo(buf[++i]);
							break;
						case DONT:
							Console.Write("--------------接收到 DONT ");
							ProcessDont(buf[++i]);
							break;
						case WONT:
							Console.Write("--------------接收到 WONT ");
							ProcessWont(buf[++i]);
							break;
						case WILL:
							Console.Write("--------------接收到 WILL ");
							ProcessWill(buf[++i]);
							break;
						case IAC:
							//正常字符
							strNormal += System.Text.Encoding.Default.GetString(buf,i,1);
							break;
						case SB:
							//子会话开始
							int j=0;
							while(buf[++i] != SE)
							{
								parsedata[j++] = buf[i];
							}
							//子会话结束:
							switch((char)parsedata[0])
							{
								case TERMTYPE:
									break;
								case TERMSPEED:
									if (parsedata[1] == 1)
									{
										Console.WriteLine("发送: SB TERMSPEED 57600,57600");
										Send(IAC +
											SB + TERMSPEED + 
											IS + "57600,57600" + IAC + SE);
									}
									break;
							}
							break;
						default:
							Console.WriteLine("无效的命令" + buf[1]);
							i++;
							break;
					};
				}
				else
				{
					//正常的文字
					strNormal += System.Text.Encoding.Default.GetString(buf,i,1);		
				}
				i++;
			}
			return strNormal;
		}

		private void ProcessDo(short ch)
		{
			//处理DO,以WILL或者WONT响应
			switch((char)ch)
			{
				case BINARY:
					Console.WriteLine(BINARY);
					Send(new char[] {IAC, WONT, BINARY});
					Console.WriteLine("发送: WONT BINARY");
					break;
				case ECHO:
					Console.WriteLine(ECHO);
					Send(new char[] {IAC, WONT, ECHO});
					Console.WriteLine("发送: WONT ECHO");
					break;
				case SGA:
					Console.WriteLine(SGA);
					if (!sw_igoahead)
					{
						Send(new char[] {IAC, WILL, SGA});
						Console.WriteLine("发送: WILL SGA");
						sw_igoahead = true;
					}
					else
					{
						Console.WriteLine("不发送响应");
					}
					break;
				case TERMSPEED:
					Console.WriteLine(TERMSPEED);
					Send(new char[] {IAC, WILL, TERMSPEED});
					Console.WriteLine("发送: WILL TERMSPEED");

					Send(IAC + SB + TERMSPEED + (char)0 + "57600,57600" + 
														  IAC + SE);
					Console.WriteLine("发送:SB TERMSPEED 57600");
					break;
				case TFLOWCNTRL:
					Console.WriteLine(TFLOWCNTRL);
					Send(new char[] {IAC, WONT, TFLOWCNTRL});
					Console.WriteLine("发送: WONT TFLOWCNTRL");
					break;
				case LINEMODE:
					Console.WriteLine(LINEMODE);
					Send(new char[] {IAC, WONT, LINEMODE});
					Console.WriteLine("发送: WONT LINEMODE");
					break;
				case STATUS:
					Console.WriteLine(STATUS);
					Send(new char[] {IAC, WONT, STATUS});
					Console.WriteLine("发送: WONT STATUS");
					break;
				case TIMING:
					Console.WriteLine(TIMING);
					Send(new char[] {IAC, WONT, TIMING});
					Console.WriteLine("发送: WONT TIMING");
					break;
				case DISPLOC:
					Console.WriteLine(DISPLOC);
					Send(new char[] {IAC, WONT, DISPLOC});
					Console.WriteLine("发送: WONT DISPLOC");
					break;
				case ENVIRON:
					Console.WriteLine(ENVIRON);
					Send(new char[] {IAC, WONT, ENVIRON});
					Console.WriteLine("发送: WONT ENVIRON");
					break;
				case UNKNOWN39:
					Console.WriteLine(UNKNOWN39);
					Send(new char[] {IAC, WILL, UNKNOWN39});
					Console.WriteLine("发送: WILL UNKNOWN39");
					break;
				case AUTHENTICATION:
					Console.WriteLine(AUTHENTICATION);
					Send(new char[] {IAC, WONT, AUTHENTICATION});
					Console.WriteLine("发送: WONT AUTHENTICATION");
					Console.WriteLine("发送: SB AUTHENTICATION");
					Send(IAC + SB + AUTHENTICATION + (char)0 + (char)0 +
						(char)0 + (char)0 + "" + IAC + SE);
					break;
				default:
					Console.WriteLine("未知的选项");
					break;
			}
		}

		//处理DONT
		private void ProcessDont(short ch)
		{
			switch((char)ch)
			{
				case SE:
					Console.WriteLine(SE);
					Console.WriteLine("接收到: RECEIVED SE");
					break;
				case ECHO:
					Console.WriteLine(ECHO);
					if (!sw_echo)
					{
						sw_echo = true;
						Send(new char[] {IAC, WONT, ECHO});
						Console.WriteLine("发送: WONT ECHO");
					}
					break;
				case SGA:
					Console.WriteLine(SGA);
					if (!sw_ugoahead)
					{
						Send(new char[] {IAC, WONT, SGA});
						Console.WriteLine("发送: WONT SGA");
						sw_ugoahead = true;
					}
					break;
				case TERMSPEED:
					Console.WriteLine(TERMSPEED);
					Send(new char[] {IAC, WONT, TERMSPEED});
					Console.WriteLine("发送: WONT TERMSPEED");
					break;
				case TFLOWCNTRL:
					Console.WriteLine(TFLOWCNTRL);
					Send(new char[] {IAC, WONT, TFLOWCNTRL});
					Console.WriteLine("发送: WONT TFLOWCNTRL");
					break;
				case STATUS:
					Console.WriteLine(STATUS);
					Send(new char[] {IAC, WONT, STATUS});
					Console.WriteLine("发送: WONT STATUS");
					break;
				case TIMING:
					Console.WriteLine(TIMING);
					Send(new char[] {IAC, WONT, TIMING});
					Console.WriteLine("发送: WONT TIMING");
					break;
				case DISPLOC:
					Console.WriteLine(DISPLOC);
					Send(new char[] {IAC, WONT, DISPLOC});
					Console.WriteLine("发送: WONT DISPLOC");
					break;
				case ENVIRON:
					Console.WriteLine(ENVIRON);
					Send(new char[] {IAC, WONT, ENVIRON});
					Console.WriteLine("发送: WONT ENVIRON");
					break;
				case UNKNOWN39:
					Console.WriteLine(UNKNOWN39);
					Send(new char[] {IAC, WILL, UNKNOWN39});
					Console.WriteLine("发送: WILL UNKNOWN39");
					break;
				default:
					break;
			}			
		}

		//处理WONT
		private void ProcessWont(short ch)
		{
			switch((char)ch)
			{
				case ECHO:
					Console.WriteLine(ECHO);
					if (sw_echo)
					{
						sw_echo = false;
						Send(new char[] {IAC, DONT, ECHO});
						Console.WriteLine("发送: DONT ECHO");
					}
					break;
				case SGA:
					Console.WriteLine(SGA);
					Send(new char[] {IAC, DONT, SGA});
					Console.WriteLine("发送: DONT SGA");
					sw_igoahead = false;
					break;
				case TERMSPEED:
					Console.WriteLine(TERMSPEED);
					Send(new char[] {IAC, DONT, TERMSPEED});
					Console.WriteLine("发送: DONT TERMSPEED");
					break;
				case TFLOWCNTRL:
					Console.WriteLine(TFLOWCNTRL);
					Send(new char[] {IAC, DONT, TFLOWCNTRL});
					Console.WriteLine("发送: DONT TFLOWCNTRL");
					break;
				case LINEMODE:
					Console.WriteLine(LINEMODE);
					Send(new char[] {IAC, DONT, LINEMODE});
					Console.WriteLine("发送: DONT LINEMODE");
					break;
				case STATUS:
					Console.WriteLine(STATUS);
					Send(new char[] {IAC, DONT, STATUS});
					Console.WriteLine("发送: DONT STATUS");
					break;
				case TIMING:
					Console.WriteLine(TIMING);
					Send(new char[] {IAC, WONT, TIMING});
					Console.WriteLine("发送: WONT TIMING");
					break;
				case DISPLOC:
					Console.WriteLine(DISPLOC);
					Send(new char[] {IAC, DONT, DISPLOC});
					Console.WriteLine("发送: DONT DISPLOC");
					break;
				case ENVIRON:
					Console.WriteLine(ENVIRON);
					Send(new char[] {IAC, DONT, ENVIRON});
					Console.WriteLine("发送: DONT ENVIRON");
					break;
				case UNKNOWN39:
					Console.WriteLine(UNKNOWN39);
					Send(new char[] {IAC, DONT, UNKNOWN39});
					Console.WriteLine("发送: DONT UNKNOWN39");
					break;
				default:
					Console.WriteLine("未知的选项");
					break;
			}
		}

		//处理WILL,以DO或者DONT响应
		private void ProcessWill(short ch)
		{
			switch((char)ch)
			{
				case ECHO:
					Console.WriteLine(ECHO);
					if (!sw_echo)
					{
						sw_echo = true;
						Send(new char[] {IAC, DO, ECHO});
						Console.WriteLine("发送: DO ECHO");
					}
					break;
				case SGA:
					Console.WriteLine(SGA);
					if (!sw_ugoahead)
					{
						Send(new char[] {IAC, DO, SGA});
						Console.WriteLine("发送: DO SGA");
						sw_ugoahead = true;
					}
					else
					{
						Console.WriteLine("不发送响应");
					}
					break;
				case TERMTYPE:
					Console.WriteLine("TERMTYPE");
					if (!sw_termsent)
					{
						Send(new char[] {IAC, WILL, TERMTYPE});
						Send(IAC + SB + TERMTYPE + (char)0 + "VT100" + IAC + SE);
						sw_termsent = true;
						Console.WriteLine("发送: SB TERMTYPE VT100");
					}
					break;
				case TERMSPEED:
					Console.WriteLine(TERMSPEED);
					Send(new char[] {IAC, DONT, TERMSPEED});
					Console.WriteLine("发送: DONT TERMSPEED");
					break;
				case TFLOWCNTRL:
					Console.WriteLine(TFLOWCNTRL);
					Send(new char[] {IAC, DONT, TFLOWCNTRL});
					Console.WriteLine("发送: DONT TFLOWCNTRL");
					break;
				case LINEMODE:
					Console.WriteLine(LINEMODE);
					Send(new char[] {IAC, WONT, LINEMODE});
					Console.WriteLine("发送: WONT LINEMODE");
					break;
				case STATUS:
					Console.WriteLine(STATUS);
					Send(new char[] {IAC, DONT, STATUS});
					Console.WriteLine("发送: DONT STATUS");
					break;
				case TIMING:
					Console.WriteLine(TIMING);
					Send(new char[] {IAC, DONT, TIMING});
					Console.WriteLine("发送: DONT TIMING");
					break;
				case DISPLOC:
					Console.WriteLine(DISPLOC);
					Send(new char[] {IAC, DONT, DISPLOC});
					Console.WriteLine("发送: DONT DISPLOC");
					break;
				case ENVIRON:
					Console.WriteLine(ENVIRON);
					Send(new char[] {IAC, DONT, ENVIRON});
					Console.WriteLine("发送: DONT ENVIRON");
					break;
				case UNKNOWN39:
					Console.WriteLine(UNKNOWN39);
					Send(new char[] {IAC, DONT, UNKNOWN39});
					Console.WriteLine("发送: DONT UNKNOWN39");
					break;
				default:
					Console.WriteLine("未知的选项");
					break;
			}
		}


	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -