📄 telnetclient.java
字号:
import java.awt.*;import java.awt.event.*;import javax.swing.*;import ipworks.*;//borland的一个网络包public class TelnetClient extends JFrame { private JPanel contentPane; private Label label = new Label(); private TextField textField = new TextField(); private Button button = new Button(); private TextArea textArea = new TextArea(); private Telnet telnet = new Telnet(); private boolean packFrame = false; public TelnetClient() {//构造体 enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { toInit(); } catch(Exception e) { e.printStackTrace(); System.out.println("error1\n"); } } private void toInit() throws Exception {//初始化窗体 contentPane = (JPanel) this.getContentPane();//初始化面板 contentPane.setLayout(null); this.setSize(new Dimension(500,500)); this.setTitle("telnet客户端"); textArea.setBounds(new Rectangle(20,20,450,320)); textArea.setEditable(true); textArea.addKeyListener(new java.awt.event.KeyAdapter() {//在文本框中响应键盘事件 public void keyTyped(KeyEvent e) { textArea_keyTyped(e); } }); label.setBounds(new Rectangle(20,400,150,20)); label.setText("输入要连接bbs服务器:"); textField.setBounds(new Rectangle(180,400,210,20)); textField.setText("itechs.iscas.ac.cn");//默认的telnet主机地址 button.setBounds(new Rectangle(400,400,70,20)); button.setLabel("建立连接"); button.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { button_actionPerformed(e); } }); contentPane.add(label,null); contentPane.add(button,null); contentPane.add(textArea,null); contentPane.add(textField,null); telnet.addTelnetEventListener(new ipworks.TelnetEventListener() {//telnet要响应的事件 public void command(TelnetCommandEvent e) { } public void connected(TelnetConnectedEvent e) { telnet_connected(e); } public void dataIn(TelnetDataInEvent e) { telnet_dataIn(e); } public void disconnected(TelnetDisconnectedEvent e) { telnet_disconnected(e); } public void doDo(TelnetDoDoEvent e) { telnet_doDo(e); } public void dont(TelnetDontEvent e) { } public void error(TelnetErrorEvent e) { } public void readyToSend(TelnetReadyToSendEvent e) { } public void subOption(TelnetSubOptionEvent e) { } public void will(TelnetWillEvent e) { telnet_will(e); } public void wont(TelnetWontEvent e) { } });//事件列表完 } public static void main(String[] args) { TelnetClient td = new TelnetClient();//实例化当前类 td.setVisible(true);//设为可见 } protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } void button_actionPerformed(ActionEvent e) {//若不联机则建立连接,否则断开连接 try{ if(button.getLabel() == "建立连接"){ telnet.setTimeout(200);//连接的最大缺省时间 telnet.setRemoteHost(textField.getText());//设置要连接的远程主机 telnet.setConnected(true);//处于连接状态 textArea.append("\n正在和主机连接...请等待\n"); } else { telnet.disconnect();//断开连接 telnet.setConnected(false);//处于脱机状态 textArea.append("\n和主机的连接断开\n"); } } catch (Exception ipwe) { System.out.println("error2\n"); ipwe.printStackTrace(); } } void telnet_dataIn(TelnetDataInEvent e) {//响应接收到主机数据的事件 textArea.appendText(new String(e.text));//显示接收到的数据 textArea.setCaretPosition(textArea.getText().length());//设置当前的输入位置 } void telnet_doDo(TelnetDoDoEvent e) {//响应主机发来的telnet DO OPTION命令的事件 try { telnet.setWontOption(e.optionCode);//向主机发送单字节的Telnet option code响应发来的命令 } catch (Exception ipwe) { System.out.println("error3\n"); } } void telnet_will(TelnetWillEvent e) {//响应收到Telnet WILL OPTION命令的事件 try { telnet.setWillOption(e.optionCode);//向主机发送单字节的Telnet option code响应发来的命令 } catch (Exception ipwe) { System.out.println("error4\n"); } } void textArea_keyTyped(KeyEvent e) {//响应在文字显示区域的键盘事件 if(telnet.isConnected()){//如果建立连接,把键盘输入发给主机 try { byte[] toSend = new byte[1]; toSend[0] = (byte)e.getKeyChar(); telnet.setDataToSend(toSend); e.consume();//不产生缺省得按键动作 } catch (Exception ipwe) { System.out.println("error5\n"); } } else textArea.append("\n没有建立连接"); } void telnet_connected(TelnetConnectedEvent e) { if (telnet.isConnected()) button.setLabel("断开连接"); } void telnet_disconnected(TelnetDisconnectedEvent e) { button.setLabel("建立连接"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -