este código logra establecer una conexion telnet con un servidor.
me falta mejorarlo. Todavía es muy ineficiente.
si le haces algunas mejoras por favor envialo a
[email protected]
Espero que te sirva...
import java.io.*;
import java.net.*;
import java.util.*;
public class TelnetInputStream extends InputStream
{
private static DataInputStream input;
private static DataOutputStream output;
private int width, height;
private String terminal;
byte IAC=0;
byte SB=0;
// used for replies
private final byte[] reply = { IAC, (byte) 0, (byte) 0 };
char c;
public void ESPERAR()throws IOException, InterruptedException{
Thread.sleep(10);
}
public TelnetInputStream(InputStream inInput, OutputStream inOutput)
{
try{
String linea=null;
int N,i;
while(true){
try{ESPERAR();}catch (InterruptedException e){e.printStackTrace();}
System.out.print('*');
N=inInput.available();
if(N!=0){
System.out.print('\n');
for(i=0;i<N;i++){
c=(char)inInput.read();
System.out.print(c);
}
try{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));linea = br.readLine();}catch(Exception e){e.printStackTrace();}
inOutput.write((linea + "\n").getBytes());
inOutput.flush();
}
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] arg)
{
try
{
Socket s = new Socket("localhost",5000);
System.out.println(s);
input = new DataInputStream(s.getInputStream());
output = new DataOutputStream(s.getOutputStream());
TelnetInputStream dir = new TelnetInputStream(input, output);
}
catch(IOException e)
{
System.out.println("Error "+e);
}
}
}