\033[?1049h和\033[?1049l的功能是切换屏幕(见笔记 关于ansi转义序列控制符 ),下面的代码是一个Telnet Server的一部分,用Telnet Client连上后,先收到一个"hello",然后切换到一个新屏幕上再收到一个"hello",然后切换回原来的屏幕,并断开连接。
SocketAddress clientAddress = sock.getRemoteSocketAddress();
// sock is the incoming socket logger.info("Accept client from " + clientAddress); Thread.currentThread().setName(this.getClass().getName()); InputStream is = sock.getInputStream(); OutputStream os = sock.getOutputStream(); byte[] helloMsg = "hello".getBytes(); os.write(helloMsg); os.flush(); Thread.sleep(2000); byte[] switchScreen = { 0x1b, 0x5b, 0x3f, 0x31, 0x30, 0x34, 0x39, 0x68 }; // \033[?1049h, switch to a new screen os.write(switchScreen); os.flush(); Thread.sleep(2000); os.write(helloMsg); os.flush(); Thread.sleep(2000); byte[] switchBack = { 0x1b, 0x5b, 0x3f, 0x31, 0x30, 0x34, 0x39, 0x6c }; // \033[?1049l, switch back to original screen os.write(switchBack); os.flush(); sock.close();