包已经找到,commons-net-1.2.2.jar.里面的例示也运行了.照这例示写了一个就是不成功.能给对方发字符.但是只能发送一个字符.示例;__
package com.zystc.dyt;import org.apache.commons.net.telnet.*;
import java.io.*;public class TelnetSample
{
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private char prompt = '$'; public TelnetSample( String server, String user ) {
try {
// Connect to the specified server
telnet.connect( server, 1002 );
// Get input and output stream references
in = telnet.getInputStream();
out = new PrintStream( telnet.getOutputStream() ); // Advance to a prompt
write(user);
readUntil( prompt + " " );
}
catch( Exception e ) {
e.printStackTrace();
}
} public void su( String password ) {
try {
write( "su" );
readUntil( "Password: " );
write( password );
prompt = '#';
readUntil( prompt + " " );
}
catch( Exception e ) {
e.printStackTrace();
}
} public String readUntil( String pattern ) {
try {
System.out.println(pattern);
char lastChar = pattern.charAt( pattern.length() - 1 );
StringBuffer sb = new StringBuffer();
boolean found = false;
char ch = ( char )in.read();
while( true ) {
System.out.print( ch );
sb.append( ch );
if( ch == lastChar ) {
if( sb.toString().endsWith( pattern ) ) {
return sb.toString();
}
}
ch = ( char )in.read();
}
}
catch( Exception e ) {
e.printStackTrace();
}
return null;
} public void write( String value ) {
try {
// send
// out.write()
out.println(value);
}
catch( Exception e ) {
e.printStackTrace();
}
} public String sendCommand( String command ) {
try {
write( command );
return readUntil( prompt + " " );
}
catch( Exception e ) {
e.printStackTrace();
}
return null;
} public void disconnect() {
try {
telnet.disconnect();
}
catch( Exception e ) {
e.printStackTrace();
}
} public static void main( String[] args ) {
try {
TelnetSample telnet = new TelnetSample( "127.0.0.1","qwew" );
telnet.disconnect();
}
catch( Exception e ) {
e.printStackTrace();
}
}
}
对方是用C++写的一个telnet协议.只要求给对方发送一个完正的字符串就行了.然后为exit.
package com.zystc.dyt;import org.apache.commons.net.telnet.*;
import java.io.*;public class TelnetSample
{
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private char prompt = '$'; public TelnetSample( String server, String user ) {
try {
// Connect to the specified server
telnet.connect( server, 1002 );
// Get input and output stream references
in = telnet.getInputStream();
out = new PrintStream( telnet.getOutputStream() ); // Advance to a prompt
write(user);
readUntil( prompt + " " );
}
catch( Exception e ) {
e.printStackTrace();
}
} public void su( String password ) {
try {
write( "su" );
readUntil( "Password: " );
write( password );
prompt = '#';
readUntil( prompt + " " );
}
catch( Exception e ) {
e.printStackTrace();
}
} public String readUntil( String pattern ) {
try {
System.out.println(pattern);
char lastChar = pattern.charAt( pattern.length() - 1 );
StringBuffer sb = new StringBuffer();
boolean found = false;
char ch = ( char )in.read();
while( true ) {
System.out.print( ch );
sb.append( ch );
if( ch == lastChar ) {
if( sb.toString().endsWith( pattern ) ) {
return sb.toString();
}
}
ch = ( char )in.read();
}
}
catch( Exception e ) {
e.printStackTrace();
}
return null;
} public void write( String value ) {
try {
// send
// out.write()
out.println(value);
}
catch( Exception e ) {
e.printStackTrace();
}
} public String sendCommand( String command ) {
try {
write( command );
return readUntil( prompt + " " );
}
catch( Exception e ) {
e.printStackTrace();
}
return null;
} public void disconnect() {
try {
telnet.disconnect();
}
catch( Exception e ) {
e.printStackTrace();
}
} public static void main( String[] args ) {
try {
TelnetSample telnet = new TelnetSample( "127.0.0.1","qwew" );
telnet.disconnect();
}
catch( Exception e ) {
e.printStackTrace();
}
}
}
对方是用C++写的一个telnet协议.只要求给对方发送一个完正的字符串就行了.然后为exit.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货